/*! HELLO command (Handshake request) */ void QwsClientSocket::handleMessageHELLO(const QwMessage &message) { Q_UNUSED(message); qDebug() << this << "Received a HELLO handshake request."; m_sessionState = Qws::StateConnected; emit sessionStateChanged(m_sessionState); sendServerInfo(); }
void IRCLobbyServer::notifyStartup() { if(gameconfig->hostorjoin== _game_session_host) { // tell everyone the server just started sendServerInfo(getChannelName()); } else if(gameconfig->hostorjoin== _game_session_join) { refreshServerList(); } }
void SMWServer::onReceive(NetPeer& client, const uint8_t* data, size_t dataLength) { //printf("Receive event.\n"); if (!data || dataLength < 3) return; uint8_t versionMajor = data[0]; uint8_t versionMinor = data[1]; uint8_t messageType = data[2]; uint64_t playerID = client.getPlayerID(); //printf(" from %lu: %u.%u:%u\n", playerID, versionMajor, versionMinor, messageType); if (versionMajor != NET_PROTOCOL_VERSION_MAJOR || versionMinor != NET_PROTOCOL_VERSION_MINOR) { sendCode(client, NET_RESPONSE_BADPROTOCOL); return; } switch (messageType) { // // Server-related // case NET_REQUEST_SERVERINFO: sendServerInfo(client); break; case NET_REQUEST_CONNECT: playerConnectsServer(playerID, data, dataLength); break; case NET_REQUEST_LEAVE_SERVER: playerLeavesRoom(playerID); break; // // Room-related // case NET_REQUEST_ROOM_LIST: sendVisibleRoomEntries(client); break; case NET_REQUEST_CREATE_ROOM: playerCreatesRoom(playerID, data, dataLength); break; case NET_REQUEST_JOIN_ROOM: playerJoinsRoom(playerID, data, dataLength); break; case NET_REQUEST_LEAVE_ROOM: playerLeavesRoom(playerID); break; case NET_NOTICE_MAP_CHANGE: hostChangesMap(playerID, data, dataLength); break; case NET_NOTICE_ROOM_CHAT_MSG: playerSendsChatMsg(playerID, data, dataLength); break; // // Room start // case NET_G2L_START_ROOM: playerStartsRoom(playerID); break; /*case NET_NOTICE_GAME_SYNC_OK: log("NET_NOTICE_GAME_SYNC_OK!"); startRoomIfEveryoneReady(playerID); break;*/ default: printf("Unknown: %d", messageType); /*for (int a = 0; a < netLayer.lastIncomingDataLength(); a++) printf(" %3d", netLayer.lastIncomingData()[a]);*/ printf("\n"); break; } // end of switch }
// read a line of irc and process it. void IRCLobby::processMessage() { assert(irc_server_socket != 0); char buf[1024]; char *host, *mess, *host_end, *user_end, *code,*irc_user,*real_user, *buf_start; readIRCLine(buf, sizeof(buf)); #ifndef WITHOUT_NETPANZER LOGGER.debug("recv irc:%s",buf); #endif buf_start=buf; if(buf[0]==':') { buf_start++; } real_user=irc_user=buf_start; if(strncmp(real_user,nickname_prefix,sizeof(nickname_prefix)-1)==0) { real_user+=sizeof(nickname_prefix)-1; } code=irc_user; // skip 1 word and spaces behind it while(*code && !isspace(*code)) { code++; } while(*code && isspace(*code)) { code++; } if((mess=strchr(code,':'))==NULL) { return; } mess++; char *code_end=code; while(*code_end && !isspace(*code_end)) code_end++; *code_end=0; int code_i=atoi(code); if(code_i == 433) { // wrong user name, change the number at the end char newplayer[256]; char *p; strncpy(newplayer,nickname.c_str(),sizeof(newplayer)-2); newplayer[sizeof(newplayer)-2]=0; p=strchr(newplayer,0); if(isdigit(p[-1])) { p--; while(isdigit(*p) && p>newplayer) p--; p++; } snprintf(p,(newplayer+sizeof(newplayer))-p,"%i",atoi(p)+1); changeNickName(newplayer); return; } if(code_i==1) { sendLoginInfo(); return; } if(code_i>=400 && code_i<500) { addChatMessage("Error",mess); LOG(("IRC:%s",buf)); return; } if(strcmp(code,"NOTICE")==0) { addChatMessage("Lobby",mess); return; } if(code_i==353) { char user_list[1024]; char *u=user_list; char *m=mess; while(*m) { if(strncmp(m,nickname_prefix,sizeof(nickname_prefix)-1)==0) { m+=sizeof(nickname_prefix)-1; } *u++=*m++; } *u=0; addChatMessage("Lobby",user_list); return; } if(strcmp(code,"PONG")==0) { expected_ping=0; return; } if(strcmp(code,"PING")==0 || strncmp(buf,"PING",4)==0) { std::stringstream pong; pong << "PONG " <<mess; sendIRCLine(pong.str()); return; } // get remote user/host if( (host=strchr(buf,'@'))==0 || (user_end=strchr(buf,'!'))==0 ) { return; } *host++=0; *user_end++=0; if(strcmp(code,"JOIN")==0) { std::string joined(real_user); joined+=" has arrived in lobby"; addChatMessage("",joined); return; } if(strcmp(code,"PART")==0 || strcmp(code,"QUIT")==0) { std::string leave(real_user); leave+=" has left the lobby"; addChatMessage("",leave); return; } if((host_end=strchr(host,' '))==0) { return; } *host_end++=0; while(isspace(*host_end)) host_end++; if(strcmp(code,"PRIVMSG")!=0) { return; } if(mess[0]=='#') { // this is a chat message addChatMessage(real_user, mess+1); return; } if(mess[0]=='-') { // this is an internal message if(strcmp(mess+1, ask_server_running_mess)==0) { // reply with server details sendServerInfo(irc_user); } else if(strncmp(mess+1,server_running_mess,sizeof(server_running_mess)-1)==0) { // add a server to the list const char *p=mess+strlen(server_running_mess)+1; const char *map; int players=atoi(p); if((p=strchr(p,'/'))==0) { LOG(("bad server description: %s\n",mess)); return; } int max_players=atoi(++p); int port=_NETPANZER_DEFAULT_PORT_TCP; char *port_str; if((port_str=strstr(p,"port:"))!=0) { port=atoi(port_str+5); } if((map=strstr(p,"map:"))==0) { LOG(("no map name: %s\n",mess)); return; } map+=4; GameServer *server = game_servers->find(host, port); if(server==0) { SDL_mutexP(game_servers_mutex); game_servers->push_back( GameServer(host, port, irc_user,real_user,map, players, max_players)); SDL_mutexV(game_servers_mutex); } else { server->irc_user = irc_user; server->real_user = real_user; server->map = map; server->playercount = players; server->max_players = max_players; } } else if(strncmp(mess+1,leaving_mess,sizeof(leaving_mess)-1)==0) { addChatMessage(real_user,mess); } } }