listener upd
This commit is contained in:
parent
4460220ada
commit
65f63bf6a8
2
cbuild
2
cbuild
@ -1 +1 @@
|
||||
Subproject commit c20e1e8f1c3e92e7f980a74942352b0a3744edcd
|
||||
Subproject commit 574ce6eab3b65e05888adad95a0211f532dd1125
|
||||
2
kerep
2
kerep
@ -1 +1 @@
|
||||
Subproject commit 95a9fcfd5e7bbe14962156a4fa4eb8d686fc4c13
|
||||
Subproject commit f2847a819d8271306cea8a841bb4b47aa36a8d73
|
||||
60
src/ListenerTCP.c
Normal file
60
src/ListenerTCP.c
Normal file
@ -0,0 +1,60 @@
|
||||
#include "port-tunnel.h"
|
||||
|
||||
void ListenerTCP_construct(ListenerTCP* ptr, knIPV4Endpoint listener_end){
|
||||
__PipeStorage_construct(&ptr->pipes);
|
||||
tryLast(knSocketTCP_open(true), m_s, ;);
|
||||
ptr->main_sock = m_s.value.VoidPtr;
|
||||
tryLast(knSocketTCP_bindAndListen(ptr->main_sock, listener_end), _m7u7, ;);
|
||||
ptr->connector_end = knIPV4Endpoint_INVALID;
|
||||
}
|
||||
|
||||
void ListenerTCP_destruct(ListenerTCP* ptr){
|
||||
__PipeStorage_destruct(&ptr->pipes);
|
||||
tryLast(knSocketTCP_close(ptr->main_sock), _m864, ;);
|
||||
}
|
||||
|
||||
|
||||
Maybe ListenerTCP_accept(ListenerTCP* ptr, knSocketTCP** connection_socket){
|
||||
char buf[65535];
|
||||
const u32 bufsize = sizeof(buf);
|
||||
InternalPacket internal_pac;
|
||||
IncomingPacket incoming_pac;
|
||||
OutgoingPacket outgoing_pac;
|
||||
try(knSocketTCP_accept(ptr->main_sock), m_cs, ;);
|
||||
*connection_socket = m_cs.value.VoidPtr;
|
||||
|
||||
try(knSocketTCP_receiveN(*connection_socket, buf, bufsize, sizeof(InternalPacket)), _m876, ;)
|
||||
|
||||
if(!InternalPacket_tryParse(buf, bufsize, &internal_pac))
|
||||
safethrow("ListenerTCP received unexpected message", ;);
|
||||
if(internal_pac.message != InternalPacketMessage_ConnectorHandshake){
|
||||
char errmsg[256];
|
||||
ksprintf(errmsg, sizeof(errmsg), "received InternalPacket with invalid data: %lu", internal_pac.message);
|
||||
safethrow(errmsg, ;)
|
||||
}
|
||||
internal_pac.data.U64 = InternalPacketMessage_ListenerHandshake;
|
||||
try(knSocketTCP_send(*connection_socket, (void*)&internal_pac, sizeof(internal_pac)), _m865, ;)
|
||||
|
||||
while(true){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void ListenerTCP_start(ListenerTCP* ptr){
|
||||
u32 connection_n = 0;
|
||||
while(true) {
|
||||
char logCtx[64];
|
||||
ksprintf(logCtx, sizeof(logCtx), "ListenerTCP/%u", connection_n++);
|
||||
knSocketTCP* connection_socket = NULL;
|
||||
Maybe m = ListenerTCP_accept(ptr, &connection_socket);
|
||||
if(m.errmsg){
|
||||
logExceptionAsWarning(logCtx, m);
|
||||
m = knSocketTCP_shutdown(connection_socket, knShutdownType_Both);
|
||||
if(m.errmsg)
|
||||
logExceptionAsError(logCtx, m);
|
||||
m = knSocketTCP_close(connection_socket);
|
||||
if(m.errmsg)
|
||||
logExceptionAsError(logCtx, m);
|
||||
}
|
||||
}
|
||||
}
|
||||
18
src/ListenerUDP.c
Normal file
18
src/ListenerUDP.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include "port-tunnel.h"
|
||||
|
||||
void ListenerUDP_construct(ListenerUDP* ptr, knIPV4Endpoint listener_end){
|
||||
__PipeStorage_construct(&ptr->pipes);
|
||||
tryLast(knSocketUDP_open(true), m_s, ;);
|
||||
ptr->main_sock = m_s.value.VoidPtr;
|
||||
tryLast(knSocketUDP_bind(ptr->main_sock, listener_end), _m7u7, ;);
|
||||
ptr->connector_end = knIPV4Endpoint_INVALID;
|
||||
}
|
||||
|
||||
void ListenerUDP_destruct(ListenerUDP* ptr){
|
||||
__PipeStorage_destruct(&ptr->pipes);
|
||||
tryLast(knSocketUDP_close(ptr->main_sock), _m864, ;);
|
||||
}
|
||||
|
||||
void ListenerUDP_start(ListenerUDP* ptr){
|
||||
|
||||
}
|
||||
103
src/Listeners.c
103
src/Listeners.c
@ -1,103 +0,0 @@
|
||||
#include "port-tunnel.h"
|
||||
|
||||
////////////////////////////////////////
|
||||
// ListenerUDP //
|
||||
////////////////////////////////////////
|
||||
|
||||
void ListenerUDP_construct(ListenerUDP* ptr, knIPV4Endpoint listener_end){
|
||||
__PipeStorage_construct(&ptr->pipes);
|
||||
tryLast(knSocketUDP_open(true), m_s, ;);
|
||||
ptr->main_sock = m_s.value.VoidPtr;
|
||||
tryLast(knSocketUDP_bind(ptr->main_sock, listener_end), _m7u7, ;);
|
||||
ptr->connector_end = knIPV4Endpoint_INVALID;
|
||||
}
|
||||
|
||||
void ListenerUDP_destruct(ListenerUDP* ptr){
|
||||
__PipeStorage_destruct(&ptr->pipes);
|
||||
tryLast(knSocketUDP_close(ptr->main_sock), _m864, ;);
|
||||
}
|
||||
|
||||
void ListenerUDP_start(ListenerUDP* ptr){
|
||||
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
// ListenerTCP //
|
||||
////////////////////////////////////////
|
||||
|
||||
void ListenerTCP_construct(ListenerTCP* ptr, knIPV4Endpoint listener_end){
|
||||
__PipeStorage_construct(&ptr->pipes);
|
||||
tryLast(knSocketTCP_open(true), m_s, ;);
|
||||
ptr->main_sock = m_s.value.VoidPtr;
|
||||
tryLast(knSocketTCP_bindAndListen(ptr->main_sock, listener_end), _m7u7, ;);
|
||||
ptr->connector_end = knIPV4Endpoint_INVALID;
|
||||
}
|
||||
|
||||
void ListenerTCP_destruct(ListenerTCP* ptr){
|
||||
__PipeStorage_destruct(&ptr->pipes);
|
||||
tryLast(knSocketTCP_close(ptr->main_sock), _m864, ;);
|
||||
}
|
||||
|
||||
/// receives data while receivedCount < N
|
||||
///@return Maybe<void>
|
||||
Maybe knSocketTCP_receiveN(knSocketTCP* socket, char* buf, u32 bufsize, u32 n){
|
||||
if(bufsize < n)
|
||||
safethrow(ERR_UNEXPECTEDVAL, ;);
|
||||
|
||||
u32 receivedTotal = 0;
|
||||
while(receivedTotal < n) {
|
||||
try(knSocketTCP_receive(socket, buf, n-receivedTotal), m_receivedCount, ;);
|
||||
receivedTotal += m_receivedCount.value.UInt64;
|
||||
}
|
||||
return MaybeNull;
|
||||
}
|
||||
|
||||
void ListenerTCP_start(ListenerTCP* ptr){
|
||||
const char logCtx[] = "ListenerTCP";
|
||||
char buf[65535];
|
||||
const u32 bufsize = sizeof(buf);
|
||||
InternalPacket internal_pac;
|
||||
IncomingPacket incoming_pac;
|
||||
OutgoingPacket outgoing_pac;
|
||||
int connection_n = 0;
|
||||
while(true) {
|
||||
Maybe m = knSocketTCP_accept(ptr->main_sock);
|
||||
if(m.errmsg){
|
||||
logExceptionAsWarning(logCtx, m);
|
||||
continue;
|
||||
}
|
||||
knSocketTCP* connection_socket = m.value.VoidPtr;
|
||||
|
||||
m = knSocketTCP_receiveN(connection_socket, buf, bufsize, sizeof(InternalPacket));
|
||||
if(m.errmsg){
|
||||
logExceptionAsWarning(logCtx, m);
|
||||
m = knSocketTCP_shutdown(connection_socket, knShutdownType_Both);
|
||||
if(m.errmsg)
|
||||
logExceptionAsWarning(logCtx, m);
|
||||
m = knSocketTCP_close(connection_socket);
|
||||
if(m.errmsg)
|
||||
logExceptionAsWarning(logCtx, m);
|
||||
continue;
|
||||
}
|
||||
if(!InternalPacket_tryParse(buf, bufsize, &internal_pac)){
|
||||
logInfo(logCtx, "ListenerTCP received something unexpected");
|
||||
m = knSocketTCP_shutdown(connection_socket, knShutdownType_Both);
|
||||
if(m.errmsg)
|
||||
logExceptionAsWarning(logCtx, m);
|
||||
m = knSocketTCP_close(connection_socket);
|
||||
if(m.errmsg)
|
||||
logExceptionAsWarning(logCtx, m);
|
||||
continue;
|
||||
}
|
||||
if(internal_pac.data.U64 != InternalPacketMessage_ConnectorHandshake){
|
||||
logInfo(logCtx, "received InternalPacket with invalid data: %lu", internal_pac.data);
|
||||
}
|
||||
|
||||
int attemptN = 0;
|
||||
while(attemptN < 20){
|
||||
// do pipes here
|
||||
}
|
||||
logWarning(logCtx, "connector is unreachable");
|
||||
}
|
||||
sleepMsec(500);
|
||||
}
|
||||
@ -7,33 +7,11 @@ extern "C" {
|
||||
#include <pthread.h>
|
||||
#include "../kerep/src/Network/network.h"
|
||||
#include "../kerep/src/Hashtable/Hashtable.h"
|
||||
#include "../kerep/src/time/time.h"
|
||||
|
||||
/// defined in help_message.c
|
||||
extern const char* help_message;
|
||||
|
||||
////////////////////////////////////////
|
||||
// Time //
|
||||
////////////////////////////////////////
|
||||
|
||||
/// nanoseconds
|
||||
typedef u64 nsec_t;
|
||||
/// microseconds
|
||||
typedef u64 usec_t;
|
||||
/// miliseconds
|
||||
typedef u64 msec_t;
|
||||
|
||||
/// system time now in nanoseconds
|
||||
///@return u64 will overflow in 13 years
|
||||
nsec_t getTimeNsec();
|
||||
|
||||
/// system time now in microseconds
|
||||
///@return u64 will overflow in 58494 years
|
||||
usec_t getTimeUsec();
|
||||
|
||||
void sleepNsec(nsec_t time);
|
||||
void sleepUsec(usec_t time);
|
||||
void sleepMsec(msec_t time);
|
||||
|
||||
////////////////////////////////////////
|
||||
// Enums //
|
||||
////////////////////////////////////////
|
||||
|
||||
38
src/time.c
38
src/time.c
@ -1,38 +0,0 @@
|
||||
#include "port-tunnel.h"
|
||||
|
||||
#define K 1000
|
||||
#define M 1000000
|
||||
#define G 1000000000
|
||||
|
||||
nsec_t getTimeNsec(){
|
||||
struct timespec t;
|
||||
if(clock_gettime(CLOCK_REALTIME, &t) != 0)
|
||||
throw(ERR_UNEXPECTEDVAL);
|
||||
u64 n = t.tv_sec * G + t.tv_nsec;
|
||||
return n;
|
||||
}
|
||||
|
||||
usec_t getTimeUsec(){
|
||||
struct timespec t;
|
||||
if(clock_gettime(CLOCK_REALTIME, &t) != 0)
|
||||
throw(ERR_UNEXPECTEDVAL);
|
||||
u64 n = t.tv_sec * M + t.tv_nsec / K;
|
||||
return n;
|
||||
}
|
||||
|
||||
void sleepNsec(nsec_t time){
|
||||
struct timespec t = {
|
||||
.tv_sec = time / G,
|
||||
.tv_nsec = time % G
|
||||
};
|
||||
if(nanosleep(&t, NULL) != 0)
|
||||
logDebug("nanosleep failed");
|
||||
}
|
||||
|
||||
void sleepUsec(usec_t time){
|
||||
sleepNsec(time * K);
|
||||
}
|
||||
|
||||
void sleepMsec(msec_t time){
|
||||
sleepNsec(time * M);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user