source: Daodan/MSVC/Flatline_Client.c@ 800

Last change on this file since 800 was 589, checked in by gumby, 13 years ago

stuff

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