-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgsock.cpp
206 lines (181 loc) · 4.37 KB
/
gsock.cpp
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
#include "gsock.h"
#include <cstdio>
#include <cstring>
#include <exception>
using namespace std;
namespace _internal_on_start
{
class _init_winsock2_2_class
{
public:
_init_winsock2_2_class()
{
/// Windows Platform need WinSock2.DLL initialization.
#if (defined _WINSOCK2_H || defined _WINSOCK_H)
WORD wd;
WSAData wdt;
wd=MAKEWORD(2,2);
int ret=WSAStartup(wd,&wdt);
if(ret<0)
{
printf("Unable to load winsock2.dll. Terminate Called.\n");
std::terminate();
}
#endif
}
~_init_winsock2_2_class()
{
/// Windows Platform need WinSock2.DLL clean up.
#if (defined _WINSOCK2_H || defined _WINSOCK_H)
WSACleanup();
#endif
}
}_init_winsock2_2_obj;
}/// End of namespace _internal_on_start
serversock::serversock()
{
sfd = socket(AF_INET, SOCK_STREAM, 0);
}
serversock::~serversock()
{
closesocket(sfd);
}
int serversock::bind(int Port)
{
memset(&saddr, 0, sizeof(saddr));
saddr.sin_addr.s_addr = INADDR_ANY;
saddr.sin_port = htons(Port);
saddr.sin_family = AF_INET;
return ::bind(sfd, (sockaddr*)&saddr, sizeof(saddr));
}
int serversock::set_reuse()
{
int opt = 1;
return setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt));
}
int serversock::listen(int MaxCount)
{
return ::listen(sfd, MaxCount);
}
sock serversock::accept()
{
sock s;
/// Linux: use socklen_t instead of int
socklen_t tmp = sizeof(s.saddr);
int ret = ::accept(sfd, (sockaddr*)&s.saddr, &tmp);
if (ret<0)
{
s.sfd = -1;/// Bad Socket
}
else
{
s.sfd = ret;
}
return s;
}
sock::sock()
{
sfd = socket(AF_INET, SOCK_STREAM, 0);
}
sock::~sock()
{
if(sfd>=0)
{
closesocket(sfd);
}
}
sock::sock(sock && tmp)
{
sfd = tmp.sfd;
saddr = tmp.saddr;
tmp.sfd = -1;
}
int sock::connect(const char * IPStr, int Port)
{
memset(&saddr, 0, sizeof(saddr));
saddr.sin_addr.s_addr = inet_addr(IPStr);
saddr.sin_port = htons(Port);
saddr.sin_family = AF_INET;
return ::connect(sfd, (sockaddr*)&saddr, sizeof(saddr));
}
int sock::send(const char * Buffer, int BufferLen)
{
return ::send(sfd, (const char*)Buffer, BufferLen, 0);
}
int sock::recv(char * Buffer, int BufferMax)
{
return ::recv(sfd, (char*)Buffer, BufferMax, 0);
}
/**[20161102] Now send/recv has time-out-control. */
int sock::setsendtime(int Second)
{
struct timeval outtime;
/// We don't know why, but on Windows, 1 Second means 1000.
#if (defined _WINSOCK2_H || defined _WINSOCK_H)
outtime.tv_sec = Second * 1000;
outtime.tv_usec = 0;
#else
outtime.tv_sec = Second;
outtime.tv_usec = 0;
#endif
return setsockopt(sfd, SOL_SOCKET, SO_SNDTIMEO, (const char*)&outtime, sizeof(outtime));
}
int sock::setrecvtime(int Second)
{
struct timeval outtime;
/// We don't know why, but on Windows, 1 Second means 1000.
#if (defined _WINSOCK2_H || defined _WINSOCK_H)
outtime.tv_sec = Second * 1000;
outtime.tv_usec = 0;
#else
outtime.tv_sec = Second;
outtime.tv_usec = 0;
#endif
return setsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&outtime, sizeof(outtime));
}
int sock::getsendtime(int & _out_Second, int & _out_uSecond)
{
struct timeval outtime;
/// Linux: use socklen_t instead of int
socklen_t _not_used_t;
int ret = getsockopt(sfd, SOL_SOCKET, SO_SNDTIMEO, (char*)&outtime, &_not_used_t);
if (ret<0) return ret;
/// We don't know why, but on Windows, 1 Second means 1000.
#if (defined _WINSOCK2_H || defined _WINSOCK_H)
_out_Second = outtime.tv_sec / 1000;
_out_uSecond = outtime.tv_usec;
#else
_out_Second = outtime.tv_sec;
_out_uSecond = outtime.tv_usec;
#endif
return ret;
}
int sock::getrecvtime(int & _out_Second, int & _out_uSecond)
{
struct timeval outtime;
/// Linux: use socklen_t instead of int
socklen_t _not_used_t;
int ret = getsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO, (char*)&outtime, &_not_used_t);
if (ret<0) return ret;
/// We don't know why, but on Windows, 1 Second means 1000.
#if (defined _WINSOCK2_H || defined _WINSOCK_H)
_out_Second = outtime.tv_sec / 1000;
_out_uSecond = outtime.tv_usec;
#else
_out_Second = outtime.tv_sec;
_out_uSecond = outtime.tv_usec;
#endif
return ret;
}
sockaddr_in sock::getaddr()
{
return saddr;
}
sock& sock::operator = (sock&& tmp)
{
closesocket(sfd);
sfd = tmp.sfd;
saddr = tmp.saddr;
tmp.sfd = -1;
return *this;
}