void TaskEditor::refreshFromServer(){ ServerCommand *command =new ServerCommand(this); command->set("command","getTask"); command->set("taskID",this->store.get("id","").asCString()); this->connect(command,SIGNAL(saveComplete(Json::Value)),this,SLOT(refreshFromServerComplete(Json::Value))); this->connect(command,SIGNAL(saveComplete(Json::Value)),command,SLOT(deleteLater())); command->send(); }
int main( int argc, char ** argv ) { int serv_port = (argc < 2) ? 7000:atoi(argv[1]); ServerCommand server; Manager::ProjectileManager::setServer(&server); Manager::SoundManager::disabled = true; server.startServer(serv_port); run(&server); return 0; }
void ClientApp::Run() { Window* mw = Window::GetInstance(); try { mw -> Initialize(); } catch (...) { ServerCommand cmd = ServerCommand(); cmd.SetType(SSetTextWrong); mw -> CommandHandle(cmd); } }
void LocalPrefServer::addCommandToQueue(const ServerCommand& command, int playerNum) { Command cmd; cmd.PlayerNum = playerNum; cmd.Command.reset(command.Clone()); commands.push(cmd); }
void TaskEditor::save(){ this->updateStore(); if(this->store["id"].asString()==""){ this->store["command"]="addTask"; }else{ this->store["command"]="editTask"; } ServerCommand *command = new ServerCommand(this->store,0); connect(command, SIGNAL(saveComplete(Json::Value)), command, SLOT(deleteLater())); command->send(); connect(command, SIGNAL(saveComplete(Json::Value)), this, SLOT(saveCompleted(Json::Value))); qDebug()<<"saved."; }
void TaskEditor::setStore(Json::Value &s, bool getNotes){ this->store=s; //remove all notes while(this->notes.length()){ this->notes.takeFirst()->deleteLater(); } if(this->store.get("id","")!="" && getNotes){ ServerCommand *command =new ServerCommand(this); command->set("command","getTaskNotes"); command->set("taskID",this->store.get("id","").asCString()); this->connect(command,SIGNAL(saveComplete(Json::Value)),this,SLOT(getNotesResponse(Json::Value))); this->connect(command,SIGNAL(saveComplete(Json::Value)),command,SLOT(deleteLater())); command->send(); } this->updateFromStore(); }
void Window::TimerHandler(int value) { Client* client = Client::GetInstance(); Window* window = Window::GetInstance(); if (!client -> isConnected()) { glutTimerFunc(100, Window::TimerHandler, 0); } else { ServerCommand scmd = client -> ListenOnce(); if (scmd.GetType() != SNothing) window -> CommandHandle( scmd); if (scmd.GetType() == SGameBegin) client -> MakeConnected(); if (scmd.GetType()!=SGameEnd) glutTimerFunc(100, Window::TimerHandler, 0); } }
void ConnectAsFirstBtn::EventHandle (Event e, Info I) { Client* client = Client::GetInstance(); switch (e) { case MouseClick: if (client -> isConnected()) return; if (!client -> isConnected()) { try { client -> Connect(SinglePlayer); } catch (ENoServerAvailable* e) { Window* window = Window::GetInstance(); ServerCommand cmd = ServerCommand(); cmd.SetType(SSetTextNoServerAvailable); window -> CommandHandle(cmd); } catch (EConnectionFailed* e) { Window* window = Window::GetInstance(); ServerCommand cmd = ServerCommand(); cmd.SetType(SSetTextConnectionError); window -> CommandHandle(cmd); } catch(IException* e) { Window* window = Window::GetInstance(); ServerCommand cmd = ServerCommand(); cmd.SetType(SSetTextWrong); window -> CommandHandle(cmd); } } break; case MouseOn: this -> _isActive = true; this -> Draw(); break; case MouseOut: this -> _isActive = false; this -> Draw(); break; } }
void TreeSocket::ProcessConnectedLine(std::string& prefix, std::string& command, parameterlist& params) { User* who = ServerInstance->FindUUID(prefix); std::string direction; if (!who) { TreeServer* ServerSource = Utils->FindServer(prefix); if (prefix.empty()) ServerSource = MyRoot; if (ServerSource) { who = ServerSource->ServerUser; } else { /* It is important that we don't close the link here, unknown prefix can occur * due to various race conditions such as the KILL message for a user somehow * crossing the users QUIT further upstream from the server. Thanks jilles! */ if ((prefix.length() == UIDGenerator::UUID_LENGTH) && (isdigit(prefix[0])) && ((command == "FMODE") || (command == "MODE") || (command == "KICK") || (command == "TOPIC") || (command == "KILL") || (command == "ADDLINE") || (command == "DELLINE"))) { /* Special case, we cannot drop these commands as they've been committed already on a * part of the network by the time we receive them, so in this scenario pretend the * command came from a server to avoid desync. */ who = ServerInstance->FindUUID(prefix.substr(0, 3)); if (!who) who = this->MyRoot->ServerUser; } else { ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Command '%s' from unknown prefix '%s'! Dropping entire command.", command.c_str(), prefix.c_str()); return; } } } // Make sure prefix is still good direction = who->server; prefix = who->uuid; /* * Check for fake direction here, and drop any instances that are found. * What is fake direction? Imagine the following server setup: * 0AA <-> 0AB <-> 0AC * Fake direction would be 0AC sending a message to 0AB claiming to be from * 0AA, or something similar. Basically, a message taking a path that *cannot* * be correct. * * When would this be seen? * Well, hopefully never. It could be caused by race conditions, bugs, or * "miscreant" servers, though, so let's check anyway. -- w * * We also check here for totally invalid prefixes (prefixes that are neither * a valid SID or a valid UUID, so that invalid UUID or SID never makes it * to the higher level functions. -- B */ TreeServer* route_back_again = Utils->BestRouteTo(direction); if ((!route_back_again) || (route_back_again->GetSocket() != this)) { if (route_back_again) ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Protocol violation: Fake direction '%s' from connection '%s'", prefix.c_str(),linkID.c_str()); return; } // Translate commands coming from servers using an older protocol if (proto_version < ProtocolVersion) { if (!PreProcessOldProtocolMessage(who, command, params)) return; } ServerCommand* scmd = Utils->Creator->CmdManager.GetHandler(command); CommandBase* cmdbase = scmd; Command* cmd; if (!scmd) { // Not a special server-to-server command cmd = ServerInstance->Parser->GetHandler(command); if (!cmd) { irc::stringjoiner pmlist(params); ServerInstance->Logs->Log(MODNAME, LOG_SPARSE, "Unrecognised S2S command :%s %s %s", who->uuid.c_str(), command.c_str(), pmlist.GetJoined().c_str()); SendError("Unrecognised command '" + command + "' -- possibly loaded mismatched modules"); return; } cmdbase = cmd; } if (params.size() < cmdbase->min_params) { irc::stringjoiner pmlist(params); ServerInstance->Logs->Log(MODNAME, LOG_SPARSE, "Insufficient parameters for S2S command :%s %s %s", who->uuid.c_str(), command.c_str(), pmlist.GetJoined().c_str()); SendError("Insufficient parameters for command '" + command + "'"); return; } if ((!params.empty()) && (params.back().empty()) && (!cmdbase->allow_empty_last_param)) { // the last param is empty and the command handler doesn't allow that, check if there will be enough params if we drop the last if (params.size()-1 < cmdbase->min_params) return; params.pop_back(); } CmdResult res; if (scmd) res = scmd->Handle(who, params); else res = cmd->Handle(params, who); if (res == CMD_INVALID) { irc::stringjoiner pmlist(params); ServerInstance->Logs->Log(MODNAME, LOG_SPARSE, "Error handling S2S command :%s %s %s", who->uuid.c_str(), command.c_str(), pmlist.GetJoined().c_str()); SendError("Error handling '" + command + "' -- possibly loaded mismatched modules"); } else if (res == CMD_SUCCESS) Utils->RouteCommand(route_back_again, cmdbase, params, who); }
void TreeSocket::ProcessConnectedLine(std::string& prefix, std::string& command, parameterlist& params) { User* who = FindSource(prefix, command); if (!who) { ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Command '%s' from unknown prefix '%s'! Dropping entire command.", command.c_str(), prefix.c_str()); return; } /* * Check for fake direction here, and drop any instances that are found. * What is fake direction? Imagine the following server setup: * 0AA <-> 0AB <-> 0AC * Fake direction would be 0AC sending a message to 0AB claiming to be from * 0AA, or something similar. Basically, a message taking a path that *cannot* * be correct. * * When would this be seen? * Well, hopefully never. It could be caused by race conditions, bugs, or * "miscreant" servers, though, so let's check anyway. -- w * * We also check here for totally invalid prefixes (prefixes that are neither * a valid SID or a valid UUID, so that invalid UUID or SID never makes it * to the higher level functions. -- B */ TreeServer* const server = TreeServer::Get(who); if (server->GetSocket() != this) { ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Protocol violation: Fake direction '%s' from connection '%s'", prefix.c_str(), linkID.c_str()); return; } // Translate commands coming from servers using an older protocol if (proto_version < ProtocolVersion) { if (!PreProcessOldProtocolMessage(who, command, params)) return; } ServerCommand* scmd = Utils->Creator->CmdManager.GetHandler(command); CommandBase* cmdbase = scmd; Command* cmd = NULL; if (!scmd) { // Not a special server-to-server command cmd = ServerInstance->Parser.GetHandler(command); if (!cmd) { if (command == "ERROR") { this->Error(params); return; } throw ProtocolException("Unknown command"); } cmdbase = cmd; } if (params.size() < cmdbase->min_params) throw ProtocolException("Insufficient parameters"); if ((!params.empty()) && (params.back().empty()) && (!cmdbase->allow_empty_last_param)) { // the last param is empty and the command handler doesn't allow that, check if there will be enough params if we drop the last if (params.size()-1 < cmdbase->min_params) return; params.pop_back(); } CmdResult res; if (scmd) res = scmd->Handle(who, params); else { res = cmd->Handle(params, who); if (res == CMD_INVALID) throw ProtocolException("Error in command handler"); } if (res == CMD_SUCCESS) Utils->RouteCommand(server->GetRoute(), cmdbase, params, who); }
int ServerTP::Run() { cout << "\nCreating server for two players...\n"; sockaddr_in addr; char buf[MAX_MSG_LENGTH]; int bytes_read; bool connected = false; addr.sin_family = AF_INET; inet_aton("127.0.0.1", &addr.sin_addr); PortsPool* ports = PortsPool::GetInstance(2); int CheckedPorts[1000]; int i = 0; while (!connected) { _port = htons(ports -> GetPort()); if (_port < 0) { for (int j = 0; j < i; ++j) ports -> RetPort(CheckedPorts[j]); throw new ENoPortsAvailable(); } _listener = socket(AF_INET, SOCK_STREAM, 0); if (_listener < 0) { for (int j = 0; j < i; ++j) ports -> RetPort(CheckedPorts[j]); throw new EConnectionFailed(); } addr.sin_family = AF_INET; addr.sin_port = _port; inet_aton("127.0.0.1", &addr.sin_addr); if (bind(_listener, (sockaddr*)&addr, sizeof(addr)) < 0) { CheckedPorts[i++] = _port; close (_listener); } else { connected = true; } } for (int j = 0; j < i; ++j) ports -> RetPort(CheckedPorts[j]); cout << "Connection port: " << this -> _port << '\n'; listen(_listener, 2); cout << "Waiting for clients...\n"; _sockdesc1 = accept(_listener, NULL, NULL); if (_sockdesc1 < 0) { cout << "Error first connection"; throw new EConnectionFailedFirst(); } ServerCommand scmd1 = ServerCommand(); scmd1.SetCommand(SConnectPermissionFirst); char* scmdb1 = scmd1.ToString(); send (_sockdesc1,scmdb1, MAX_MSG_LENGTH, 0); delete[] scmdb1; cout << "First connected. Waiting for second...\n"; _sockdesc2 = accept(_listener, NULL, NULL); if (_sockdesc2 < 0) { cout << "Error second connection.\n"; throw new EConnectionFailedSecond(); } ServerCommand scmd2 = ServerCommand(); scmd2.SetCommand(SConnectPermissionSecond); char* scmdb2 = scmd2.ToString(); send (_sockdesc2,scmdb2, MAX_MSG_LENGTH, 0); delete[] scmdb2; cout << "Two clients are ready\n"; int maxSock = (_sockdesc1 > _sockdesc2 ? _sockdesc1 : _sockdesc2); fcntl(_listener, F_SETFL, O_NONBLOCK); ServerCommand scmd3 = ServerCommand(); scmd3.SetCommand(SGameBegin); char* scmdb3 = scmd3.ToString(); send (_sockdesc1,scmdb3, MAX_MSG_LENGTH, 0); send (_sockdesc2,scmdb3, MAX_MSG_LENGTH, 0); delete[] scmdb3; _game -> SwitchState(); while(true) { fd_set readset; FD_ZERO(&readset); FD_SET(_listener, &readset); FD_SET(_sockdesc1, &readset); FD_SET(_sockdesc2, &readset); timeval timeout; timeout.tv_sec = 10; timeout.tv_usec = 0; if (select(maxSock+1, &readset, NULL, NULL, &timeout ) <= 0) { continue; } if ( FD_ISSET(_sockdesc1, &readset)) { bytes_read = recv(_sockdesc1, buf, MAX_MSG_LENGTH, 0); if (bytes_read <= 0) { cout << "Receiving from 1 error\n"; throw new EConnectionFailedFirst(); } cout << "Received from first: " << bytes_read << " bytes\n"; ClientCommand ccmd = ClientCommand(buf); ServerCommand scmd = _game -> GetAnswer(ccmd); char* scmdbytes = scmd.ToString(); switch (scmd.GetAddr()) { case F: send(_sockdesc1, scmdbytes, MAX_MSG_LENGTH, 0); break; case S: send(_sockdesc2, scmdbytes, MAX_MSG_LENGTH, 0); break; case B: send(_sockdesc1, scmdbytes, MAX_MSG_LENGTH, 0); send(_sockdesc2, scmdbytes, MAX_MSG_LENGTH, 0); break; default: ; } if (scmd.GetWinner()!=0) { cout << "Winner: " << scmd.GetWinner() << '\n'; scmd.SetCommand(SGameEnd); scmdbytes = scmd.ToString(); send(_sockdesc1, scmdbytes, MAX_MSG_LENGTH, 0); send(_sockdesc2, scmdbytes, MAX_MSG_LENGTH, 0); close (_listener); close (_sockdesc1); close (_sockdesc2); ports -> RetPort(this -> _port); break; } delete[] scmdbytes; } if (FD_ISSET(_sockdesc2, &readset)) { bytes_read = recv(_sockdesc2, buf, MAX_MSG_LENGTH, 0); if (bytes_read <= 0) { cout << "Receiving from 2 error\n"; throw new EConnectionFailedSecond(); } cout << "Received from second: " << bytes_read << " bytes\n"; ClientCommand ccmd = ClientCommand(buf); ServerCommand scmd = _game -> GetAnswer(ccmd); char* scmdbytes = scmd.ToString(); switch (scmd.GetAddr()) { case F: send(_sockdesc1, scmdbytes, MAX_MSG_LENGTH, 0); break; case S: send(_sockdesc2, scmdbytes, MAX_MSG_LENGTH, 0); break; case B: send(_sockdesc1, scmdbytes, MAX_MSG_LENGTH, 0); send(_sockdesc2, scmdbytes, MAX_MSG_LENGTH, 0); break; default: ; } if (scmd.GetWinner()!=0) { cout << "Winner: " << scmd.GetWinner() << '\n'; scmd.SetCommand(SGameEnd); scmdbytes = scmd.ToString(); send(_sockdesc1, scmdbytes, MAX_MSG_LENGTH, 0); send(_sockdesc2, scmdbytes, MAX_MSG_LENGTH, 0); close (_listener); close (_sockdesc1); close (_sockdesc2); ports -> RetPort(this -> _port); break; } delete[] scmdbytes; } } }