Start doing TCP networking
This commit is contained in:
@@ -7,6 +7,11 @@
|
||||
#include <block/chain.h>
|
||||
#include <block/block.h>
|
||||
|
||||
// Nets
|
||||
#define MAX_CONS 32 // Some baseline for now
|
||||
#define LISTEN_PORT 9393
|
||||
|
||||
// Economics
|
||||
#define DECIMALS 1000000000000ULL
|
||||
#define DIFFICULTY_ADJUSTMENT_INTERVAL 3840 // Every 3840 blocks (roughly every 4 days with a 90 second block time)
|
||||
// Max adjustment per is x2. So if blocks are coming in too fast, the difficulty will at most double every 24 hours, and vice versa if they're coming in too slow.
|
||||
|
||||
29
include/nets/net_node.h
Normal file
29
include/nets/net_node.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef NET_NODE_H
|
||||
#define NET_NODE_H
|
||||
|
||||
#ifndef _WIN32
|
||||
// POSIX
|
||||
#include <tcpd/tcpconnection.h>
|
||||
#include <tcpd/tcpserver.h>
|
||||
#endif
|
||||
|
||||
#include <dynarr.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <constants.h>
|
||||
|
||||
typedef struct {
|
||||
tcp_server_t* server;
|
||||
// TODO: Add the list of clients as well
|
||||
} net_node_t;
|
||||
|
||||
net_node_t* Node_Create();
|
||||
void Node_Destroy(net_node_t* node);
|
||||
|
||||
// Callback logic
|
||||
void Node_Server_OnConnect(tcp_connection_t* client);
|
||||
void Node_Server_OnData(tcp_connection_t* client);
|
||||
void Node_Server_OnDisconnect(tcp_connection_t* client);
|
||||
|
||||
#endif
|
||||
@@ -11,19 +11,19 @@
|
||||
|
||||
#define MTU 1500
|
||||
|
||||
struct TcpClient {
|
||||
struct tcp_connection_t {
|
||||
int clientFd;
|
||||
struct sockaddr_in clientAddr;
|
||||
uint32_t clientId;
|
||||
|
||||
unsigned char dataBuf[MTU];
|
||||
ssize_t dataBufLen;
|
||||
void (*on_data)(struct TcpClient* client);
|
||||
void (*on_disconnect)(struct TcpClient* client);
|
||||
void (*on_data)(struct tcp_connection_t* client);
|
||||
void (*on_disconnect)(struct tcp_connection_t* client);
|
||||
|
||||
pthread_t clientThread;
|
||||
};
|
||||
|
||||
typedef struct TcpClient TcpClient;
|
||||
typedef struct tcp_connection_t tcp_connection_t;
|
||||
|
||||
#endif
|
||||
@@ -10,7 +10,7 @@
|
||||
#include <pthread.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <tcpd/tcpclient.h>
|
||||
#include <tcpd/tcpconnection.h>
|
||||
#include <numgen.h>
|
||||
#include <dynarr.h>
|
||||
|
||||
@@ -20,37 +20,37 @@ typedef struct {
|
||||
int opt;
|
||||
|
||||
// Called before the client thread runs
|
||||
void (*on_connect)(TcpClient* client);
|
||||
void (*on_connect)(tcp_connection_t* client);
|
||||
// Called when data is received
|
||||
void (*on_data)(TcpClient* client);
|
||||
void (*on_data)(tcp_connection_t* client);
|
||||
// Called before the socket and client thread are killed; Do NOT free client manually
|
||||
void (*on_disconnect)(TcpClient* client);
|
||||
void (*on_disconnect)(tcp_connection_t* client);
|
||||
|
||||
// max clients
|
||||
size_t clients;
|
||||
TcpClient** clientsArrPtr;
|
||||
tcp_connection_t** clientsArrPtr;
|
||||
|
||||
pthread_t svrThread;
|
||||
} TcpServer;
|
||||
} tcp_server_t;
|
||||
|
||||
struct tcpclient_thread_args {
|
||||
TcpClient* clientPtr;
|
||||
TcpServer* serverPtr;
|
||||
tcp_connection_t* clientPtr;
|
||||
tcp_server_t* serverPtr;
|
||||
};
|
||||
|
||||
typedef struct tcpclient_thread_args tcpclient_thread_args;
|
||||
|
||||
TcpServer* TcpServer_Create();
|
||||
void TcpServer_Destroy(TcpServer* ptr);
|
||||
tcp_server_t* TcpServer_Create();
|
||||
void TcpServer_Destroy(tcp_server_t* ptr);
|
||||
|
||||
void TcpServer_Init(TcpServer* ptr, unsigned short port, const char* addr);
|
||||
void TcpServer_Start(TcpServer* ptr, int maxcons);
|
||||
void TcpServer_Stop(TcpServer* ptr);
|
||||
void TcpServer_Send(TcpServer* ptr, TcpClient* cli, void* data, size_t len);
|
||||
void TcpServer_Init(tcp_server_t* ptr, unsigned short port, const char* addr);
|
||||
void TcpServer_Start(tcp_server_t* ptr, int maxcons);
|
||||
void TcpServer_Stop(tcp_server_t* ptr);
|
||||
void TcpServer_Send(tcp_server_t* ptr, tcp_connection_t* cli, void* data, size_t len);
|
||||
void Generic_SendSocket(int sock, void* data, size_t len);
|
||||
void TcpServer_Disconnect(TcpServer* ptr, TcpClient* cli);
|
||||
void TcpServer_KillClient(TcpServer* ptr, TcpClient* cli);
|
||||
void TcpServer_Disconnect(tcp_server_t* ptr, tcp_connection_t* cli);
|
||||
void TcpServer_KillClient(tcp_server_t* ptr, tcp_connection_t* cli);
|
||||
|
||||
size_t Generic_FindClientInArrayByPtr(TcpClient** arr, TcpClient* ptr, size_t len);
|
||||
size_t Generic_FindClientInArrayByPtr(tcp_connection_t** arr, tcp_connection_t* ptr, size_t len);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user