mirror of
https://github.com/hmaxnl/netex.git
synced 2024-11-10 03:04:19 +01:00
Reworked initial packed server side.
This commit is contained in:
parent
e48ae98860
commit
18f923c6ae
|
@ -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();
|
||||||
|
|
||||||
|
|
|
@ -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"
|
||||||
|
|
20
src/sockets/communication.h
Normal file
20
src/sockets/communication.h
Normal 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
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user