diff --git a/CMakeLists.txt b/CMakeLists.txt index ed1a46f1..0fd118cb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -301,6 +301,7 @@ add_subdirectory(dWorldServer) add_subdirectory(dAuthServer) add_subdirectory(dChatServer) add_subdirectory(dMasterServer) # Add MasterServer last so it can rely on the other binaries +add_subdirectory(dChatClient) target_precompile_headers( dZoneManager PRIVATE diff --git a/dChatClient/CMakeLists.txt b/dChatClient/CMakeLists.txt new file mode 100644 index 00000000..ed6c639c --- /dev/null +++ b/dChatClient/CMakeLists.txt @@ -0,0 +1,2 @@ +add_executable(ChatClient ChatClient.cpp) +target_link_libraries(ChatClient raknet dCommon) diff --git a/dChatClient/ChatClient.cpp b/dChatClient/ChatClient.cpp new file mode 100644 index 00000000..15eec33d --- /dev/null +++ b/dChatClient/ChatClient.cpp @@ -0,0 +1,93 @@ +#include +#include +#include +#include +#include + +#include "eConnectionType.h" +#include "eChatMessageType.h" +#include "dCommonVars.h" + +static constexpr uint16_t CHAT_PORT = 2005; +static const char PASS_INTERNAL[16] = "3.25 DARKFLAME1"; +static const char PASS_EXTERNAL[9] = "3.25 ND1"; + +int main(int argc, const char** argv) { + std::cout << "Hello World!" << std::endl; + + SocketDescriptor socketDescriptor(0, 0); + RakPeerInterface* peer = RakNetworkFactory::GetRakPeerInterface(); + uint16_t maxConnections = 1; // one outgoing + bool useEncryption = true; + + if (!peer) { + std::cerr << "Failed to get RakPeer interface!" << std::endl; + return 1; + } + if (!peer->Startup(maxConnections, 10, &socketDescriptor, 1)) { + std::cerr << "Failed to startup rak peer interface!" << std::endl; + return 1; + } + + if (useEncryption) peer->InitializeSecurity(NULL, NULL, NULL, NULL); + + if (!peer->Connect("localhost", CHAT_PORT, PASS_EXTERNAL, 8)) { + std::cerr << "Failed to initiate connection to chat server" << std::endl; + return 1; + } + + // Establish connection + Packet* packet; + bool connected = false; + SystemAddress remote; + + while (!connected) { + packet = peer->Receive(); + if (!packet) continue; + + uint8_t packet_id = packet->data[0]; + switch (packet_id) { + case ID_INVALID_PASSWORD: + std::cerr << "Password invalid" << std::endl; + return 1; + case ID_CONNECTION_REQUEST_ACCEPTED: + std::cout << "Connection accepted" << std::endl; + remote = packet->systemAddress; + connected = true; + break; + default: + std::cout << "Packet: " << static_cast(packet_id) << std::endl; + } + peer->DeallocatePacket(packet); + } + + std::cout << "Starting tests" << std::endl; + + //Notify chat about it + CBITSTREAM; + BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, eChatMessageType::GM_ANNOUNCE); + + std::string title = "My Title"; + std::string message = "My Message"; + + bitStream.Write(title.size()); + bitStream.Write(title.c_str(), title.size()); + bitStream.Write(message.size()); + bitStream.Write(message.c_str(), message.size()); + + peer->Send(&bitStream, SYSTEM_PRIORITY, RELIABLE, 0, remote, false); + + while (true) { + packet = peer->Receive(); + if (!packet) continue; + + uint8_t packet_id = packet->data[0]; + switch (packet_id) { + default: + std::cout << "Packet: " << static_cast(packet_id) << std::endl; + } + peer->DeallocatePacket(packet); + } + + return 0; +} diff --git a/dChatServer/ChatPacketHandler.cpp b/dChatServer/ChatPacketHandler.cpp index 82cea018..cc9c8394 100644 --- a/dChatServer/ChatPacketHandler.cpp +++ b/dChatServer/ChatPacketHandler.cpp @@ -355,6 +355,18 @@ void ChatPacketHandler::HandleGMLevelUpdate(Packet* packet) { inStream.Read(player.gmLevel); } +void ChatPacketHandler::HandleGMAnnounce(Packet* packet) { + CINSTREAM_SKIP_HEADER; + uint32_t titleLen, messageLen; + inStream.Read(titleLen); + std::string title(titleLen, 0); + inStream.Read(title.data(), titleLen); + inStream.Read(messageLen); + std::string message(messageLen, 0); + inStream.Read(message.data(), messageLen); + LOG("GM Announcement from %s: '%s' '%s'", packet->systemAddress.ToString(), title.c_str(), message.c_str()); +} + void ChatPacketHandler::HandleWho(Packet* packet) { CINSTREAM_SKIP_HEADER; diff --git a/dChatServer/ChatPacketHandler.h b/dChatServer/ChatPacketHandler.h index def9c9b9..d48ac28e 100644 --- a/dChatServer/ChatPacketHandler.h +++ b/dChatServer/ChatPacketHandler.h @@ -50,6 +50,7 @@ namespace ChatPacketHandler { void HandleFriendResponse(Packet* packet); void HandleRemoveFriend(Packet* packet); void HandleGMLevelUpdate(Packet* packet); + void HandleGMAnnounce(Packet* packet); void HandleWho(Packet* packet); void HandleShowAll(Packet* packet); diff --git a/dChatServer/ChatServer.cpp b/dChatServer/ChatServer.cpp index 81b6ddef..3e0ff0b1 100644 --- a/dChatServer/ChatServer.cpp +++ b/dChatServer/ChatServer.cpp @@ -122,10 +122,13 @@ int main(int argc, char** argv) { uint32_t framesSinceMasterDisconnect = 0; uint32_t framesSinceLastSQLPing = 0; + // Independant chat server + bool isStandalone = argc > 1 && strcmp(argv[1], "--standalone") == 0; + Game::logger->Flush(); // once immediately before main loop while (!Game::ShouldShutdown()) { //Check if we're still connected to master: - if (!Game::server->GetIsConnectedToMaster()) { + if (!isStandalone && !Game::server->GetIsConnectedToMaster()) { framesSinceMasterDisconnect++; if (framesSinceMasterDisconnect >= chatFramerate) @@ -281,6 +284,7 @@ void HandlePacket(Packet* packet) { break; case eChatMessageType::GM_ANNOUNCE:{ // we just forward this packet to every connected server + ChatPacketHandler::HandleGMAnnounce(packet); inStream.ResetReadPointer(); Game::server->Send(inStream, packet->systemAddress, true); // send to everyone except origin }