wip: Http Client experiments

This commit is contained in:
Xiphoseer 2024-05-01 23:41:59 +02:00
parent 776a9c36a4
commit b78ece8919
5 changed files with 62 additions and 13 deletions

View File

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

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

@ -72,8 +72,14 @@ Source/RakThread.h Source/SuperFastHash.h S
Source/HTTPConnection.h Kbhit.h Source/HTTPConnection.h Kbhit.h
) )
add_library(raknet STATIC ${RAKNET_SOURCES}) 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 target_compile_options(raknet PRIVATE
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>: $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
-w> -w>

View File

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

View File

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