void LiveServer::OnReceiveHello(LivePeer* connection, NetworkMessage* nmsg) { uint32_t rvid = nmsg->ReadU32(); if(rvid != __RME_VERSION_ID__) { NetworkMessage* omsg = AllocMessage(); omsg->AddByte(PACKET_KICK); // FAREWELL omsg->AddString("Wrong editor version."); connection->Send(omsg); connection->Close(); return; } uint32_t net_ver = nmsg->ReadU32(); if(net_ver != __LIVE_NET_VERSION__) { NetworkMessage* omsg = AllocMessage(); omsg->AddByte(PACKET_KICK); // FAREWELL omsg->AddString("Wrong protocol version."); connection->Send(omsg); connection->Close(); return; } uint32_t client_version = nmsg->ReadU32(); std::string rnick = nmsg->ReadString(); std::string pass = nmsg->ReadString(); wxString upass = wxString(pass.c_str(), wxConvUTF8); if(password != upass) { log->Message(wxT("Client tried to connect, but used the wrong password, connection refused.")); connection->Close(); return; } connection->SetNick(wxString(rnick.c_str(), wxConvUTF8)); log->Message(connection->GetNick() << wxT(" (") << connection->GetHost() << wxT(") connected.")); if((ClientVersionID)client_version != gui.GetCurrentVersionID()) { NetworkMessage* omsg = AllocMessage(); omsg->AddByte(PACKET_CHANGE_CLIENT_VERSION); // CHANGE CLIENT VER omsg->AddU32(gui.GetCurrentVersionID()); connection->Send(omsg); } else { // Reply that we have accepted the remote connection NetworkMessage* omsg = AllocMessage(); omsg->AddByte(PACKET_ACCEPTED_CLIENT); // CONNECTION ACCEPTED WITHOUT ISSUE connection->Send(omsg); } }
void LiveServer::OnReceiveReady(LivePeer* connection, NetworkMessage* nmsg) { // Client has changed server version to the proper one PeerList::iterator piter = std::find(connecting_clients.begin(), connecting_clients.end(), connection); if (piter == connecting_clients.end()) { connection->Close(); return; } connecting_clients.erase(piter); // Find free client id bool f = false; for(size_t s = 1; s < 16; ++s) { if((1 << s) & ~client_mask) { connection->SetClientID(s); client_mask |= (1 << s); f = true; break; } } if(!f) { // Not enough room, disconnect! NetworkMessage* omsg = AllocMessage(); omsg->AddByte(0x81); // FAREWELL omsg->AddString("Server is full."); connection->Send(omsg); connection->Close(); return; } connected_clients.push_back(connection); log->UpdateClientList(connected_clients); // Let's reply NetworkMessage* omsg = AllocMessage(); omsg->AddByte(0x80); // HELLO TO YOU TOO! omsg->AddString(editor->map.getName()); omsg->AddU16(editor->map.getWidth()); omsg->AddU16(editor->map.getHeight()); connection->Send(omsg); // Change parser connection->parser = &LiveServer::OnParseEditorPackets; }
void RMENet::OnReceivePing(NetworkMessage *nmsg) { // Send pong NetworkMessage* omsg = AllocMessage(); omsg->AddByte(0x02); connection->Send(omsg); }
void LiveServer::UpdateOperation(int percent) { for(PeerList::iterator citer = connected_clients.begin(); citer != connected_clients.end(); ++citer) { NetworkMessage* msg = AllocMessage(); msg->AddByte(PACKET_UPDATE_OPERATION); msg->AddU32(percent); (*citer)->Send(msg); } }
void LiveServer::StartOperation(wxString msg) { for(PeerList::iterator citer = connected_clients.begin(); citer != connected_clients.end(); ++citer) { NetworkMessage* nmsg = AllocMessage(); nmsg->AddByte(PACKET_START_OPERATION); nmsg->AddString(nstr(msg)); (*citer)->Send(nmsg); } }
void LiveServer::BroadcastChat(wxString speaker, wxString message) { for(PeerList::iterator citer = connected_clients.begin(); citer != connected_clients.end(); ++citer) { NetworkMessage* nmsg = AllocMessage(); nmsg->AddByte(PACKET_SERVER_TALK); nmsg->AddString(nstr(speaker)); nmsg->AddString(nstr(message)); (*citer)->Send(nmsg); } log->Chat(name, message); }
void LiveServer::BroadcastCursor(const LiveCursor& cursor) { for(PeerList::iterator citer = connected_clients.begin(); citer != connected_clients.end(); ++citer) { if((*citer)->GetClientID() != cursor.id) { NetworkMessage* msg = AllocMessage(); msg->AddByte(PACKET_CURSOR_UPDATE); AddCursor(msg, cursor); (*citer)->Send(msg); } } }
bool RMENet::Connect() { wxIPV4address ipaddr; ipaddr.Hostname("127.0.0.1");//90.230.54.138")); ipaddr.Service(31312); socket = newd wxSocketClient(wxSOCKET_NOWAIT); connection = newd NetworkConnection(this, socket); socket->SetClientData(connection); socket->SetEventHandler(*this, wxID_ANY); socket->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_OUTPUT_FLAG | wxSOCKET_LOST_FLAG); socket->Notify(true); wxEvtHandler::Connect(wxID_ANY, wxEVT_SOCKET, wxSocketEventHandler(RMENet::HandleEvent)); socket->Connect(ipaddr, false); if(!socket || !socket->WaitOnConnect(5, 0) || !socket || !socket->IsConnected()) { if(socket) socket->Destroy(); socket = nullptr; delete connection; connection = nullptr; return false; } NetworkMessage* nmsg = AllocMessage(); nmsg->AddByte(0x00); // Hello! nmsg->AddU32(__LIVE_NET_VERSION__); nmsg->AddString(g_settings.getString(Config::LIVE_USERNAME)); nmsg->AddString(g_settings.getString(Config::LIVE_PASSWORD)); connection->Send(nmsg); return true; }
bool Player::substractMoneyContainer(Container *container, unsigned long *money) { int goldcoins; int platcoins; NetworkMessage msg; for(int i=0; i<container->size();i++) { Item *item = container->getItem(i); Container* new_container = dynamic_cast<Container*>(item); if(new_container && *money) { substractMoneyContainer(new_container, money); } else if(item && *money) { switch(item->getID()) { case ITEM_COINS_GOLD: //gold coins msg.Reset(); goldcoins = item->getItemCountOrSubtype(); for(containerLayout::const_iterator cit = getContainers(); cit != getEndContainer(); ++cit) { if(cit->second == container) { //remove item msg.AddByte(0x72); msg.AddByte(cit->first); msg.AddByte(i); } } container->removeItem(item); if(*money >= goldcoins) { i--; // If we remove an item from the container then we need substract 1 to the container's main item counter *money -= goldcoins; //delete item; item->releaseThing(); } else { item->setItemCountOrSubtype(goldcoins - *money); *money = 0; container->addItem(item); for(containerLayout::const_iterator cit = getContainers(); cit != getEndContainer(); ++cit) { if(cit->second == container) { //add item msg.AddByte(0x70); msg.AddByte(cit->first); msg.AddU16(item->getID()); msg.AddByte(item->getItemCountOrSubtype()); } } } sendNetworkMessage(&msg); break; case ITEM_COINS_PLATINUM: //platinum coins msg.Reset(); goldcoins = item->getItemCountOrSubtype() * 100; NetworkMessage msg2; for(containerLayout::const_iterator cit = getContainers(); cit != getEndContainer(); ++cit) { if(cit->second == container) { //remove item msg.AddByte(0x72); msg.AddByte(cit->first); msg.AddByte(i); } } container->removeItem(item); if(*money >= goldcoins) { i--; // If we remove an item from the container then we need substract 1 to the container's main item counter *money -= goldcoins; //delete item; item->releaseThing(); } else { platcoins = (int)((goldcoins - *money)/100); goldcoins = (int)(goldcoins - *money)%100; *money = 0; if(platcoins) { item->setItemCountOrSubtype(platcoins); container->addItem(item); for(containerLayout::const_iterator cit = getContainers(); cit != getEndContainer(); ++cit) { if(cit->second == container) { //add item msg.AddByte(0x70); msg.AddByte(cit->first); msg.AddU16(item->getID()); msg.AddByte(item->getItemCountOrSubtype()); } } if(goldcoins) { Item *new_item = Item::CreateItem(ITEM_COINS_GOLD, goldcoins); Container *default_container = dynamic_cast<Container*>(getItem(SLOT_BACKPACK)); if(default_container && default_container->addItem(new_item)) // There is space in container { for(containerLayout::const_iterator cit = getContainers(); cit != getEndContainer(); ++cit) { if(cit->second == default_container) { //add item msg.AddByte(0x70); msg.AddByte(cit->first); msg.AddU16(new_item->getID()); msg.AddByte(new_item->getItemCountOrSubtype()); } } } else // There is no space in container { //TODO: place the item in ground... delete new_item; } } } else { if(goldcoins) { //delete item; item->releaseThing(); Item *new_item = Item::CreateItem(ITEM_COINS_GOLD, goldcoins); item = new_item; container->addItem(item); for(containerLayout::const_iterator cit = getContainers(); cit != getEndContainer(); ++cit) { if(cit->second == container) { //add item msg.AddByte(0x70); msg.AddByte(cit->first); msg.AddU16(item->getID()); msg.AddByte(item->getItemCountOrSubtype()); } } } else { //delete item; item->releaseThing(); item = NULL; } } } sendNetworkMessage(&msg); break; } } } if(*money == 0) return true; else return false; }
bool Player::substractMoney(unsigned long money) { int goldcoins; int platcoins; NetworkMessage msg; for(int i=SLOT_HEAD; i <= SLOT_AMMO; i++) { Container* new_container = dynamic_cast<Container*>(items[i]); if(new_container && money) { substractMoneyContainer(new_container, &money); } else if(items[i] && money) { switch(items[i]->getID()) { case ITEM_COINS_GOLD: //gold coins goldcoins = items[i]->getItemCountOrSubtype(); if(money >= goldcoins) { money -= goldcoins; //delete items[i]; items[i]->releaseThing(); items[i] = NULL; } else { items[i]->setItemCountOrSubtype(goldcoins - money); money = 0; } client->sendInventory(i); break; case ITEM_COINS_PLATINUM: //platinum coins goldcoins = items[i]->getItemCountOrSubtype() * 100; if(money >= goldcoins) { money -= goldcoins; //delete items[i]; items[i]->releaseThing(); items[i] = NULL; } else { platcoins = (int)((goldcoins - money)/100); goldcoins = (goldcoins - money)%100; money = 0; if(platcoins) { items[i]->setItemCountOrSubtype(platcoins); if(goldcoins) { Item *new_item = Item::CreateItem(ITEM_COINS_GOLD, goldcoins); Container *default_container = dynamic_cast<Container*>(getItem(SLOT_BACKPACK)); if(default_container && default_container->addItem(new_item)) // There is space in container { for(containerLayout::const_iterator cit = getContainers(); cit != getEndContainer(); ++cit) { if(cit->second == default_container) { //add item msg.AddByte(0x70); msg.AddByte(cit->first); msg.AddU16(new_item->getID()); msg.AddByte(new_item->getItemCountOrSubtype()); } } } else // There is no space in container { //TODO: place the item in ground... delete new_item; } } } else { //delete items[i]; items[i]->releaseThing(); items[i] = NULL; if(goldcoins) { Item *new_item = Item::CreateItem(ITEM_COINS_GOLD, goldcoins); items[i] = new_item; } } } client->sendInventory(i); break; } } } if(money == 0) return true; else return false; }