Reworked initial packed server side.

This commit is contained in:
Max 2023-02-02 14:33:44 +01:00
parent e48ae98860
commit 18f923c6ae
4 changed files with 67 additions and 7 deletions

View File

@ -7,11 +7,6 @@
#define PORT_NUM 42069 #define PORT_NUM 42069
struct net_data
{
char sName[100];
};
int clientstart(); int clientstart();
int start_server(); int start_server();

View File

@ -1,6 +1,7 @@
target_sources(cexp PRIVATE target_sources(cexp PRIVATE
"client.c" "client.c"
"communication.h"
"sock_helper.c" "sock_helper.c"
"server.c" "server.c"
"thread_manager.c" "thread_manager.c"

View File

@ -0,0 +1,20 @@
#ifndef COMMUNICATION_H
#define COMMUNICATION_H
#include <stdlib.h>
typedef enum msg_type
{
serverInfo,
message
} EMessageType;
typedef struct com_data
{
size_t size;
EMessageType type;
char data;
} CommData;
#endif //COMMUNICATION_H

View File

@ -6,21 +6,24 @@
#include "cexp.h" #include "cexp.h"
#include "sockets.h" #include "sockets.h"
#include "thread_manager.h" #include "thread_manager.h"
#include "communication.h"
int server_sockfd; int server_sockfd;
struct sockaddr_in server_sockaddr; struct sockaddr_in server_sockaddr;
bool b_listen = true; bool b_listen = true;
char sName[64] = "Test server Linux\0"; char sName[64] = "Test server Linux\0";
struct net_data server_data; CommData* srvInitData = NULL;
void* srvInitBuffer = NULL;
uint32_t totalBufferSize;
int setup_server(); int setup_server();
void* thread_listen_network(); void* thread_listen_network();
void* thread_client_handler(void* arg); void* thread_client_handler(void* arg);
void send_initial_pack(int client);
int start_server() int start_server()
{ {
PRINT_LINE("Starting server..."); PRINT_LINE("Starting server...");
int server_result = setup_server(); int server_result = setup_server();
if (server_result != 0) if (server_result != 0)
return server_result; return server_result;
@ -38,6 +41,7 @@ int start_server()
continue; continue;
} }
TRACE_WARN("Not a valid option given!"); TRACE_WARN("Not a valid option given!");
//TODO: Implement!
} }
return 0; return 0;
@ -126,3 +130,43 @@ void* thread_client_handler(void* arg)
return NULL; return NULL;
} }
void send_initial_pack(int client)
{
char* srvName = "Linux test server";
if (srvInitBuffer == NULL)
{
uint32_t srtLen = strlen(srvName) + 1;
totalBufferSize = sizeof(uint32_t) + sizeof(EMessageType) + srtLen;
srvInitBuffer = malloc(totalBufferSize);
if (srvInitBuffer == NULL)
{
TRACE_ERROR("Could not allocate memory!");
return;
}
*(char*)srvInitBuffer = htonl(srtLen);
*((uint32_t*)srvInitBuffer + 1) = htonl(serverInfo);
void* dataBuffer = ((char*)srvInitBuffer + sizeof(uint32_t) + sizeof(EMessageType));
if (strcpy(dataBuffer, srvName) != dataBuffer)
{
TRACE_ERROR("Could not copy data to buffer!");
free(srvInitBuffer);
srvInitBuffer = NULL;
return;
}
}
if (totalBufferSize <= 0)
return;
size_t totalDataSend = 0;
while (totalDataSend < totalBufferSize)
{
size_t sended = send(client, srvInitBuffer, totalBufferSize - totalDataSend, 0);
if (sended == -1)
{
TRACE_ERROR("Could not send data.");
break;
}
totalDataSend += sended;
}
}