|
|
91ef56 |
#ifndef SOCKET_H
|
|
|
91ef56 |
#define SOCKET_H
|
|
|
91ef56 |
|
|
|
91ef56 |
|
|
|
541903 |
#include <thread></thread>
|
|
|
541903 |
#include <atomic></atomic>
|
|
|
91ef56 |
|
|
|
541903 |
#include "protocol.h"
|
|
|
541903 |
|
|
|
541903 |
|
|
|
9bbba5 |
enum : ErrorCode {
|
|
|
9bbba5 |
ERR_TCP_COMMON = ERR_TCP,
|
|
|
9bbba5 |
ERR_TCP_WORKER_FAILED,
|
|
|
9bbba5 |
};
|
|
|
9bbba5 |
|
|
|
9bbba5 |
|
|
|
541903 |
class TcpProtocol;
|
|
|
541903 |
|
|
|
541903 |
|
|
|
9bbba5 |
class TcpSocket final: public Socket {
|
|
|
541903 |
public:
|
|
|
541903 |
const int sockId;
|
|
|
541903 |
protected:
|
|
|
541903 |
friend class TcpProtocol;
|
|
|
541903 |
|
|
|
541903 |
TcpSocket(
|
|
|
541903 |
Protocol &protocol,
|
|
|
541903 |
const Connection::Handle &connection,
|
|
|
541903 |
const Server::Handle &server,
|
|
|
541903 |
const Address &address,
|
|
|
541903 |
int sockId );
|
|
|
541903 |
|
|
|
541903 |
TcpProtocol& getProtocol()
|
|
|
541903 |
{ return *(TcpProtocol*)&protocol; }
|
|
|
541903 |
};
|
|
|
541903 |
|
|
|
541903 |
|
|
|
541903 |
class TcpProtocol: public Protocol {
|
|
|
541903 |
private:
|
|
|
541903 |
friend class TcpSocket;
|
|
|
541903 |
|
|
|
2499ad |
std::mutex mutexIdle;
|
|
|
2499ad |
std::mutex mutexRun;
|
|
|
2499ad |
std::atomic<bool> wantRun;</bool>
|
|
|
2499ad |
|
|
|
541903 |
std::thread *thread;
|
|
|
541903 |
int epollId;
|
|
|
9bbba5 |
int eventsId;
|
|
|
541903 |
|
|
|
2499ad |
void controlCommand(unsigned long long cmd);
|
|
|
2499ad |
void threadConnectionWrite(TcpSocket *socket, const Connection::Handle &connection);
|
|
|
2499ad |
void threadConnectionRead(TcpSocket *socket, const Connection::Handle &connection);
|
|
|
2499ad |
void threadProcessEvents(void *eventsPtr, int count);
|
|
|
541903 |
void threadRun();
|
|
|
541903 |
|
|
|
541903 |
public:
|
|
|
541903 |
TcpProtocol();
|
|
|
5a39be |
~TcpProtocol();
|
|
|
541903 |
|
|
|
541903 |
ErrorCode resolve(Address &address) override;
|
|
|
646228 |
|
|
|
646228 |
protected:
|
|
|
9bbba5 |
ErrorCode onStart() override;
|
|
|
9bbba5 |
void onStop(ErrorCode) override;
|
|
|
9bbba5 |
void onSocketChanged(Socket *socket) override;
|
|
|
646228 |
ErrorCode onConnect(const Connection::Handle &connection, const Address &address) override;
|
|
|
646228 |
ErrorCode onListen(const Server::Handle &server, const Address &address) override;
|
|
|
541903 |
};
|
|
|
91ef56 |
|
|
|
91ef56 |
|
|
|
91ef56 |
#endif
|