source: Daodan/src/flatline/Flatline_Client.c@ 984

Last change on this file since 984 was 881, checked in by alloc, 11 years ago

Daodan: Some flatline cleanups

File size: 7.0 KB
Line 
1#include "Flatline.h"
2#include "Flatline_Client.h"
3#include "Flatline_Packet.h"
4#include "Mariusnet_Public.h"
5#include "../Daodan_Patch.h"
6//#include "PortForwardWrapper.h"
7
8int client_slot = 0;
9RGBA green = {0, 0xFF, 0, 0};
10RGBA red = {0, 0, 0xFF, 0};
11RGBA grey = {0x80,0x80,0x80,0x80};
12
13
14int FLcEventHandler( int eventIndex, int args[] )
15{
16 switch(eventIndex)
17 {
18 case(EV_DISCONNECT):
19 FLrPlayerDisconnect( args[0] );
20 break;
21 case(EV_KILLED):
22 ONrCharacter_SetHitPoints( PlayerList[args[0]]->Chr, 0);
23 case(EV_DOOR_OPEN):
24 OBJrDoor_ForceOpen( args[0] );
25 break;
26 case(EV_CONSOLE_USE):
27 OBJrConsole_OnActivate( OBJrConsole_GetByID(args[0]), PlayerList[args[1]]->Chr );
28 break;
29 case(EV_RESPAWN):
30 ONrCorpse_Create(PlayerList[args[0]]->Chr);
31 break;
32 default:
33 break;
34 }
35 return 0;
36}
37
38void FLcConnect( int ip, short port )
39{
40 if( NetPlatform_Initalize()) {
41 static flatline_packet packet;
42 memset(&client_address, 0, sizeof(sockaddr_in));
43 sock = NetUDPSocket_Create(ntohs(port), &client_address);
44 address.sin_family = AF_INET; address.sin_port = port; address.sin_addr.S_un.S_addr = ip;
45 //address.sin_family = AF_INET; address.sin_port = htons(27777); address.sin_addr.S_un.S_addr = inet_addr("192.168.0.1");
46
47 packet.id = CONNECT_SEND;
48 memcpy(((connect_send*)(packet.data))->country , "XD", 2);
49 memcpy(((connect_send*)(packet.data))->name, player_name, 256);
50 DDrConsole_PrintF("%s", ((connect_send*)(packet.data))->name);
51 CreateThread(NULL, 0, StartClient, &packet, 0, 0);
52
53 }
54}
55
56bool FLrClient_Run(flatline_packet* packet)
57{
58
59 char data[1400];
60 uint16_t len;
61 int j;
62 int sent_bytes;
63
64/* PortMappingContainer_C PMC =
65 {
66 "",
67 "27777",
68 "27777",
69 "UDP",
70 "",
71 "",
72 "Flatline!"
73 };
74
75 uPnP_Remove( &PMC );*/
76 client_connected = 0;
77
78
79 //starts the connection
80 DDrConsole_PrintF("Connecting to server %s on socket %i", inet_ntoa(address.sin_addr), client_sock);
81 sent_bytes = NetUDPSocket_Send(client_sock, (sockaddr*)&address, (char*)packet, FLATLINE_HEADER + sizeof(connect_send) );
82 if(sent_bytes == SOCKET_ERROR) {
83 NetCatchError();
84 }
85 //loops once per second waiting for a reply.
86 for(j = 0; j < CONNECTION_TIMEOUT; j++) {
87 while(NetUDPSocket_Recieve(client_sock, (sockaddr_storage *) &client_address, data, &len)){
88 packet = (flatline_packet*)data;
89 if(packet->id == CONNECT_REPLY) {
90 if(packet->connect_reply.goodtogo){
91
92 client_connected = 1;
93
94 client_slot = ((connect_reply*)packet->data)->player_slot;
95
96 PlayerList[client_slot] = Players+client_slot;
97 PlayerList[client_slot]->Chr = ONgGameState->PlayerCharacter;
98
99 DDrConsole_PrintColored("Connection successful!",0,green, grey);
100
101 /*THIS MIGHT BE BAD*/ snprintf( PlayerList[client_slot]->name, 32, "%s", player_name );
102
103 //disable local input.
104 DDrPatch_NOOP(0x004FA929, 5 + 6 + 5);
105
106 //Disable local turning
107 //DDrPatch_NOOP(0x004F7EA8, 2);
108 //DDrPatch_Byte( 0x004F7EB1 , 0xE9);
109 //DDrPatch_MakeJump( 0x004F7EB1, 0x004F8030 );
110
111
112 //DDrPatch_Byte(0x04ED6FB, 0xEB);
113
114 //DDrConsole_PrintF("Slot %i", ((connect_reply*)packet)->player_slot);
115 //DDrPatch_NOOP(0x43B23,0x10);
116 //DDrPatch_NOOP(0x4EC248,(0x5A-0x48));
117 //DDrPatch_NOOP(0x4EC861, 6);
118
119 //Close Mariusnet collection because it hurts latency
120 MSNet_RoomExit();
121 break;
122 }
123 else {
124 DDrConsole_PrintF("Connection rejected: %s", ((connect_reply*)packet->data)->message);
125 return false;
126 break;
127 }
128 }
129 }
130 if(client_connected) break;
131 DDrConsole_PrintF("Connection timing out in %i seconds...", CONNECTION_TIMEOUT - j);
132 Sleep(1000);
133 }
134 //the client timed out without recieving an error message.
135 if(!client_connected) {
136 DDrConsole_PrintColored("Connection timed out.",0,red, grey);
137 return false;
138 }
139
140 return true;
141}
142
143
144void FLrClient_GetPackets()
145{
146 flatline_packet packet;
147 uint16_t len;
148 //#define SPAM_INPUT
149#ifdef SPAM_INPUT
150 struct timeval lasttime;
151 struct timeval thistime;
152 gettimeofday(&lasttime, 0);
153#endif
154
155
156 while(NetUDPSocket_Recieve(client_sock, (sockaddr_storage *) &client_address, &packet, &len)) {
157 //packet = (flatline_packet*)data;
158 //DDrConsole_PrintF("Data recieved, length %i, type %i", len, ((flatline_packet*)data)->id);
159 switch(packet.id) {
160 case MESSAGE:
161 COrMessage_Print(packet.data, "chat", 0);
162 break;
163 case CHANGE_NAME:
164 if(PlayerList[(char)packet.data[0]])
165 {
166 char message_buffer[1024];
167 sprintf(message_buffer,"%s changed their name to %s", PlayerList[(char)packet.data[0]]->name, packet.data + 1);
168 COrMessage_Print(message_buffer, "name_change", 0);
169
170 /*THIS MIGHT BE BAD*/ snprintf(PlayerList[packet.data[0]]->name, 32, "%s", packet.data + 1);
171
172 }
173 break;
174 case CONNECT_SEND:
175 ;if(1) {
176 flatline_packet connect_recv;
177 memcpy(&connect_recv.connect_reply.message,"This isn't a server!", sizeof("This isn't a server!"));
178 NetUDPSocket_Send(client_sock, (sockaddr *) &address, (char*)&connect_recv, sizeof(bool) + FLATLINE_HEADER + sizeof("This isn't a server!"));
179 }
180 case CONNECT_REPLY:
181 break; //extra packet or something.
182 case NEW_PLAYER:
183 ;if(1) { //haxhaxhax
184 CharacterObject* Char = &(packet.new_player.Character);
185 uint32_t chr_index = 0;
186 Character* PC;
187 DDrConsole_PrintF("%i | %i", packet.new_player.Playernumber ,client_slot);
188 //Char->OSD.Options = 0;
189 if(packet.new_player.Playernumber == client_slot) {
190 PlayerList[packet.new_player.Playernumber] = &Players[0];
191 PC = (ONgGameState->PlayerCharacter);
192 Players[0].Chr = PC;
193
194 }
195 else {
196 ONrGameState_NewCharacter(Char, NULL, NULL, &chr_index);
197 ONgGameState->CharacterStorage[chr_index].charType = 0;
198 PlayerList[packet.new_player.Playernumber] = &Players[chr_index];
199 Players[chr_index].Chr = &(ONgGameState->CharacterStorage[chr_index]);
200 Players[chr_index].Chr->Flags &= 0xFFBFFFFF;
201 Players[chr_index].spawnnumber = chr_index;
202 DDrConsole_PrintF("Spawning player %s, class %s, slot %i", ((new_player*)(packet.data))->Character.OSD.Name, ((new_player*)(packet.data))->Character.OSD.Class,chr_index) ;
203 /*THIS MIGHT BE BAD*/ snprintf(Players[chr_index].name, 32, "%s", ((new_player*)(packet.data))->Character.OSD.Name);
204 }
205 //Players[((new_player*)(packet.data))->Playernumber].spawnnumber = ONrGameState_NewCharacter(&(((new_player*)(packet.data))->Character), NULL, NULL, 0);
206 break;
207 }
208
209 case FLATLINE_EVENT:
210 FLcEventHandler( packet.flatline_event.event_index, packet.flatline_event.intArray );
211 break;
212 case PK_PING:
213 packet.id = PK_PONG;
214 NetUDPSocket_Send(client_sock, (sockaddr *) &address, (char*)&packet, FLATLINE_HEADER + 4);
215 break;
216 case PK_PLAYER_DATA:
217 FLcReadPlayerData( &packet, len );
218 break;
219 default:
220 DDrConsole_PrintF("Warning, recieved badly formed packet!");
221 break;
222 }
223 }
224}
225
Note: See TracBrowser for help on using the repository browser.