-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.c
106 lines (92 loc) · 2.41 KB
/
client.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include "client.h"
#include "decoder.h"
int client_getsocket(Client *client)
{
return client->socket;
}
int client_waitingtosend(Client *client)
{
return client->sbufsize > 0;
}
int client_new(Client *client, int socket)
{
client->socket = socket;
if ((client->rbuf = malloc(recvbufsize * sizeof(unsigned char))) == NULL) {
perror("malloc");
return -1;
}
client->rbufsize = 0;
client->rpos = 0;
if ((client->sbuf = malloc(sendbufsize * sizeof(unsigned char))) == NULL) {
perror("malloc");
free(client->rbuf);
return -1;
}
client->sbufsize = 0;
if (client_decoder_init(client) < 0) {
free(client->rbuf);
free(client->sbuf);
return -1;
}
return 0;
}
int client_recv(Client *client)
{
if (client->rbufsize == recvbufsize) { // FIXME FIXME FIXME
fprintf(stderr, "WARNING: Buffer full, dump buffer, "
"posibility of data lost.\n");
client->rbufsize = 0;
client->rpos = 0;
client_decoder_clear(client);
}
ssize_t received = recv(client->socket, client->rbuf + client->rbufsize,
recvbufsize - client->rbufsize, 0);
if (received <= 0) {
return -1;
}
client->rbufsize += received;
return 0;
}
void client_cleartrame(Client * client)
{
memmove(client->rbuf, client->rbuf + client->rpos,
client->rbufsize - client->rpos);
client->rbufsize -= client->rpos;
client->rpos = 0;
}
void client_sendfrom(Client *dst, Client *src)
{
if (sendbufsize-dst->sbufsize < src->rpos) {
fprintf(stderr, "WARNING: Send buffer full, trame ignored !\n");
} else {
memcpy(dst->sbuf + dst->sbufsize,
src->rbuf, src->rpos);
dst->sbufsize += src->rpos;
}
}
int client_send(Client *client)
{
ssize_t sended = send(client->socket, client->sbuf, client->sbufsize, 0);
if (sended < 0) {
return -1;
}
memmove(client->sbuf, client->sbuf + sended, client->sbufsize - sended);
client->sbufsize -= sended;
return 0;
}
void client_delete(Client *client)
{
client_decoder_destroy(client);
close(client->socket);
client->socket = 0;
free(client->rbuf);
client->rbuf = NULL;
client->rbufsize = 0;
free(client->sbuf);
client->sbuf = NULL;
client->sbufsize = 0;
}