1 #include <unistd.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <time.h>
6 #include <sys/time.h>
7 
8 
9 #include "globals.h"
10 #include "sizes.h"
11 #include "status.h"
12 #include "netdata.h"
13 #include "netvariables.h"
14 #include "key.h"
15 #include "menu.h"
16 #include "init.h"
17 
18 
19 struct sockaddr_in servaddr;
20 int GameType;
21 float WorldWidth;
22 int myShip;
23 int svrsock;
24 
25 
26 /*
27   Function to connect to the game server.
28   This function uses the Server IP and Port to connect using UDP
29 */
ConnectServer()30 int ConnectServer()
31 {
32   char senddata;
33   int n;
34   struct timeval timeout;
35   struct GameParametersStruct GameParameters;
36   int i, rc;
37 
38   InitKeyValues();
39 
40   if ((svrsock=socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
41     printf("Socket Error\n");
42     exit(1);
43   }
44 
45   // Set a timeout of 5 seconds for listening
46   timeout.tv_sec=5;
47   timeout.tv_usec=0;
48   setsockopt(svrsock, SOL_SOCKET, SO_RCVTIMEO,
49              &timeout, sizeof(struct timeval));
50 
51   memset(&servaddr, 0, sizeof(servaddr));
52 
53   servaddr.sin_family=AF_INET;
54   servaddr.sin_port=htons(ServerPort);
55 
56 #ifdef CYGWIN
57   servaddr.sin_addr.s_addr=inet_addr(ServerIP);
58 #else
59   if (inet_pton(AF_INET, ServerIP, &servaddr.sin_addr) <=0) {
60     printf("inet_pton error for %s\n", ServerIP);
61     exit(2);
62   }
63 #endif
64 
65   /*
66     Send out a test packet.  For some reason on some platforms the ServerIP
67     value for the first packet is incorrect on the server.  So a test packet is
68     sent first.  This seems to correct the problem since every packet after
69     this works fine.
70   */
71   senddata=TEST_PACKET;
72   sendto(svrsock, &senddata, 1, 0, (struct sockaddr *)&servaddr,
73          sizeof(servaddr));
74 
75   // Try to connect to the server and receive the game parameters
76   for (i=0; i<3; i++) {
77     if (i!=0) {
78       printf("Error connecting to server, retrying\n");
79     }
80 
81     if (sendto(svrsock, &PlayerName, MAX_PLAYERNAME_LENGTH, 0,
82                (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
83       printf("Write Error in Connect Server\n");
84       exit(1);
85     }
86     if ((n=recvfrom(svrsock, (char *)&GameParameters,
87                     sizeof(GameParametersStruct), 0, NULL, NULL)) ==
88         sizeof(GameParametersStruct)) {
89       break;
90     }
91   }
92 
93   if (n!=sizeof(GameParametersStruct)) {
94     printf("Failed to connect to server\n");
95     AbortGame();
96     return(1);
97   }
98 
99   // Set game parameters read from the connection
100   WorldWidth=GameParameters.WorldWidth;
101   myShip=ntohs(GameParameters.PlayerNum);
102   MaxShield=ntohl(GameParameters.MaxShield);
103   MaxArmor=ntohl(GameParameters.MaxArmor);
104   GameType=ntohs(GameParameters.GameType);
105 
106   return(0);
107 }
108 
109 
110 // Function to tell the server the key presses and releases
TellServer(char keypress,int on)111 void TellServer(char keypress, int on)
112 {
113   char sendvalue;
114 
115   sendvalue=keypress;
116 
117   if (on) {
118     sendvalue+=0x10;
119   }
120   if (sendto(svrsock, &sendvalue, 1, 0, (struct sockaddr *)&servaddr,
121                 sizeof(servaddr)) < 0) {
122     printf("Write Error in TellServer\n");
123     exit(1);
124   }
125 }
126 
127 
128 // Function to send status of all keys to server
SendUpdate()129 void SendUpdate()
130 {
131   if (sendto(svrsock, &KeyValues, sizeof(KeyValuesStruct), 0,
132              (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
133     printf("Write Error in SendUpdate\n");
134     exit(1);
135   }
136 }
137