Compare commits

...

3 Commits

Author SHA1 Message Date
Xiphoseer
b78ece8919 wip: Http Client experiments 2024-05-01 23:41:59 +02:00
Xiphoseer
776a9c36a4 wip: test chat client 2024-05-01 22:34:01 +02:00
Daniel Seiler
35c463656d Use volume for mariadb persistence (#1555)
I initially used the bind mount because it's arguably easier to back up and move around than a volume, but turns out with https://github.com/DarkflameUniverse/NexusDashboard/issues/92 it's nothing we can recommend for Docker Desktop on WSL, which unfortunately is the primary setup newcomers will try this with. So changing the default to be a volume should address that (presumably by hosting the volume within the WSL Docker VM, as opposed to the host NTFS filesystem)
2024-04-29 22:51:13 +02:00
11 changed files with 180 additions and 15 deletions

View File

@@ -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

View File

@@ -0,0 +1,5 @@
add_executable(ChatClient ChatClient.cpp)
target_link_libraries(ChatClient raknet dCommon)
add_executable(ChatHttpClient ChatHttpClient.cpp)
target_link_libraries(ChatHttpClient raknet dCommon)

View File

@@ -0,0 +1,93 @@
#include <iostream>
#include <RakPeerInterface.h>
#include <RakNetworkFactory.h>
#include <MessageIdentifiers.h>
#include <BitStream.h>
#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<uint32_t>(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<uint32_t>(title.size());
bitStream.Write(title.c_str(), title.size());
bitStream.Write<uint32_t>(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<uint32_t>(packet_id) << std::endl;
}
peer->DeallocatePacket(packet);
}
return 0;
}

View File

@@ -0,0 +1,32 @@
#include <iostream>
#include <TCPInterface.h>
#include <HTTPConnection.h>
#include <RakSleep.h>
int main(int argc, const char** argv) {
std::cout << "Hello World!" << std::endl;
TCPInterface tcp;
if (!tcp.Start(0, 0)) {
std::cerr << "Failed to start TCP Interface" << std::endl;
return 1;
}
SystemAddress addr = tcp.Connect("127.0.0.1", 8000);
std::cout << "addr: " << addr.ToString() << std::endl;
std::string req = "\
GET /helloFromRakNet.txt HTTP/1.1\r\n\
User-Agent: raknet / 3.25\r\n\
\r\n";
std::cout << req;
tcp.Send(req.c_str(), req.size(), addr);
RakSleep(500);
tcp.CloseConnection(addr);
return 0;
}

View File

@@ -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;

View File

@@ -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);

View File

@@ -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
}

View File

@@ -7,7 +7,7 @@ services:
- darkflame
image: mariadb:latest
volumes:
- ${DB_DATA_DIR:-./db/data}:/var/lib/mysql
- ${DB_DATA_DIR:-db-data}:/var/lib/mysql
environment:
- MARIADB_RANDOM_ROOT_PASSWORD=1
- MARIADB_USER=${MARIADB_USER:-darkflame}
@@ -79,3 +79,6 @@ services:
networks:
darkflame:
volumes:
db-data:

View File

