| 170 |
|
message_size = sprintf(message.data + 1, "%s", name); |
| 171 |
|
|
| 172 |
|
UDPServer_SendToAll(&message, message_size + 2 + FLATLINE_HEADER); |
| 173 |
+ |
} |
| 174 |
+ |
void FLsSend_BINACHAR( short j, sockaddr* socket ) |
| 175 |
+ |
{ |
| 176 |
+ |
|
| 177 |
+ |
ActiveCharacter* AC = ONrGetActiveCharacter( PlayerList[j]->Chr); |
| 178 |
+ |
flatline_packet new_char = {0}; |
| 179 |
+ |
CharacterObject* Char = &new_char.new_player.Character; |
| 180 |
+ |
|
| 181 |
+ |
new_char.id = NEW_PLAYER; |
| 182 |
+ |
new_char.new_player.Playernumber = j; |
| 183 |
+ |
|
| 184 |
+ |
// memset(Char, 0, sizeof(CharacterObject)); |
| 185 |
+ |
Char->Header.Type = 'CHAR'; |
| 186 |
+ |
Char->OSD.Options = chr_dontaim; |
| 187 |
+ |
|
| 188 |
+ |
sprintf(Char->OSD.Name,"%s",PlayerList[j]->name); |
| 189 |
+ |
|
| 190 |
+ |
sprintf(Char->OSD.Class, "%s", TMrInstance_GetInstanceName(PlayerList[j]->Chr->ONCC)); |
| 191 |
+ |
|
| 192 |
+ |
if(AC) |
| 193 |
+ |
{ |
| 194 |
+ |
Char->Header.Position = AC->PhyContext->Position; |
| 195 |
+ |
} |
| 196 |
+ |
else |
| 197 |
+ |
{ |
| 198 |
+ |
Char->Header.Position.X = 0; |
| 199 |
+ |
Char->Header.Position.Y = 0; |
| 200 |
+ |
Char->Header.Position.Z = 0; |
| 201 |
+ |
} |
| 202 |
+ |
|
| 203 |
+ |
NetTCPServer_Send(socket, (char*)&new_char, sizeof(new_player) + FLATLINE_HEADER ); |
| 204 |
+ |
} |
| 205 |
+ |
bool FLrServer_PacketCallback(char* data, int datalen, int from) |
| 206 |
+ |
{ |
| 207 |
+ |
int i, j; |
| 208 |
+ |
bool found_player = 0; |
| 209 |
+ |
flatline_packet * packet = (flatline_packet*)data; |
| 210 |
+ |
static int recieved = 0; |
| 211 |
+ |
sockaddr_in sender; |
| 212 |
+ |
sender.sin_family = AF_INET; |
| 213 |
+ |
sender.sin_port = htons(27777); |
| 214 |
+ |
sender.sin_addr = *((struct in_addr*)(int*)&from); |
| 215 |
+ |
|
| 216 |
+ |
|
| 217 |
+ |
//packet->data[datalen] = '\0'; |
| 218 |
+ |
|
| 219 |
+ |
//DDrConsole_PrintF("Packet \r%d recieved from %i", ++recieved, from); |
| 220 |
+ |
|
| 221 |
+ |
|
| 222 |
+ |
|
| 223 |
+ |
//if data[0] != CONNECT_SEND, search in playerlist for ip address |
| 224 |
+ |
|
| 225 |
+ |
|
| 226 |
+ |
|
| 227 |
+ |
|
| 228 |
+ |
switch(packet->id) { |
| 229 |
+ |
flatline_packet connect_recv; |
| 230 |
+ |
player_info * playah; |
| 231 |
+ |
//rewrite this when we get TCP support. |
| 232 |
+ |
//rewrite this before we get TCP support* |
| 233 |
+ |
//the way of seeing if there is room for players sucks. |
| 234 |
+ |
case CONNECT_SEND: |
| 235 |
+ |
; |
| 236 |
+ |
|
| 237 |
+ |
connect_recv.id = CONNECT_REPLY; |
| 238 |
+ |
|
| 239 |
+ |
//if(Players[i].ip == sender.sin_addr.S_un.S_addr) break; //needs to send an error message |
| 240 |
+ |
sender.sin_addr.S_un.S_addr = htonl(sender.sin_addr.S_un.S_addr); |
| 241 |
+ |
playah = FLrServer_AddPlayer(from,packet->connect_send.name, 0, 0); |
| 242 |
+ |
DDrConsole_PrintF("%s connected from %s", packet->connect_send.name, inet_ntoa(sender.sin_addr ) ); |
| 243 |
+ |
if(!((int)playah > -5 && (int)playah <= 0)) { |
| 244 |
+ |
|
| 245 |
+ |
connect_recv.connect_reply.goodtogo = 1; |
| 246 |
+ |
connect_recv.connect_reply.player_slot = playah->list_slot; |
| 247 |
+ |
//DDrConsole_PrintF("Slot: %i", playah->list_slot); |
| 248 |
+ |
|
| 249 |
+ |
//sending this several times to make sure it gets through. Really need to make up some form of packet tracking. |
| 250 |
+ |
NetUDPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(connect_reply) + FLATLINE_HEADER); |
| 251 |
+ |
NetUDPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(connect_reply) + FLATLINE_HEADER); |
| 252 |
+ |
NetUDPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(connect_reply) + FLATLINE_HEADER); |
| 253 |
+ |
NetUDPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(connect_reply) + FLATLINE_HEADER); |
| 254 |
+ |
NetUDPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(connect_reply) + FLATLINE_HEADER); |
| 255 |
+ |
Sleep(100); |
| 256 |
+ |
|
| 257 |
+ |
|
| 258 |
+ |
|
| 259 |
+ |
for(j = 0; j < max_connections; j++) { |
| 260 |
+ |
if(PlayerList[j] != 0) { |
| 261 |
+ |
FLsSend_BINACHAR( j, (sockaddr *)&sender); |
| 262 |
+ |
} |
| 263 |
+ |
|
| 264 |
+ |
} |
| 265 |
+ |
} |
| 266 |
+ |
else { |
| 267 |
+ |
//fix the error messages... |
| 268 |
+ |
DDrConsole_PrintF("Server is full. :("); |
| 269 |
+ |
connect_recv.connect_reply.goodtogo = 0; |
| 270 |
+ |
sender.sin_addr.S_un.S_addr = htonl(sender.sin_addr.S_un.S_addr); |
| 271 |
+ |
memcpy(&connect_recv.connect_reply.message,"Server is full.", sizeof("Server is full.")); |
| 272 |
+ |
NetTCPServer_Send((sockaddr *) &sender, (char*)&connect_recv, sizeof(bool)*2 + FLATLINE_HEADER + sizeof("Server is full.")); |
| 273 |
+ |
|
| 274 |
+ |
} |
| 275 |
+ |
|
| 276 |
+ |
|
| 277 |
+ |
break; |
| 278 |
+ |
case CONNECT_REPLY: |
| 279 |
+ |
break; //do nothing...a server shouldn't recieve this type of packet. |
| 280 |
+ |
case MESSAGE: |
| 281 |
+ |
for(i = 0; i < MAX_PLAYERS; i++) { |
| 282 |
+ |
//DDrConsole_PrintF("%i : %i | %s : %s", from, Players[i].ip, inet_ntoa(*(struct in_addr*)&from), inet_ntoa(*(struct in_addr*)&(Players[i].ip))); |
| 283 |
+ |
if(Players[i].ip == sender.sin_addr.S_un.S_addr) { |
| 284 |
+ |
found_player = 1; |
| 285 |
+ |
break; |
| 286 |
+ |
} |
| 287 |
+ |
} |
| 288 |
+ |
if(found_player == 0) break; |
| 289 |
+ |
else { |
| 290 |
+ |
char message_buffer[512] = {0}; |
| 291 |
+ |
flatline_packet message; |
| 292 |
+ |
int message_size; |
| 293 |
+ |
data[datalen] = 0; |
| 294 |
+ |
|
| 295 |
+ |
DDrConsole_PrintF("%s: %s", Players[i].name, packet->data); |
| 296 |
+ |
sprintf(message_buffer, "%s: %s", Players[i].name, packet->data); |
| 297 |
+ |
|
| 298 |
+ |
message.id = MESSAGE; |
| 299 |
+ |
message_size = sprintf(message.data, "%s", message_buffer); |
| 300 |
+ |
COrMessage_Print(message_buffer, "chat", 0); |
| 301 |
+ |
UDPServer_SendToAll(&message, message_size + 1 + FLATLINE_HEADER); |
| 302 |
+ |
break; |
| 303 |
+ |
} |
| 304 |
+ |
case CHANGE_NAME: |
| 305 |
+ |
for(i = 0; i < MAX_PLAYERS; i++) { |
| 306 |
+ |
if(PlayerList[i] && PlayerList[i]->ip == sender.sin_addr.S_un.S_addr) { |
| 307 |
+ |
found_player = 1; |
| 308 |
+ |
break; |
| 309 |
+ |
} |
| 310 |
+ |
} |
| 311 |
+ |
if(found_player == 0) break; |
| 312 |
+ |
else { |
| 313 |
+ |
bool name_exists = 0; |
| 314 |
+ |
for(j = 0; j < MAX_PLAYERS; j++) { |
| 315 |
+ |
if(PlayerList[j] && !strcmp(packet->data, PlayerList[j]->name)) { |
| 316 |
+ |
name_exists = 1; |
| 317 |
+ |
break; |
| 318 |
+ |
} |
| 319 |
+ |
} |
| 320 |
+ |
if(!name_exists) { |
| 321 |
+ |
FLsUpdateName( i, packet->data ); |
| 322 |
+ |
} |
| 323 |
+ |
break; |
| 324 |
+ |
} |
| 325 |
+ |
case PLAYER_INPUT: |
| 326 |
+ |
|
| 327 |
+ |
for(i = 0; i < max_connections; i++) { |
| 328 |
+ |
if(PlayerList[i] != 0 && PlayerList[i]->ip == sender.sin_addr.S_un.S_addr) { |
| 329 |
+ |
found_player = 1; |
| 330 |
+ |
break; |
| 331 |
+ |
} |
| 332 |
+ |
} |
| 333 |
+ |
|
| 334 |
+ |
if(found_player == 0) break; |
| 335 |
+ |
else { |
| 336 |
+ |
input_struct * packet_input = &packet->input_struct; |
| 337 |
+ |
|
| 338 |
+ |
|
| 339 |
+ |
PlayerList[i]->InputFromClient.Actions1 = packet_input->Actions1; |
| 340 |
+ |
PlayerList[i]->InputFromClient.Actions2 = packet_input->Actions2; |
| 341 |
+ |
PlayerList[i]->InputFromClient.MouseDeltaX = packet_input->MouseDeltaX; |
| 342 |
+ |
PlayerList[i]->InputFromClient.MouseDeltaY = packet_input->MouseDeltaY; |
| 343 |
+ |
PlayerList[i]->FacingFromClient = packet_input->DesiredFacing; |
| 344 |
+ |
//PlayerList[i]->LastInputTime = packet_input->Time; |
| 345 |
+ |
|
| 346 |
+ |
break; |
| 347 |
+ |
} |
| 348 |
+ |
case PK_PONG: |
| 349 |
+ |
for(i = 0; i < max_connections; i++) { |
| 350 |
+ |
if(PlayerList[i] != 0 && PlayerList[i]->ip == sender.sin_addr.S_un.S_addr) { |
| 351 |
+ |
found_player = 1; |
| 352 |
+ |
break; |
| 353 |
+ |
} |
| 354 |
+ |
} |
| 355 |
+ |
|
| 356 |
+ |
if(found_player == 0) break; |
| 357 |
+ |
if(packet->ping != lastPingTime) |
| 358 |
+ |
{ |
| 359 |
+ |
PlayerList[i]->Ping = 999; |
| 360 |
+ |
} |
| 361 |
+ |
else |
| 362 |
+ |
{ |
| 363 |
+ |
PlayerList[i]->Ping = GetTickCount() - packet->ping; |
| 364 |
+ |
} |
| 365 |
+ |
break; |
| 366 |
+ |
case PK_MISSING_PLAYER: |
| 367 |
+ |
if(packet->integer < MAX_PLAYERS) |
| 368 |
+ |
{ |
| 369 |
+ |
FLsSend_BINACHAR( packet->integer, &sender); |
| 370 |
+ |
} |
| 371 |
+ |
break; |
| 372 |
+ |
default: |
| 373 |
+ |
DDrConsole_PrintF("Warning, recieved badly formed packet!"); |
| 374 |
+ |
break; |
| 375 |
+ |
} |
| 376 |
+ |
return true; |
| 377 |
|
} |