-
Notifications
You must be signed in to change notification settings - Fork 0
/
sock_wrap.h
293 lines (262 loc) · 7.79 KB
/
sock_wrap.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#ifndef __SOCK_WRAP_H__
#define __SOCK_WRAP_H__
#include <cstring>
#include <iostream>
#include <netdb.h>
#include <netinet/in.h>
#include <string>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
namespace ModelSQL
{
const char *default_address = "mysocket";
const char *server_invitation = "Enter the name of the file that will record the result of the work";
const char *server_stop = "Input 'END' to stop";
// SocketException --- Exception class
class SocketException
{
public:
std :: string Message;
enum socket_exception_code
{
ESE_SOCKCREATE,
ESE_SOCKCONN,
ESE_SOCKSEND,
ESE_SOCKRECV,
ESE_SOCKBIND,
ESE_SOCKLISTEN,
ESE_SOCKACCEPT
};
SocketException (socket_exception_code);
void report ();
};
// BaseSocket --- basic class fot sockets
class BaseSocket
{
protected:
int m_Socket;
struct sockaddr_un m_SockAddr;
public:
explicit BaseSocket (int sd = -1, const char *address = NULL);
// sending info
void Write (void *, int);
void PutChar (char);
void PutString (const char *);
void PutString (const std::string &);
// recieving info
int Read (void *, int);
char GetChar ();
std::string GetString ();
int GetSockDescriptor ();
~ BaseSocket () {}
};
// ServerSocket --- class for server sockets
class ServerSocket : public BaseSocket
{
public:
ServerSocket (const char *);
void Bind ();
void Listen (const int);
BaseSocket * Accept ();
~ ServerSocket ();
};
// ClientSocket --- class for client sockets
class ClientSocket: public BaseSocket
{
public:
ClientSocket (const char *);
void Connect ();
~ ClientSocket ();
};
//------------------------------SocketException methods ------------------------------
SocketException :: SocketException (socket_exception_code errcode)
{
switch (errcode) {
case ESE_SOCKCREATE:
Message = "Socket error: server - unsuccessful 'socket'";
break;
case ESE_SOCKCONN:
Message = "Socket error: client - unsuccessful 'connect'";
break;
case ESE_SOCKSEND:
Message = "Socket error: unsuccessful 'send'";
break;
case ESE_SOCKRECV:
Message = "Socket error: unsuccessful 'recv'";
break;
case ESE_SOCKBIND:
Message = "Socket error: server - unsuccessful 'bind'";
break;
case ESE_SOCKLISTEN:
Message = "Socket error errorOR: server - unsuccessful 'listen'";
break;
case ESE_SOCKACCEPT:
Message = "Socket error: server - unsuccessful 'accept'";
break;
}
}
void SocketException :: report ()
{
std :: cout << Message << std :: endl << "Code: " << errno << std :: endl;
return;
}
//------------------------------ BaseSocket methods ------------------------------
BaseSocket :: BaseSocket (int sd, const char * address)
{
m_Socket = sd;
if (address != NULL)
{
m_SockAddr.sun_family = AF_UNIX;
strcpy (m_SockAddr.sun_path, address);
}
}
void BaseSocket :: Write (void * buf, int len)
{
int s_send = send (m_Socket, buf, len, 0);
if (s_send == -1)
{
throw SocketException (SocketException :: ESE_SOCKSEND);
}
}
void BaseSocket :: PutChar (char sym)
{
int s_send = send (m_Socket, (char *) &sym, sizeof (char), 0);
if (s_send == -1)
{
throw SocketException (SocketException :: ESE_SOCKSEND);
}
}
void BaseSocket :: PutString (const char * str)
{
int s_send = send (m_Socket, str, strlen (str) * sizeof (char), 0);
if (s_send == -1)
{
throw SocketException (SocketException :: ESE_SOCKSEND);
}
if (str[strlen (str) - 1] != '\n')
BaseSocket :: PutChar ('\n');
}
void BaseSocket :: PutString (const std::string &str)
{
BaseSocket :: PutString (str.c_str());
}
int BaseSocket :: Read (void * buf, int len)
{
int s_recv = 0;
while (s_recv == 0)
{
s_recv = recv (m_Socket, buf, len, 0);
}
if (s_recv == -1)
{
throw SocketException (SocketException :: ESE_SOCKRECV);
}
return s_recv;
}
char BaseSocket :: GetChar ()
{
char sym;
int s_recv = 0;
while (s_recv == 0)
{
s_recv = recv (m_Socket, (char *) &sym, sizeof (char), 0);
}
if (s_recv == -1)
{
throw SocketException (SocketException :: ESE_SOCKRECV);
}
return sym;
}
std::string BaseSocket :: GetString ()
{
std :: string str;
char sym = ' ';
while ((sym != EOF) && (sym != '\n'))
{
sym = BaseSocket :: GetChar ();
str.push_back (sym);
}
return str;
}
int BaseSocket :: GetSockDescriptor ()
{
return m_Socket;
}
//------------------------------ ServerSocket methods ------------------------------
ServerSocket :: ServerSocket (const char * address)
{
// create sockets for one computer
int sock_fd = socket (AF_UNIX, SOCK_STREAM, 0);
if (sock_fd == -1)
{
throw SocketException (SocketException :: ESE_SOCKCREATE);
}
m_Socket = sock_fd;
m_SockAddr.sun_family = AF_UNIX;
strcpy (m_SockAddr.sun_path, address);
Bind ();
Listen (1);
}
void ServerSocket :: Bind ()
{
int s_bind = bind (m_Socket, (struct sockaddr *) &m_SockAddr, sizeof (m_SockAddr));
if (s_bind == -1)
{
throw SocketException (SocketException :: ESE_SOCKBIND);
}
}
void ServerSocket :: Listen (const int backlog)
{
int s_listen = listen (m_Socket, backlog);
if (s_listen == -1)
{
throw SocketException (SocketException :: ESE_SOCKLISTEN);
}
}
BaseSocket * ServerSocket :: Accept ()
{
int sock_fd = accept (m_Socket, NULL, NULL);
if (sock_fd == -1)
{
throw SocketException(SocketException::ESE_SOCKACCEPT);
}
BaseSocket * p_sock = new BaseSocket (sock_fd);
return p_sock;
}
ServerSocket :: ~ ServerSocket ()
{
// unlink with socket
shutdown (m_Socket, 2);
close (m_Socket);
}
// ------------------------------ ClientSocket methods ------------------------------
ClientSocket :: ClientSocket (const char * address)
{
int sock_fd = socket (AF_UNIX, SOCK_STREAM, 0);
if (sock_fd == -1)
{
throw SocketException (SocketException :: ESE_SOCKCREATE);
}
m_Socket = sock_fd;
m_SockAddr.sun_family = AF_UNIX;
strcpy (m_SockAddr.sun_path, address);
Connect ();
}
void ClientSocket :: Connect ()
{
int s_connect = connect (m_Socket, (struct sockaddr *) &m_SockAddr, sizeof (m_SockAddr));
if (s_connect == -1)
{
throw SocketException (SocketException :: ESE_SOCKCONN);
}
}
ClientSocket :: ~ ClientSocket ()
{
// unlink with socket
shutdown (m_Socket, 2);
close (m_Socket);
}
}; // the end of namespace ModelSQL
#endif