#include "SocketServer.h" #include bool SocketServer::loadMapList(char* maploc) { ::std::string workDir; ::Utility::String::extractDirPath( workDir, maploc, '\\' ); //maploc is the filename of the list which contains all maps //load all map file names into the server, but don't load the maps themselves. std::ifstream file; file.open(maploc); if (!file.is_open()) return false; ::std::string str; while(!file.eof()) { ::std::getline( file, str ); maps.push_back( workDir + str ); } /* maps.push_back("map1test.map"); maps.push_back("map2 test.map"); */ return true; } bool SocketServer::LoadInitData(char* maploc) { std::vector cont; char* in=new char[100]; std::ifstream ifs; ifs.open(maploc); if(!ifs.is_open()) { return false; } while(!ifs.eof()) { ifs.getline(in, 100); cont=splitString(in, '='); if (cont.size()==2) { if(!strcmp("nr_players_per_session", cont[0].c_str())) { playersPerSessionCount=atoi(cont[1].c_str()); } else if(!strcmp("nr_kills_to_win", cont[0].c_str())) { killsRequiredPerSession=atoi(cont[1].c_str()); } else if(!strcmp("match_type", cont[0].c_str())) { //Isn't used } } } ifs.close(); } SocketServer::~SocketServer() { serverTCPConnectionLoopActive=false; serverUDPReceiveLoopActive=false; serverTCPReceiveLoopActive=false; for (int i=0; iptr)->serverGameCreationLoop(tra->threadID); return 0; } bool SocketServer::serverGameCreationLoop(int delay) { // TODO: Mem access Violoation Crash 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ... delay = -858993460 //Mem access violation in a thread can also be caused by failure from something else instead of it, //it still breaks at header even if, for example, server->load or lobby.startLobbyCountdown breaks it //If you get an error here, make sure that isn't the problem. int count; while(serverGameCreationActive) { if (nrActiveSessions==0) { count=0; for (unsigned int i=0; i=playersPerSessionCount) { games.resize(1); //lobby.resize(games.size()+1); session =new GameLogic::Session(); lobby = Lobby(); timer.resize(1); timeTillUpdate.resize(1); timeTillUpdate[0]=GAME_UPDATEDELAY; updateCount.resize(1); updateCount[0]=0; int curID=(int)games.size()-1; int mapid=rand()%maps.size(); session->setNrPlayers(playersPerSessionCount); session->setKillsRequired(killsRequiredPerSession); session->load(maps[mapid]); printf("Map nr %d loaded, name %s.\n",mapid, maps[mapid].c_str()); count=0; for (unsigned int i=0; countaccessPlayer(i).spawn(); count++; } } lobbyActive=true; sendLobbyInitData(curID); lobby.startLobbyCountdown(LOBBY_WAIT_TIME); sendRenderData(curID); //return true; } if(lobbyActive) { for (int i=0; i<1; i++) { float ttimer=lobby.timeLeft(); if (ttimer==0) { printf("Starting game.\n"); games[i].initGame(users,playersPerSessionCount); sendInitData(i); nrActiveSessions++; lobbyActive=false; //serverGameCreationActive=false; } } } } Sleep(delay); } printf("Maximum server count reached, shutting down the sever creation thread.\n"); return false; } SocketServer::SocketServer() { UDPSocket = INVALID_SOCKET; nrActiveSessions=0; serverGameCreationActive=false; serverTCPConnectionLoopActive=false; serverTCPReceiveLoopActive=false; serverUDPReceiveLoopActive=false; killsRequiredPerSession=10; playersPerSessionCount=1; LoadInitData("../ServerData.dat"); //--------------------------------------------- // Set up the port and IP of the server //Port starts up as a different one from when UDPSocketected, it changes once the server has exchanged some info with the client UDPRecvAddr.sin_family = AF_INET; UDPRecvAddr.sin_port = htons(UDPRecvPort); UDPRecvAddr.sin_addr.s_addr = htonl(INADDR_ANY); sessionEvents=std::vector(0); sessionEffects=std::vector(0); TCPRecvAddr.sin_family = AF_INET; TCPRecvAddr.sin_port = htons(TCPRecvPort); TCPRecvAddr.sin_addr.s_addr = htonl(INADDR_ANY); addrSize=sizeof(sockaddr_in); for (int i=0; i=users.size()) { //User doesn't exist printf("UDP sendData(%d) sendto failed because the specified user does not exist\n", uid); } else { iResult = sendto(UDPSocket, data, size, 0, (SOCKADDR *) & users[uid].getAddr(), addrSize); if (iResult == SOCKET_ERROR) { wprintf(L"UDP sendData(%d) sendto failed with error: %d\n", uid, WSAGetLastError()); closesocket(UDPSocket); WSACleanup(); return false; } } } return true; } bool SocketServer::sendKeyFrameData(int size, const char* data) { for (int i=0; i