|
|
71057f |
#ifndef PROTOCOL_H
|
|
|
71057f |
#define PROTOCOL_H
|
|
|
71057f |
|
|
|
71057f |
|
|
|
71057f |
#include <thread></thread>
|
|
|
71057f |
|
|
|
71057f |
#include "utils.h"
|
|
|
71057f |
#include "connection.h"
|
|
|
71057f |
#include "server.h"
|
|
|
71057f |
|
|
|
71057f |
|
|
|
71057f |
|
|
|
71057f |
class Protocol {
|
|
|
71057f |
public:
|
|
|
71057f |
enum State {
|
|
|
71057f |
STATE_NONE,
|
|
|
71057f |
STATE_OPENING,
|
|
|
51b3f0 |
STATE_INITIALIZING,
|
|
|
71057f |
STATE_OPEN,
|
|
|
71057f |
STATE_CLOSE_REQ,
|
|
|
71057f |
STATE_CLOSING,
|
|
|
71057f |
STATE_CLOSED,
|
|
|
71057f |
STATE_FINISHED
|
|
|
71057f |
};
|
|
|
71057f |
|
|
|
71057f |
private:
|
|
|
71057f |
friend class Server;
|
|
|
71057f |
friend class Connection;
|
|
|
71057f |
|
|
|
71057f |
std::thread *thread;
|
|
|
71057f |
int epollFd;
|
|
|
71057f |
int eventFd;
|
|
|
71057f |
|
|
|
71057f |
std::atomic<state> state;</state>
|
|
|
71057f |
std::atomic<state> stateWanted;</state>
|
|
|
71057f |
ReadProtector stateProtector;
|
|
|
71057f |
|
|
|
71057f |
std::atomic<int> closeWaiters;</int>
|
|
|
71057f |
std::atomic<int> finishWaiters;</int>
|
|
|
71057f |
std::condition_variable closeCondition;
|
|
|
71057f |
|
|
|
71057f |
Connection *connFirst, *connLast;
|
|
|
71057f |
Connection::Queue connQueue;
|
|
|
71057f |
|
|
|
71057f |
Server *srvFirst, *srvLast;
|
|
|
71057f |
Server::Queue srvQueue;
|
|
|
71057f |
|
|
|
71057f |
void wakeup();
|
|
|
71057f |
void updateEvents(Connection &connection);
|
|
|
71057f |
void initSocket(int sockId);
|
|
|
71057f |
|
|
|
71057f |
void wantState(State state);
|
|
|
71057f |
|
|
|
71057f |
void threadConnQueue();
|
|
|
71057f |
void threadConnEvents(Connection &connection, unsigned int events);
|
|
|
6ebd3b |
void threadSrvDisconnect(Server &server, Connection &connection, bool error);
|
|
|
71057f |
void threadSrvQueue();
|
|
|
71057f |
void threadSrvEvents(Server &server, unsigned int events);
|
|
|
71057f |
void threadRun();
|
|
|
71057f |
|
|
|
71057f |
public:
|
|
|
71057f |
Protocol();
|
|
|
71057f |
virtual ~Protocol();
|
|
|
71057f |
|
|
|
71057f |
bool open();
|
|
|
71057f |
void closeReq();
|
|
|
71057f |
void close();
|
|
|
71057f |
void closeWait(unsigned long long timeoutUs = 0, bool withReq = true);
|
|
|
71057f |
};
|
|
|
71057f |
|
|
|
71057f |
|
|
|
71057f |
#endif
|