@@ -72,8 +72,14 @@ Source/RakThread.h Source/SuperFastHash.h S
Source/HTTPConnection.h Kbhit.h
)
add_library(raknet STATIC ${RAKNET_SOURCES})
find_package(OpenSSL COMPONENTS SSL)
if(OpenSSL_FOUND)
target_compile_definitions(raknet PUBLIC OPEN_SSL_CLIENT_SUPPORT)
target_link_libraries(raknet PRIVATE OpenSSL::SSL)
endif()
target_compile_options(raknet PRIVATE
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
-w>

View File

@@ -198,6 +198,7 @@ SystemAddress TCPInterface::Connect(const char* host, unsigned short remotePort,
remoteClient->systemAddress.binaryAddress=inet_addr(host);
remoteClient->systemAddress.port=remotePort;
InsertRemoteClient(remoteClient);
WaitForPendingClients();
return remoteClient->systemAddress;
}
else
@@ -223,7 +224,7 @@ void TCPInterface::StartSSLClient(SystemAddress systemAddress)
if (ctx==0)
{
SSLeay_add_ssl_algorithms();
meth = SSLv2_client_method();
meth = SSLv23_client_method();
SSL_load_error_strings();
ctx = SSL_CTX_new (meth);
RakAssert(ctx!=0);
@@ -243,12 +244,14 @@ bool TCPInterface::IsSSLActive(SystemAddress systemAddress)
#endif
void TCPInterface::Send( const char *data, unsigned length, SystemAddress systemAddress )
{
printf("TCP Send %d, %d, %p\n", isStarted, remoteClients.Size(), data);
if (isStarted==false)
return;
if (remoteClients.Size()==0)
return;
if (data==0)
return;
printf("Acquiring lock\n");
Packet *p=outgoingMessages.WriteLock();
p->length=length;
p->data = (unsigned char*) rakMalloc( p->length );
@@ -347,6 +350,7 @@ void TCPInterface::DeleteRemoteClient(RemoteClient *remoteClient, fd_set *except
void TCPInterface::InsertRemoteClient(RemoteClient* remoteClient)
{
printf("new remote client\n");
remoteClientsInsertionQueueMutex.Lock();
remoteClientsInsertionQueue.Push(remoteClient);
remoteClientsInsertionQueueMutex.Unlock();
@@ -423,14 +427,7 @@ RAK_THREAD_DECLARATION(ConnectionAttemptLoop)
tcpInterface->InsertRemoteClient(remoteClient);
// Wait for the other thread to pick up the remote client
bool isEmpty;
do
{
RakSleep(30);
tcpInterface->remoteClientsInsertionQueueMutex.Lock();
isEmpty=tcpInterface->remoteClientsInsertionQueue.IsEmpty();
tcpInterface->remoteClientsInsertionQueueMutex.Unlock();
} while(isEmpty==false && tcpInterface->threadRunning);
tcpInterface->WaitForPendingClients();
// Notify user that the connection attempt has completed.
if (tcpInterface->threadRunning)
@@ -442,6 +439,16 @@ RAK_THREAD_DECLARATION(ConnectionAttemptLoop)
return 0;
}
void TCPInterface::WaitForPendingClients() {
bool isEmpty;
do
{
RakSleep(30);
remoteClientsInsertionQueueMutex.Lock();
isEmpty=remoteClientsInsertionQueue.IsEmpty();
remoteClientsInsertionQueueMutex.Unlock();
} while(isEmpty==false && threadRunning);
}
RAK_THREAD_DECLARATION(UpdateTCPInterfaceLoop)
{
@@ -700,7 +707,7 @@ RAK_THREAD_DECLARATION(UpdateTCPInterfaceLoop)
}
#if defined(OPEN_SSL_CLIENT_SUPPORT)
void RemoteClient::InitSSL(SSL_CTX* ctx, SSL_METHOD *meth)
void RemoteClient::InitSSL(SSL_CTX* ctx, const SSL_METHOD *meth)
{
(void) meth;

View File

@@ -128,6 +128,7 @@ protected:
friend RAK_THREAD_DECLARATION(UpdateTCPInterfaceLoop);
friend RAK_THREAD_DECLARATION(ConnectionAttemptLoop);
void WaitForPendingClients();
void DeleteRemoteClient(RemoteClient *remoteClient, fd_set *exceptionFD);
void InsertRemoteClient(RemoteClient* remoteClient);
SOCKET SocketConnect(const char* host, unsigned short remotePort);
@@ -141,7 +142,7 @@ protected:
#if defined(OPEN_SSL_CLIENT_SUPPORT)
SSL_CTX* ctx;
SSL_METHOD *meth;
const SSL_METHOD *meth;
DataStructures::SingleProducerConsumer<SystemAddress> startSSL;
DataStructures::List<SystemAddress> activeSSLConnections;
#endif
@@ -160,7 +161,7 @@ struct RemoteClient
#if defined(OPEN_SSL_CLIENT_SUPPORT)
SSL* ssl;
void InitSSL(SSL_CTX* ctx, SSL_METHOD *meth);
void InitSSL(SSL_CTX* ctx, const SSL_METHOD *meth);
void DisconnectSSL(void);
void FreeSSL(void);
void Send(const char *data, unsigned int length);