-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.c
executable file
·215 lines (195 loc) · 4.34 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
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
215
#include <string.h>
#include <stdio.h>
#include "enet/include/enet/enet.h"
#include "joynet.h"
#include "serialize.h"
#include "message.h"
#include "chat.h"
#include "game_message.h"
/* client functions */
JOYNET_CLIENT * joynet_create_client(void)
{
JOYNET_CLIENT * cp;
int i;
cp = malloc(sizeof(JOYNET_CLIENT));
if(!cp)
{
return NULL;
}
cp->serial_data = joynet_create_serial_data();
if(!cp->serial_data)
{
return NULL;
}
cp->host = NULL;
cp->peer = NULL;
strcpy(cp->screen_name, "");
cp->user = -1; // -1 means not connected
cp->group = 0;
cp->master = 0;
cp->playing = 0;
cp->spectating = 0;
cp->global_callback = NULL;
for(i = 0; i < JOYNET_MAX_CHANNELS; i++)
{
cp->channel_callback[i] = NULL;
}
return cp;
}
void joynet_destroy_client(JOYNET_CLIENT * cp)
{
joynet_destroy_serial_data(cp->serial_data);
free(cp);
}
int joynet_connect_client_to_server(JOYNET_CLIENT * cp, char * host, int port)
{
ENetEvent event;
if(enet_address_set_host(&cp->address, host) < 0)
{
return 0;
}
cp->address.port = port;
cp->host = enet_host_create(NULL, 1, 0, 0, 0);
if(!cp->host)
{
return 0;
}
cp->peer = enet_host_connect(cp->host, &cp->address, 4, 0);
if(!cp->peer)
{
return 0;
}
if(enet_host_service(cp->host, &event, 5000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT)
{
return 1;
}
else
{
enet_peer_reset(cp->peer);
}
return 0;
}
void joynet_disconnect_client_from_server(JOYNET_CLIENT * cp)
{
if(cp->peer)
{
enet_peer_disconnect(cp->peer, JOYNET_DISCONNECT_CLIENT_CLOSED);
enet_host_flush(cp->host);
enet_host_destroy(cp->host);
}
}
void joynet_set_client_group(JOYNET_CLIENT * cp, short group)
{
char data[2];
ENetPacket * pp;
if(cp->peer)
{
joynet_serialize(cp->serial_data, data);
joynet_putw(cp->serial_data, group);
pp = joynet_create_packet(JOYNET_CHAT_MESSAGE_SET_GROUP, cp->serial_data);
enet_peer_send(cp->peer, JOYNET_CHANNEL_CHAT, pp);
}
}
void joynet_set_client_screen_name(JOYNET_CLIENT * cp, char * screen_name)
{
if(cp->peer)
{
ENetPacket * pp = joynet_build_packet(JOYNET_CHAT_MESSAGE_SET_NAME, screen_name, strlen(screen_name) + 1);
enet_peer_send(cp->peer, JOYNET_CHANNEL_CHAT, pp);
}
}
void joynet_poll_client(JOYNET_CLIENT * cp)
{
ENetEvent event;
if(cp && cp->host && cp->peer)
{
while(enet_host_service(cp->host, &event, 0))
{
switch(event.type)
{
/* data has been received */
case ENET_EVENT_TYPE_RECEIVE:
{
JOYNET_MESSAGE message;
joynet_decode_message(&message, &event);
switch(event.channelID)
{
case JOYNET_CHANNEL_SYSTEM:
{
break;
}
case JOYNET_CHANNEL_CHAT:
{
joynet_handle_client_chat_message(cp, &message);
break;
}
case JOYNET_CHANNEL_GAME:
{
joynet_handle_client_game_message(cp, &message);
break;
}
}
if(cp->channel_callback[event.channelID])
{
cp->channel_callback[event.channelID](&message);
}
break;
}
case ENET_EVENT_TYPE_DISCONNECT:
{
break;
}
default:
{
break;
}
}
if(cp->global_callback)
{
cp->global_callback(&event);
}
}
}
}
void joynet_send_client_chat(JOYNET_CLIENT * cp, char * message, int group)
{
if(cp->peer)
{
ENetPacket * packet;
if(group)
{
packet = joynet_build_packet(JOYNET_CHAT_MESSAGE_GROUP_TALK, message, strlen(message) + 1);
}
else
{
packet = joynet_build_packet(JOYNET_CHAT_MESSAGE_TALK, message, strlen(message) + 1);
}
enet_peer_send(cp->peer, JOYNET_CHANNEL_CHAT, packet);
enet_host_flush(cp->host);
}
}
void joynet_set_client_global_callback(JOYNET_CLIENT * cp, int(*callback)(ENetEvent * ep))
{
cp->global_callback = callback;
}
void joynet_set_client_channel_callback(JOYNET_CLIENT * cp, int channel, int(*callback)(JOYNET_MESSAGE * mp))
{
cp->channel_callback[channel] = callback;
}
void joynet_set_client_chat_callback(JOYNET_CLIENT * cp, int(*callback)(char * user, char * message))
{
cp->chat_callback = callback;
}
void joynet_set_client_voip_callback(JOYNET_CLIENT * cp, int(*callback)(int client, void * data, int size))
{
cp->voip_callback = callback;
}
void joynet_send_client_voip_data(JOYNET_CLIENT * cp, void * data, int size)
{
ENetPacket * pp;
if(cp->peer)
{
pp = joynet_build_packet(JOYNET_CHAT_MESSAGE_VOIP, data, size);
enet_peer_send(cp->peer, JOYNET_CHANNEL_CHAT, pp);
}
}