|
|
71057f |
#ifndef CONNECTION_H
|
|
|
71057f |
#define CONNECTION_H
|
|
|
71057f |
|
|
|
71057f |
|
|
|
71057f |
#include <cassert></cassert>
|
|
|
71057f |
|
|
|
71057f |
#include <atomic></atomic>
|
|
|
71057f |
#include <condition_variable></condition_variable>
|
|
|
71057f |
|
|
|
71057f |
#include "utils.h"
|
|
|
71057f |
|
|
|
71057f |
|
|
|
71057f |
#define MAX_ADDR_SIZE 256
|
|
|
71057f |
|
|
|
71057f |
|
|
|
71057f |
class Protocol;
|
|
|
71057f |
class Server;
|
|
|
71057f |
|
|
|
71057f |
|
|
|
71057f |
class Root {
|
|
|
71057f |
public:
|
|
|
71057f |
virtual ~Root() { }
|
|
|
71057f |
};
|
|
|
71057f |
|
|
|
71057f |
|
|
|
71057f |
class Connection: public Root {
|
|
|
71057f |
public:
|
|
|
71057f |
enum State: int {
|
|
|
71057f |
STATE_NONE,
|
|
|
71057f |
STATE_OPENING,
|
|
|
71057f |
STATE_INITIALIZING,
|
|
|
71057f |
STATE_RESOLVING,
|
|
|
71057f |
STATE_CONNECTING,
|
|
|
71057f |
STATE_OPEN,
|
|
|
71057f |
STATE_CLOSE_REQ,
|
|
|
71057f |
STATE_CLOSING,
|
|
|
71057f |
STATE_CLOSED,
|
|
|
71057f |
STATE_FINISHED
|
|
|
71057f |
};
|
|
|
71057f |
|
|
|
71057f |
struct Task {
|
|
|
71057f |
void *data;
|
|
|
71057f |
size_t size;
|
|
|
71057f |
size_t completion;
|
|
|
71057f |
bool watch;
|
|
|
71057f |
Task *next;
|
|
|
71057f |
|
|
|
71057f |
inline explicit Task(
|
|
|
71057f |
void *data = nullptr,
|
|
|
71057f |
size_t size = 0,
|
|
|
71057f |
size_t completion = 0,
|
|
|
71057f |
bool watch = false,
|
|
|
71057f |
Task *next = nullptr
|
|
|
71057f |
):
|
|
|
71057f |
data(data), size(size), completion(completion), watch(watch), next(next)
|
|
|
71057f |
{ assert(!!data == !!size); }
|
|
|
71057f |
};
|
|
|
71057f |
|
|
|
71057f |
typedef CounteredQueueMISO<task, &task::next=""> TaskQueue;</task,>
|
|
|
71057f |
|
|
|
71057f |
private:
|
|
|
71057f |
friend class Protocol;
|
|
|
71057f |
|
|
|
71057f |
std::atomic<state> state;</state>
|
|
|
71057f |
std::atomic<state> stateWanted;</state>
|
|
|
71057f |
ReadProtector stateProtector;
|
|
|
71057f |
std::atomic<bool> enqueued;</bool>
|
|
|
71057f |
std::atomic<bool> error;</bool>
|
|
|
71057f |
|
|
|
71057f |
std::atomic<int> closeWaiters;</int>
|
|
|
71057f |
std::atomic<int> finishWaiters;</int>
|
|
|
71057f |
std::condition_variable closeCondition;
|
|
|
71057f |
|
|
|
71057f |
unsigned char address[MAX_ADDR_SIZE];
|
|
|
71057f |
size_t addressSize;
|
|
|
71057f |
|
|
|
71057f |
Protocol *protocol;
|
|
|
71057f |
Server *server;
|
|
|
71057f |
Connection *prev, *next;
|
|
|
71057f |
Connection *srvPrev, *srvNext;
|
|
|
71057f |
Connection *queueNext;
|
|
|
71057f |
int sockId;
|
|
|
71057f |
State stateLocal;
|
|
|
6ebd3b |
bool errorLocal;
|
|
|
71057f |
|
|
|
71057f |
std::atomic<unsigned int=""> events;</unsigned>
|
|
|
71057f |
TaskQueue readQueue;
|
|
|
71057f |
TaskQueue writeQueue;
|
|
|
71057f |
|
|
|
71057f |
void wantState(State state, bool error);
|
|
|
71057f |
|
|
|
71057f |
public:
|
|
|
71057f |
typedef QueueMISO<connection, &connection::queuenext=""> Queue;</connection,>
|
|
|
71057f |
friend class QueueMISO<connection, &connection::queuenext="">;</connection,>
|
|
|
71057f |
friend class ChainFuncs<connection, &connection::queuenext="">;</connection,>
|
|
|
71057f |
|
|
|
71057f |
public:
|
|
|
71057f |
Connection();
|
|
|
71057f |
virtual ~Connection();
|
|
|
71057f |
|
|
|
6ebd3b |
bool open(Protocol &protocol, void *address, size_t addressSize, int sockId = -1, Server *server = nullptr);
|
|
|
71057f |
void closeReq();
|
|
|
71057f |
void close(bool error);
|
|
|
71057f |
void closeWait(unsigned long long timeoutUs = 0, bool withReq = true, bool error = true);
|
|
|
71057f |
|
|
|
71057f |
bool read(Task &task);
|
|
|
71057f |
bool write(Task &task);
|
|
|
71057f |
|
|
|
71057f |
protected:
|
|
|
71057f |
virtual void onOpeningError();
|
|
|
71057f |
virtual void onOpen(const void *address, size_t addressSize);
|
|
|
71057f |
virtual bool onReadReady(Task &task, bool error);
|
|
|
71057f |
virtual bool onWriteReady(Task &task, bool error);
|
|
|
71057f |
virtual void onCloseReqested();
|
|
|
71057f |
virtual void onClose(bool error);
|
|
|
71057f |
};
|
|
|
71057f |
|
|
|
71057f |
|
|
|
71057f |
#endif
|