-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSocket.h
148 lines (111 loc) · 3.15 KB
/
Socket.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#pragma once
#include "Common.h"
#include "SocketState.h"
#include "ISocketBaseEvent.h"
#include "Buffer.h"
#include "SockAddr.h"
#include <vector>
#include <map>
#include <functional>
EASY_NS_BEGIN
class SocketBase;
class SocketError {
public:
SocketError(int code, int inter = 0, const char *msg = "", SocketBase* s = 0);
void formatError();
int code;
int internalCode;
string msg;
SocketBase* s;
};
class UdpData : public Ref {
public:
UdpData(Buffer *b, SockAddr *a);
~UdpData();
Buffer *buffer;
SockAddr *addr;
};
typedef function< void(void*) > EventFunc;
class Socket : public ISocketBaseEvent {
public:
friend class SocketState;
friend class SocketStateConnecting;
friend class SocketStateConnected;
friend class SocketStateListening;
typedef vector<Socket*> SocketVec;
static inline const char* getMulticastAddr(int protocol);
Socket();
Socket(SocketBase *p);
virtual ~Socket();
bool connect(int port, const char *ip);
bool listen(int port, const char *ip = 0);
bool send(const char *buf, int len = 0);
void close();
void shutdown();
bool create(int protocol = -1);
// udp
bool send(const char *buf, int len, int port, const char *ip);
// multi cast
bool addMembership(const char *addr = 0, const char *interface_ = 0);
bool dropMembership(const char *addr = 0, const char *interface_ = 0);
// methods
void closeConnection(int i);
void closeAllConnections();
// Note: need update state manually
void update();
bool isConnected();
bool isClosed();
int getState();
virtual void onError(int error, int internalError);
virtual void onClose(bool hasError = false);
virtual void onListening();
virtual void onConnection(SocketBase *s);
virtual void onConnect();
virtual void onData();
int select(int ms = 0);
bool canRead();
bool canWrite();
void on(int name, EventFunc cb);
Socket* getConnection(int i);
SocketVec& getConnections() { return connections; }
int getMaxConnections() { return maxConnections; }
float getConnectTimeout() { return connectTimeoutSecs; }
const char* getIp();
int getPort();
float getIOTimeout() { return ioTimeoutSecs; }
bool isUdp() { return socket_type == SOCK_DGRAM; }
void setMaxConnections(int max) { maxConnections = max; }
void setConnectTimeout(float sec) { connectTimeoutSecs = sec; }
void setCheckIpv6Only(bool check) { checkIpv6Only = check; }
void setSocketType(int type);
void setUdp() { setSocketType(SOCK_DGRAM); }
void setIOTimeout(float sec) { ioTimeoutSecs = sec; }
protected:
typedef vector<EventFunc> EventFuncVec;
typedef map<int, EventFuncVec*> EventMap;
void setState(StateType type, void *param = 0);
void recv();
bool accept();
bool checkConnected();
void emitError();
void recvfrom();
SockAddr* getUdpAddr();
void emit(int name, void *p = 0);
// support ipv6-only
int checkProtocol(int port, const char *ip, bool passive = false);
SocketBase *impl;
SocketState *state;
EventMap events;
SocketVec connections;
int maxConnections;
// -1 means socket default timeout
float connectTimeoutSecs;
addrinfo* addrInfo;
bool checkIpv6Only;
int socket_type;
SockAddr *addrUdp;
SockAddr *addrTemp;
// -1 means never
float ioTimeoutSecs;
};
EASY_NS_END