-
Notifications
You must be signed in to change notification settings - Fork 1
/
BaseClient.cpp
214 lines (158 loc) · 4.1 KB
/
BaseClient.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
207
208
209
210
211
212
213
214
#include "BaseClient.h"
#include <errno.h>
BaseClient::BaseClient(const char * ip, int port, char msp):message_separator(msp) {
struct sockaddr_in addr;
if((netinfo.descriptor = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
throw CannotCreateSocketError();
}
addr.sin_family = AF_INET;
addr.sin_port = server.port = htons(port);
if((server.ip = inet_aton(ip, &(addr.sin_addr))) == 0) {
throw BadIPError();
}
if (connect(netinfo.descriptor, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
throw CannotEstablishConnectionError();
}
buffer.size = 0;
buffer.allocated = buffer_t::DEFAULT_BUFFER;
buffer.data = (char *) malloc(buffer.allocated * sizeof(char));
message_queue.count = 0;
message_queue.lo = message_queue.hi = 0;
}
BaseClient::~BaseClient() {
shutdown(netinfo.descriptor, SHUT_RDWR);
close(netinfo.descriptor);
if(buffer.data){
free(buffer.data);
}
}
void BaseClient::send(const char * data, int length) {
int cnt = 0;
int res;
while(cnt < length){
res = write(netinfo.descriptor, data+cnt, length-cnt);
if(res == -1){
throw NetworkErrorErrno(errno);
}
cnt += res;
}
}
void BaseClient::send(const char * data){
int length = strlen(data);
send(data, length);
}
/* BaseClient:buffer_t methods */
void BaseClient::buffer_t::push(const char * given_data, int length) {
int new_size = size + length;
if(new_size > MAXIMUM_BUFFER) {
throw BufferOverflowError();
}
if(new_size > allocated) {
allocated = new_size;
data = (char *) realloc(data, new_size * sizeof(char));
}
memcpy(data + size, given_data, length * sizeof(char));
size = new_size;
}
/* BaseClient::message_queue_t methods */
BaseClient::message_queue_t::message_queue_t():lo(0),hi(0),count(0) {};
BaseClient::message_queue_t::~message_queue_t() {
message_list * tmp = lo;
while(tmp != hi) {
tmp = lo->next;
free(lo);
lo = tmp;
}
if(hi != 0){
free(hi);
}
}
bool BaseClient::message_queue_t::isEmpty() const { return count == 0; }
int BaseClient::message_queue_t::getLength() const { return count; }
void BaseClient::message_queue_t::push (const char * data, int length) {
message_list * tmp;
tmp = (message_list *) malloc (sizeof(*tmp));
tmp->prev = hi;
tmp->next = 0;
tmp->message = (char *) malloc( (length+1) * sizeof(char) );
memcpy(tmp->message, data, length * sizeof(char));
tmp->message[length] = '\0';
tmp->length = length + 1;
if(hi != 0){
hi->next = tmp;
hi = tmp;
} else {
lo = hi = tmp;
}
count++;
}
char * BaseClient::message_queue_t::pop() {
struct message_list *tmp;
char * res;
if(lo == NULL){
return NULL;
}
tmp = lo->next;
res = lo->message;
free(lo);
lo = tmp;
if(!tmp){
hi = 0;
}
count--;
return res;
}
/* BaseClient methods */
int BaseClient::split_messages(char separator) {
buffer_t tmp_b;
char * pos;
int cnt = 0;
if(buffer.size == 0){
return 0;
}
tmp_b = buffer;
while( tmp_b.size > 0 && ((pos = (char *) memchr(tmp_b.data, separator, tmp_b.size)) != NULL) ){
int delta = pos - tmp_b.data + 1;
// extract message from the buffer;
message_queue.push(tmp_b.data, delta);
tmp_b.data += delta;
tmp_b.size -= delta;
tmp_b.allocated -= delta;
++cnt;
}
// reallocate memory
if(tmp_b.allocated < buffer_t::DEFAULT_BUFFER ){
tmp_b.allocated = buffer_t::DEFAULT_BUFFER;
}
char * rest = (char *) malloc(tmp_b.allocated * sizeof(char));
memcpy(rest, tmp_b.data, tmp_b.size);
tmp_b.data = rest;
free(buffer.data);
buffer = tmp_b;
return cnt;
}
int BaseClient::check(struct timeval timeout) {
fd_set dsc;
char * buff;
const int BUFF_LEN = 64;
int length;
FD_ZERO(&dsc);
FD_SET(netinfo.descriptor, &dsc);
select(netinfo.descriptor+1, &dsc, NULL, NULL, &timeout);
if(!FD_ISSET(netinfo.descriptor, &dsc)){ // incomming from the server
return -1;
}
buff = (char *) malloc(BUFF_LEN * sizeof(char));
length = read(netinfo.descriptor, buff, BUFF_LEN);
if(length == 0){
throw DisconnectedError();
} else
if(length == -1){
throw NetworkErrorErrno(errno);
}
buffer.push(buff, length);
return split_messages(message_separator);
}
int BaseClient::getDescriptor(){
return netinfo.descriptor;
}