-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_client.hpp
62 lines (50 loc) · 1.19 KB
/
tcp_client.hpp
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
#ifndef VI_TCP_CLIENT
#define VI_TCP_CLIENT
#include "tcp_socket.hpp"
namespace vi
{
class tcp_client
{
public:
enum client_result
{
ok,
try_again,
error
};
private:
tcp_socket mSocket;
public:
tcp_socket& connection()
{
return mSocket;
}
template<class T>
tcp_client::client_result send(const T& packet)
{
int result = ::send(mSocket.mSocket, &packet, sizeof(packet), 0);
if (result > 0)
{
return tcp_client::ok;
}
else
{
return (EWOULDBLOCK == errno) ? tcp_client::try_again : tcp_client::error;
}
}
template<class T>
tcp_client::client_result receive(T& packet)
{
int result = ::recv(mSocket.mSocket, &packet, sizeof(packet));
if (result > 0)
{
return tcp_client::ok;
}
else
{
return (EWOULDBLOCK == errno) ? tcp_client::try_again : tcp_client::error;
}
}
};
};
#endif