-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTCPServer.h
75 lines (60 loc) · 1.26 KB
/
TCPServer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once
#include "mg/aio/IOTask.h"
#include "mg/box/Mutex.h"
namespace mg {
namespace aio {
class IOCore;
enum TCPServerState
{
TCP_SERVER_STATE_NEW,
TCP_SERVER_STATE_BOUND,
TCP_SERVER_STATE_LISTENING,
TCP_SERVER_STATE_CLOSING,
TCP_SERVER_STATE_CLOSED,
};
class TCPServerSubscription
{
protected:
~TCPServerSubscription() = default;
public:
virtual void OnAccept(
mg::net::Socket aSock,
const mg::net::Host& aHost) = 0;
// Close is signaled only if Listen() was successful beforehand.
virtual void OnClose() = 0;
};
class TCPServer
: private IOSubscription
{
public:
SHARED_PTR_RENEW_API(TCPServer)
bool Bind(
const mg::net::Host& aHost,
mg::box::Error::Ptr& aOutErr);
uint16_t GetPort() const;
bool Listen(
uint32_t aBacklog,
TCPServerSubscription* aSub,
mg::box::Error::Ptr& aOutErr);
void PostClose();
bool IsClosed() const;
IOCore& GetCore();
private:
TCPServer(IOCore& aCore);
~TCPServer() override;
void OnEvent(
const IOArgs& aArgs) override;
mutable mg::box::Mutex myMutex;
TCPServerState myState;
IOTask myTask;
IOEvent myAcceptEvent;
TCPServerSubscription* mySub;
IOServerSocket* myBoundSocket;
};
inline IOCore&
TCPServer::GetCore()
{
return myTask.GetCore();
}
}
}