-
Notifications
You must be signed in to change notification settings - Fork 1
/
sockets_cpp.h
160 lines (132 loc) · 4.04 KB
/
sockets_cpp.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
149
150
151
152
153
154
155
156
157
158
159
/*
*
* Copyright (C) 2009 Daniel J. Calandria Hernández &
* Antonio Cañas Vargas
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __swad_socket_h
#define __swad_socket_h
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <cstdarg>
#include <time.h>
#include <vector>
enum
{
E_RW_ERROR = -2,
E_TIMEOUT = -8,
E_RW_RETRY = -1,
E_FAIL = -1,
};
enum
{
S_CONNECTION_CLOSED = -1,
S_OK = 0,
};
const unsigned DEF_MAX_CONNECTIONS = 1000;
const unsigned DEF_MAX_QUEUE = 100;
const unsigned DEF_TIMEOUT = 100;
class SocketSession
{
public:
virtual ssize_t Read ( char *buf, size_t len ) = 0;
virtual ssize_t Write (const char *buf, size_t len ) = 0;
virtual int Close () = 0;
//virtual ssize_t printf ( const char *fmt, ... ) = 0;
virtual ~SocketSession() {}
};
class SocketServer
{
public:
virtual int Open ( int port, unsigned max_connections, unsigned max_queue, time_t timeout = DEF_TIMEOUT) = 0;
virtual int Close () = 0;
virtual SocketSession* WaitConnection () = 0;
virtual ~SocketServer() {}
};
class SocketSessionNonBlock;
class SocketServerNonBlock : public SocketServer
{
protected:
time_t timeout;
int listener;
int max_descriptor;
fd_set static_tbl;
fd_set dynamic_tbl;
std::vector < SocketSessionNonBlock* > sessions;
public:
SocketServerNonBlock ();
virtual ~SocketServerNonBlock ();
SocketServerNonBlock ( int port, unsigned max_connections = DEF_MAX_CONNECTIONS, unsigned max_queue = DEF_MAX_QUEUE,
time_t timeout = DEF_TIMEOUT );
SocketServerNonBlock ( const SocketServerNonBlock &src );
SocketServerNonBlock& operator= ( const SocketServerNonBlock &src);
public:
virtual int Open ( int port, unsigned max_connections = DEF_MAX_CONNECTIONS, unsigned max_queue = DEF_MAX_QUEUE,
time_t timeout = DEF_TIMEOUT );
virtual int Close ( );
virtual int SetMaxConnections ( unsigned max_connections );
virtual int Update ( unsigned usec = 500000 );
SocketSession* WaitConnection ( );
size_t Broadcast ( const char *msg, size_t size );
friend class SocketSessionNonBlock;
protected:
int CloseSession ( SocketSessionNonBlock* session );
};
class SocketSessionNonBlock : public SocketSession
{
protected:
int socket;
int idx;
bool selected;
time_t timeout;
time_t next_timeout;
SocketServerNonBlock *server;
bool is_timeout;
public:
SocketSessionNonBlock( time_t timeout = 0 );
virtual ~SocketSessionNonBlock();
virtual ssize_t Read (char *buf, size_t len);
virtual ssize_t Write (const char *buf, size_t len );
virtual int Close ();
virtual time_t GetTimeout() const { return next_timeout; }
virtual void SetTimeout (time_t timeout_)
{
next_timeout = time(0) + timeout_;
timeout = timeout_;
}
friend class SocketServerNonBlock;
};
/* TODO */
class SocketClient
{
protected:
int sckt;
fd_set select_flag;
public:
SocketClient ();
virtual ~SocketClient ();
virtual int Connect ( const char *server_name, int port );
virtual int Close ();
virtual ssize_t Read ( char *buf, size_t len, unsigned usec = 500000 );
virtual ssize_t Write ( const char *buf, size_t len );
};
#endif