//Copyright (C) 2007 Ryan Houdek////This library is free software; you can redistribute it and/or//modify it under the terms of the GNU Lesser General Public//License as published by the Free Software Foundation; either//version 2.1 of the License, or (at your option) any later version.////This library is distributed in the hope that it will be useful,//but WITHOUT ANY WARRANTY; without even the implied warranty of//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU//Lesser General Public License for more details.////You should have received a copy of the GNU Lesser General Public//License along with this library; if not, write to the Free Software//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA#define WIN32_LEAN_AND_MEAN#include "Includes.h"#include "tcputil.h"#include <string.h>#include <sys/types.h>#ifndef WIN32#include <unistd.h>#include <sys/time.h>#else#include <windows.h>#include <time.h>#endif#include <stdlib.h>#include <math.h>#define MAXLEN (10*1024) // 10 KB - adequate for text!#define pi 3.14159265#define mvspeed 4#define maxparticles 500#define FALSE 0#define TRUE 1#include <stdio.h>using namespace std;int local_thread_main ( void * ); TCPsocket sock;bool debug[2] = { false }; //Debug[1] is for bulletsbool minimap = true;int done = 0;SDL_Thread *net_thread = NULL, *local_thread = NULL;int fhw = 0;int reso = 4;int texmode = GL_RGB4;int full = SDL_OPENGL | SDL_FULLSCREEN;int mode = 16;int PlayerID = 0;double shootTime[2];bool rec = FALSE;long size = 0;long point = 0;unsigned char *Server;FILE * pFile;#ifdef WIN32#define strcasecmp _stricmp#define strncasecmp _strnicmpchar *strsep ( char ** stringp, const char *delim ){ char *p; if (!stringp) return (NULL); p = *stringp; while (** stringp && !strchr(delim, ** stringp)) (*stringp)++; if (** stringp) { ** stringp = '\0'; (*stringp)++; } else *stringp = NULL; return (p);}#endifint inString ( char *String, char *String2 ){ srand(time(0)); int found = 0; for ( int i = 0; i < strlen(String); i++ ) { if (String[i] == String2[found]) { found++; if (found >= strlen(String2)) { break; } continue; } found = 0; } if (found >= strlen(String2)) { return 0; } if (found == 0) { return -1; } return found;}void do_command ( char *msg ){ if(rec == TRUE) { if(strlen(msg) == 4) point = size; if(point >= size) { printf("\n\nDone!\nPoints:%i size:%i\n",point-1,size); //printf("%s\n\n\n",Server); for(int a = 0; a < size;a++) { fputc ( Server[a] , pFile ); printf("%d ",Server[a]); } fclose(pFile); rec = FALSE; }else { char tmp[2]; sprintf(tmp,"%c",atoi(msg)); Server[point] = tmp[0]; point++; putMsg(sock, "/START"); } } char *command, *p; int len; len = strlen(msg); p = msg; command = strsep(&p, " \t"); // /NICK : change the clients name if (inString(command, "SIZE") == 0) { sscanf(p, "%d", &size); Server = (unsigned char*)malloc(size + 18); putMsg(sock, "/START"); return; } if (inString(command, "START") == 0) { rec = TRUE; pFile = fopen ("GameServer","wb"); if (pFile==NULL) perror ("Error opening file"); putMsg(sock, "/START"); return; }}// the network input loopint net_thread_main ( void *data ){ TCPsocket sock = (TCPsocket)data; SDLNet_SocketSet set; int numready; char *str = NULL; set = SDLNet_AllocSocketSet(1); if (!done && !set) { printf("SDLNet_AllocSocketSet: %s\n", SDLNet_GetError()); SDLNet_Quit(); SDL_Quit(); done = 2; } if (!done && SDLNet_TCP_AddSocket(set, sock) == -1) { printf("SDLNet_TCP_AddSocket: %s\n", SDLNet_GetError()); SDLNet_Quit(); SDL_Quit(); done = 3; } while (!done) { numready = SDLNet_CheckSockets(set, (Uint32)-1); if (numready == -1) { printf("SDLNet_CheckSockets: %s\n", SDLNet_GetError()); done = 4; break; } // check to see if the server sent us data if (numready && SDLNet_SocketReady(sock)) { // getMsg is in tcputil.h, it gets a string from the socket // with a bunch of error handling if (!getMsg(sock, &str)) { char *errstr = SDLNet_GetError(); printf("getMsg: %s\n", strlen(errstr) ? errstr : "Server disconnected"); done = 5; break; } // post it to the screen //printf("%s\n", str); do_command(str); } } if (!done) done = 1; //SDL_KillThread(local_thread); return (0);}#undef mainint main ( int argc, char *argv [] ){ fhw = 1; texmode = GL_CLAMP; reso = 1; full = SDL_OPENGL; mode = 32; //player Test; SDL_Init(0); IPaddress ip; char message[MAXLEN]; int numready; Uint16 port; SDLNet_SocketSet set; fd_set fdset; int result; char *name, *str; struct timeval tv; name="Testing"; SDL_Init(SDL_INIT_VIDEO/*|SDL_INIT_AUDIO*/); /*if(Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 2048) < 0) { printf("Warning: Couldn't set 44100 Hz 16-bit audio\n\ - Reason: %s\n", SDL_GetError()); } Mix_Music *music = Mix_LoadMUS("Bleach.mp3"); Mix_PlayMusic(music, -1);*/ // initialize SDL_net if (SDLNet_Init() == -1) { printf("SDLNet_Init: %s\n", SDLNet_GetError()); SDL_Quit(); exit(3); } set = SDLNet_AllocSocketSet(1); if (!set) { printf("SDLNet_AllocSocketSet: %s\n", SDLNet_GetError()); SDLNet_Quit(); SDL_Quit(); exit(4); //most of the time this is a major error, but do what you want. } // get the port from the commandline port = 5505; // Resolve the argument into an IPaddress type printf("Connecting to %s port %d\n", argv[1], port); if (SDLNet_ResolveHost(&ip, "localhost", port) == -1) { printf("SDLNet_ResolveHost: %s\n", SDLNet_GetError()); SDLNet_Quit(); SDL_Quit(); exit(5); } // open the server socket sock = SDLNet_TCP_Open(&ip); if (!sock) { printf("SDLNet_TCP_Open: %s\n", SDLNet_GetError()); SDLNet_Quit(); SDL_Quit(); exit(6); } if (SDLNet_TCP_AddSocket(set, sock) == -1) { printf("SDLNet_TCP_AddSocket: %s\n", SDLNet_GetError()); SDLNet_Quit(); SDL_Quit(); exit(7); } // login with a name if (!putMsg(sock, name)) { SDLNet_TCP_Close(sock); SDLNet_Quit(); SDL_Quit(); exit(8); } printf("Logged in as %s\n", name); net_thread = SDL_CreateThread(net_thread_main, sock); if (!net_thread) { printf("SDL_CreateThread: %s\n", SDL_GetError()); SDLNet_Quit(); SDL_Quit(); exit(9); } putMsg(sock,"/GET"); while (1) { //Read Key(Crappy Style) for quiting instantly SDL_Event Event; while (SDL_PollEvent(&Event)) { switch(Event.type) { case SDL_QUIT: return 0; break; } } } return 0;}