PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 13ms RepoModel.GetById 27ms app.codeStats 0ms

/TheElements/jni/server.c

http://thelements.googlecode.com/
C | 179 lines | 99 code | 20 blank | 60 comment | 19 complexity | f4e5a4aa83fb988a61922c4bd23cbc37 MD5 | raw file
  1/*
  2 * server.c
  3 * --------------------------
  4 * Defines functions for building the buffer,
  5 * sending it to the server, and receiving and
  6 * storing the return data.
  7 */
  8
  9#include "server.h"
 10
 11void adduserpass (void)
 12{
 13	/*
 14	 * Add the username and password to the save buffer. This happens every time, thus the
 15	 * need for a separate function.
 16	 */
 17
 18	int i; //Counter variable
 19
 20	//Fill in username
 21	for (i = 4; i < 4 + userlength; i++)
 22	{
 23		buffer[i] = username[i];
 24	}
 25	//Add the null byte as the delimiter
 26	buffer[4 + userlength] = (char) 0;
 27	//Fill in password
 28	for (i = 4 + userlength + 1; i < 4 + userlength + 1 + passlength; i++)
 29	{
 30		buffer[i] = password[i];
 31	}
 32	//Add the null byte as the delimiter
 33	buffer[4 + userlength + 1 + passlength] = 0;
 34}
 35
 36
 37void buildbuffer(int type)
 38{
 39	/*
 40	 * Build the buffer to be sent (int buffer[]).
 41	 */
 42	int i = 0;
 43	//Save data
 44	if(type == 0)
 45	{
 46		//The length of the packet, 3 bytes for length, 1 byte for command, rest for data
 47		bufferlength = 3 + 1 + (userlength+1) + (passlength+1) + (2*TPoints*4);
 48
 49		//Fill in the 3 length bytes
 50		buffer[0] = (char)(bufferlength >> 16);
 51		buffer[1] = (char)(bufferlength % (256 * 256) >> 8);
 52		buffer[2] = (char)(bufferlength % (256 * 256 * 256));
 53
 54		//The command byte is 0
 55		buffer[3] = (char)0;
 56
 57		//Put in username and password
 58		adduserpass();
 59
 60		//Fill in the save data
 61		for (i = 4 + (userlength+1) + (passlength+1); i < (2*TPoints*4) + (userlength+1) + (passlength+1); i++)
 62		{
 63			buffer[i] = (char) (spawn[i / 8] >> 8);
 64			buffer[++i] = (char) (spawn[(i - 1) / 8] % 256);
 65			buffer[++i] = (char) ((int)(x[(i - 2) / 8]) >> 8);
 66			buffer[++i] = (char) ((int)(x[(i - 3) / 8]) % 256);
 67			buffer[++i] = (char) (((int)y[(i - 4) / 8]) >> 8);
 68			buffer[++i] = (char) ((int)(y[(i - 5) / 8]) % 256);
 69			buffer[++i] = (char) (element[(i - 6) / 8] >> 8);
 70			buffer[++i] = (char) (element[(i - 7) / 8] % 256);
 71		}
 72
 73		//SEND DATA HERE
 74	}
 75	//Load a save
 76	else if (type == 1)
 77	{
 78		//Length of the packets: 3 bytes for length, 1 byte for command, rest of user and pass data
 79		bufferlength = 3 + 1 + (userlength + 1) + (passlength + 1);
 80		//Fill in length data
 81		buffer[0] = (char)(bufferlength >> 16);
 82		buffer[1] = (char)(bufferlength % (256 * 256) >> 8);
 83		buffer[2] = (char)(bufferlength % (256 * 256 * 256));
 84		//Command byte is 1
 85		buffer[3] = (char)1;
 86
 87		//Put in username and password
 88		adduserpass();
 89
 90		//Add the data for what file
 91		//SEND DATA HERE
 92	}
 93	//Validate username and password
 94	else if (type == 2)
 95	{
 96		//Length of the packets: 3 bytes for length, 1 byte for command, rest of user and pass data
 97		bufferlength = 3 + 1 + (userlength + 1) + (passlength + 1);
 98		//Fill in length data
 99		buffer[0] = (char)(bufferlength >> 16);
100		buffer[1] = (char)(bufferlength % (256 * 256) >> 8);
101		buffer[2] = (char)(bufferlength % (256 * 256 * 256));
102		//Command byte is 2
103		buffer[3] = (char)2;
104
105		//Put in username and password
106		adduserpass();
107
108		//SEND DATA HERE
109	}
110	//Register a username with a password
111	else if (type == 3)
112	{
113			//Length of packet: 3 bytes for length, 1 byte for command, rest for user and pass data
114			bufferlength = 3 + 1 + (userlength + 1) + ( passlength + 1 );
115			//Fill in length bytes
116			buffer[0] = (char)(bufferlength >> 16);
117			buffer[1] = (char)(bufferlength % (256 * 256) >> 8);
118			buffer[2] = (char)(bufferlength % (256 * 256 * 256));
119			//Command is 3
120			buffer[3] = (char)3;
121
122			//Put in username and password
123			adduserpass();
124
125			//SEND DATA HERE
126	}
127	//Send custom element data (still not used)
128	else if (type = 4)
129	{
130		/*
131		int length = 3 + 1 + (TElements * 2);
132		buffer[0] = (char)(length >> 16);
133		buffer[1] = (char)(length % (256 * 256) >> 8);
134		buffer[2] = (char)(length % (256 * 256 * 256));
135		buffer[3] = (char)1;
136		for (i = 4; i < 2 * TElements; i++)
137		{
138			buffer[i] = (char) (colliseelement1[i / 2]);
139			buffer[++i] = (char) (collision[22][(i - 1) / 2]);
140		}
141		*/
142	}
143}
144
145int sendbuffer(void)
146{
147	/*
148	 * Create the socket and send the buffer that was previously created.
149	 */
150	sockfd = socket(AF_INET, SOCK_STREAM, 0); //Create a socket using IPv4 and TCP
151	if(sockfd < 0)
152	{
153		error = "Could not create socket";
154		return 0;
155	}
156	server = gethostbyname(SERVER_IP); //Create the hostent using server IP
157	bzero((char*) &serv_addr, sizeof(serv_addr)); //Clear the serv_addr struct
158	serv_addr.sin_family = AF_INET; //Use IPv4
159	bcopy((char*)server->h_addr, (char*)&serv_addr.sin_addr.s_addr, server->h_length); //Use the hostent to fill the serv_addr struct
160	serv_addr.sin_port = htons(PORTNUM); //Set up the port number using network order
161	if(connect(sockfd, &serv_addr, sizeof(serv_addr)) < 0)
162	{
163		error = "Could not connect";
164		return 0;
165	}
166
167	while(bufferlength > 0)
168	{
169		n = write(sockfd, buffer, bufferlength);
170		if(n < 0)
171		{
172			error = "Could not write to socket";
173			return 0;
174		}
175		bufferlength -= n;
176	}
177
178	return 1;
179}