-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsession.cpp
More file actions
171 lines (153 loc) · 4.08 KB
/
session.cpp
File metadata and controls
171 lines (153 loc) · 4.08 KB
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
#include "session.h"
#include <cstdio>
#include "macro_def.h"
#include "bubble_def.h"
#include "utils.h"
Session::Session(std::string server_ip, int server_port) : mSocket(mIOService)
{
try
{
std::printf("[INFO] Connecting to %s:%d\n", server_ip.c_str(), server_port);
asio::ip::tcp::endpoint endpoint(asio::ip::address::from_string(server_ip), server_port);
mSocket.connect(endpoint);
std::printf("[INFO] Connected to %s:%d\n", server_ip.c_str(), server_port);
}
catch (std::exception& e)
{
LOG_ERR(e.what());
}
}
Session::~Session()
{
mSocket.close();
}
int Session::send(const char *data, size_t size)
{
int nbytes = -1;
try
{
#ifdef DEBUG
printf("[INFO] Sending %lu bytes\n", size);
#endif
nbytes = asio::write(mSocket, asio::buffer((void*) data, size));
#ifdef DEBUG
std::printf("[INFO] Sent %d bytes\n", nbytes);
LOG_BUFFER("[DEBUG]Sent:", nbytes, data);
LOG_BUFFER_HEX("---", nbytes, data);
#endif
}
catch (std::exception& e)
{
LOG_ERR(e.what());
}
return nbytes;
}
char* Session::receive_packet()
{
char buffer[PACKHEAD_SIZE];
int nbytes = receive_til_full(buffer, PACKHEAD_SIZE);
if (nbytes <= 0 || (size_t)nbytes < PACKHEAD_SIZE || (unsigned char)buffer[0] != PACKHEAD_MAGIC)
{
LOG_ERR("Packet header is corrupted");
return NULL;
}
PackHead* packhead = (PackHead*) buffer;
uint32_t uiPackLength = ntohl(packhead->uiLength);
size_t packetSize = uiPackLength + STRUCT_MEMBER_POS(PackHead, cPackType);
if (packetSize < PACKHEAD_SIZE)
{
LOG_ERR("Packet length field is corrupted");
return NULL;
}
char *packet = new char[packetSize];
size_t bytes_left = packetSize - PACKHEAD_SIZE;
char *recv_start = packet + PACKHEAD_SIZE;
memcpy(packet, buffer, PACKHEAD_SIZE);
nbytes = receive_til_full(recv_start, bytes_left);
if (nbytes < 0 || (size_t)nbytes != bytes_left)
{
LOG_ERR("Packet data is corrupted");
delete[] packet;
return NULL;
}
return packet;
}
int Session::receive_packet_to_buffer(char *buffer, size_t buffer_size)
{
if (buffer_size < PACKHEAD_SIZE)
{
LOG_ERR("Receive buffer is too small");
return -1;
}
int nbytes = receive_til_full(buffer, PACKHEAD_SIZE);
if (nbytes <= 0 || (size_t)nbytes < PACKHEAD_SIZE || (unsigned char)buffer[0] != PACKHEAD_MAGIC)
{
LOG_ERR("Packet header is corrupted");
return -1;
}
PackHead* packhead = (PackHead*) buffer;
uint32_t uiPackLength = ntohl(packhead->uiLength);
size_t packetSize = uiPackLength + STRUCT_MEMBER_POS(PackHead, cPackType);
if (packetSize < PACKHEAD_SIZE)
{
LOG_ERR("Packet length field is corrupted");
return -1;
}
if (packetSize > buffer_size)
{
LOG_ERR("Receive buffer is too small");
return -1;
}
size_t bytes_left = packetSize - PACKHEAD_SIZE;
char *recv_start = buffer + PACKHEAD_SIZE;
nbytes = receive_til_full(recv_start, bytes_left);
if (nbytes < 0 || (size_t)nbytes != bytes_left)
{
LOG_ERR("Packet data is corrupted");
return -1;
}
return 0;
}
int Session::receive_til_full(char *buffer, size_t size)
{
int nbytes = -1;
try
{
#ifdef DEBUG
printf("[INFO] Receiving %lu bytes\n", size);
#endif
nbytes = asio::read(mSocket, asio::buffer(buffer, size));
#ifdef DEBUG
printf("[INFO] Received %d bytes\n", nbytes);
LOG_BUFFER("[DEBUG] Received:", nbytes, buffer);
LOG_BUFFER_HEX("---", nbytes, buffer);
#endif
}
catch (std::exception& e)
{
LOG_ERR(e.what());
}
return nbytes;
}
int Session::receive_at_least(char *buffer, size_t size, size_t at_least)
{
int nbytes = -1;
try
{
assert(at_least <= size);
#ifdef DEBUG
printf("[INFO] Receiving at least %lu bytes\n", at_least);
#endif
nbytes = asio::read(mSocket, asio::buffer(buffer, size));
#ifdef DEBUG
printf("[INFO] Received %d bytes\n", nbytes);
LOG_BUFFER("[DEBUG] Received:", nbytes, buffer);
LOG_BUFFER_HEX("---", nbytes, buffer);
#endif
}
catch (std::exception& e)
{
LOG_ERR(e.what());
}
return nbytes;
}