void ReadGame (char *filename) { FILE *f; int i; char str[16]; gi.FreeTags (TAG_GAME); f = fopen (filename, "rb"); if (!f) gi.error ("Couldn't open %s", filename); fread (str, sizeof(str), 1, f); if (strcmp (str, __DATE__)) { fclose (f); gi.error ("Savegame from an older version.\n"); } g_edicts = gi.TagMalloc (game.maxentities * sizeof(g_edicts[0]), TAG_GAME); globals.edicts = g_edicts; fread (&game, sizeof(game), 1, f); game.clients = gi.TagMalloc (game.maxclients * sizeof(game.clients[0]), TAG_GAME); for (i=0 ; i<game.maxclients ; i++) ReadClient (f, &game.clients[i]); fclose (f); }
/* * 必须在服务器端调用 */ int Socket::WaitClient() { // 初始化文件描述符集合 initialize file descriptor set FD_ZERO(&_sockets); // 把Sock_fd加入到文件描述符集合 FD_SET(_server_sock, &_sockets); _maxsock = _socks[_conn_amount-1]; // 把活动的socket的句柄加入到文件描述符集合中 for (int i = 0; i < _conn_amount; i++) { FD_SET(_socks[i], &_sockets); } // 设置为永久阻塞,因此会阻塞掉调用线程。 int ret = select(_maxsock + 1, &_sockets, NULL, NULL, NULL); if (ret <= 0) { perror("select error!"); return -1; } // 轮询各个文件描述符(socket) for (int i = 0; i < _conn_amount; i++) { if (FD_ISSET(_socks[i], &_sockets)) { if (_socks[i] == _server_sock) { AcceptClient(); } else { ReadClient(i); } } } return 0; }
void ServerPanelRpcServer::incomingConnection(int iSocket) { // Check to see if the server is disabled if (this->bDisabled) { // We're done return; } // Create a new asynchronous socket QTcpSocket* cSocket = new QTcpSocket(this); // Add the event handler to read the client connect(cSocket, SIGNAL(readyRead()), this, SLOT(ReadClient())); // Add the event handler for disconnecting a client connect(cSocket, SIGNAL(disconnected()), this, SLOT(DiscardClient())); // Set the socket descriptor cSocket->setSocketDescriptor(iSocket); }
/* * Read the game structs from * a file. Called when ever a * savegames is loaded. */ void ReadGame(const char *filename) { FILE *f; int i; char str_ver[32]; char str_game[32]; char str_os[32]; char str_arch[32]; gi.FreeTags(TAG_GAME); f = fopen(filename, "rb"); if (!f) { gi.error("Couldn't open %s", filename); } /* Sanity checks */ fread(str_ver, sizeof(str_ver), 1, f); fread(str_game, sizeof(str_game), 1, f); fread(str_os, sizeof(str_os), 1, f); fread(str_arch, sizeof(str_arch), 1, f); if (strcmp(str_ver, SAVEGAMEVER)) { fclose(f); gi.error("Savegame from an incompatible version.\n"); } else if (strcmp(str_game, GAMEVERSION)) { fclose(f); gi.error("Savegame from an other game.so.\n"); } else if (strcmp(str_os, OS)) { fclose(f); gi.error("Savegame from an other os.\n"); } else if (strcmp(str_arch, ARCH)) { fclose(f); gi.error("Savegame from an other architecure.\n"); } g_edicts = gi.TagMalloc(game.maxentities * sizeof(g_edicts[0]), TAG_GAME); globals.edicts = g_edicts; fread(&game, sizeof(game), 1, f); game.clients = gi.TagMalloc(game.maxclients * sizeof(game.clients[0]), TAG_GAME); for (i = 0; i < game.maxclients; i++) { ReadClient(f, &game.clients[i]); } fclose(f); }
/** Read data from clients @param data data to be read */ void* ReadClient(void *data) { TuxLogger_Debug("Start ReadClient()",NULL); if (data == NULL) return NULL; tux_client client = (tux_client) data; if (client == NULL) { return NULL; } char *buff = (char *) malloc(sizeof(char) * 2048); int iResult; int isAlive = 0; TuxLogger_Debug("ReadClient() varible are initialized",NULL); TuxLogger_Debug("Start reading data from the client %d", client->id); do { TuxLogger_Debug("Reading looping for client %d", client->id); iResult = recv(client->sock, buff, 2048, 0); if (iResult > 0) { TuxLogger_Debug("Receiving data !",NULL); isAlive = 1; TuxLogger_Debug( "Allocating memory for the command",NULL); char *cmd = (char *) malloc(sizeof(char) * (iResult + 1)); TuxLogger_Debug( "Allocation OK",NULL); TuxLogger_Debug( "Copy data in cmd",NULL); snprintf(cmd, iResult + 1, "%s", buff); TuxLogger_Debug( "Raw data received: %s", cmd); if (buff != NULL) { /*free(buff); buff=NULL;*/ } ParseCommand(client, trim(cmd)); if (cmd != NULL) { /*free(cmd); cmd=NULL;*/ } } else isAlive = 0; break; } while (iResult > 0); if (isAlive == 1) { TuxLogger_Debug( "Client is still alive. Keep watching data coming from him",NULL); ReadClient(data); } else { TuxLogger_Debug( "Connection with the client %d has been closed by him. Closing the socket..", client->id); removeUniqueID(client->pID); close(client->sock); TuxLogger_Debug( "Socket closed", client->id); } if (client->uKey != NULL) { /*free(client->uKey); client->uKey = NULL;*/ } if (client->username != NULL) { /*free(client->username); client->username=NULL;*/ } if (client != NULL) { /*free(client); client=NULL;*/ } if (data != NULL) { /*free(data); data=NULL;*/ } return 0; }