|
|
71057f |
#ifndef CONNECTION_H
|
|
|
71057f |
#define CONNECTION_H
|
|
|
71057f |
|
|
|
71057f |
|
|
|
b42f0a |
#include "socket.h"
|
|
|
71057f |
|
|
|
71057f |
|
|
|
71057f |
class Server;
|
|
|
71057f |
|
|
|
71057f |
|
|
|
b42f0a |
class Connection: public Socket {
|
|
|
71057f |
public:
|
|
|
71057f |
struct Task {
|
|
|
71057f |
void *data;
|
|
|
71057f |
size_t size;
|
|
|
71057f |
size_t completion;
|
|
|
71057f |
Task *next;
|
|
|
71057f |
|
|
|
71057f |
inline explicit Task(
|
|
|
71057f |
void *data = nullptr,
|
|
|
71057f |
size_t size = 0,
|
|
|
71057f |
size_t completion = 0,
|
|
|
71057f |
Task *next = nullptr
|
|
|
71057f |
):
|
|
|
b42f0a |
data(data), size(size), completion(completion), next(next)
|
|
|
71057f |
{ assert(!!data == !!size); }
|
|
|
71057f |
};
|
|
|
71057f |
typedef CounteredQueueMISO<task, &task::next=""> TaskQueue;</task,>
|
|
|
71057f |
|
|
|
71057f |
private:
|
|
|
71057f |
friend class Protocol;
|
|
|
b42f0a |
friend class Server;
|
|
|
71057f |
|
|
|
71057f |
Server *server;
|
|
|
71057f |
Connection *srvPrev, *srvNext;
|
|
|
6ebd3b |
bool errorLocal;
|
|
|
71057f |
|
|
|
71057f |
TaskQueue readQueue;
|
|
|
71057f |
TaskQueue writeQueue;
|
|
|
71057f |
|
|
|
b42f0a |
bool open(Protocol &protocol, void *address, size_t addressSize, int sockId, Server *server);
|
|
|
71057f |
|
|
|
71057f |
public:
|
|
|
71057f |
Connection();
|
|
|
b42f0a |
~Connection();
|
|
|
71057f |
|
|
|
b42f0a |
bool open(Protocol &protocol, void *address, size_t addressSize);
|
|
|
71057f |
|
|
|
71057f |
bool read(Task &task);
|
|
|
71057f |
bool write(Task &task);
|
|
|
71057f |
|
|
|
b42f0a |
private:
|
|
|
b42f0a |
void handleState() override;
|
|
|
b42f0a |
void handleEvents(unsigned int events) override;
|
|
|
b42f0a |
|
|
|
71057f |
protected:
|
|
|
b42f0a |
virtual void onReadReady(Task &task, bool error);
|
|
|
b42f0a |
virtual void onWriteReady(Task &task, bool error);
|
|
|
71057f |
};
|
|
|
71057f |
|
|
|
71057f |
|
|
|
71057f |
#endif
|