some small changes

This commit is contained in:
timerix 2022-10-30 19:47:13 +06:00
parent af758fc318
commit abfbc7eaa2
11 changed files with 36 additions and 15 deletions

View File

@ -0,0 +1 @@
# kerep network

View File

@ -11,7 +11,7 @@ Maybe knIPV4Address_fromStr(char* addrStr){
c=*addrStr++;
switch (c){
case '\0':
if(i<3) {
if(i<3){
errmsg_extra="end of string";
goto default_case;
}

View File

@ -26,7 +26,6 @@ typedef struct knIPV4Endpoint {
#define knIPV4Endpoint_create(ADDR, PORT) (knIPV4Endpoint){ADDR, PORT}
#if __cplusplus
}
#endif

View File

@ -0,0 +1,21 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "knSocket.h"
typedef struct knChanneledSocket{
union {
knSocket;
knSocket base;
};
uint16 channelsAmount;
knChannel** channels;
} knChanneledSocket;
#if __cplusplus
}
#endif

View File

@ -1,3 +1,2 @@
#include "knPackage.h"

View File

@ -40,7 +40,6 @@ typedef struct knChannel {
knPackageQueueElem* queueStart;
} knChannel;
#if __cplusplus
}
#endif

View File

@ -2,7 +2,7 @@
#include "stdSocketHeaders.h"
Maybe knSocket_open(knSocketType sockType){
Maybe knSocket_open(knSocketProtocol sockType){
knSocket* newSocket=malloc(sizeof(knSocket));
newSocket->type=sockType;
newSocket->channels=NULL;
@ -13,12 +13,12 @@ Maybe knSocket_open(knSocketType sockType){
default:
safethrow("unknown socket type", free(newSocket));
break;
case knSocketType_TCP:
case knSocketProtocol_TCP:
newSocket->socketfd=socket(AF_INET, SOCK_STREAM, 0);
if(newSocket->socketfd==-1)
safethrow("can't create TCP socket", free(newSocket));
break;
case knSocketType_UDP:
case knSocketProtocol_UDP:
newSocket->socketfd=socket(AF_INET, SOCK_DGRAM, 0);
if(newSocket->socketfd==-1)
safethrow("can't create UDP socket", free(newSocket));

View File

@ -9,21 +9,21 @@ extern "C" {
#include "knPackage.h"
typedef enum __attribute__((__packed__)) knSocketType {
knSocketType_TCP, knSocketType_UDP
} knSocketType;
typedef enum __attribute__((__packed__)) knSocketProtocol {
knSocketProtocol_TCP, knSocketProtocol_UDP, knSocket_Channeled
} knSocketProtocol;
typedef struct knSocket {
knSocketType type;
uint16 channelsAmount;
knChannel** channels;
knSocketProtocol type;
int64 socketfd;
knIPV4Endpoint localEndpoint;
knIPV4Endpoint remoteEndpoint;
// uint16 channelsAmount;
// knChannel** channels;
} knSocket;
///@return Maybe<knSocket*> new socket
Maybe knSocket_open(knSocketType sockType);
Maybe knSocket_open(knSocketProtocol sockType);
///@return Maybe<void> error or nothing
Maybe knSocket_close(knSocket* socket);

View File

@ -13,6 +13,7 @@ extern "C" {
#include "knAddress.h"
#include "knPackage.h"
#include "knSocket.h"
#include "knChanneledSocket.h"
#if __cplusplus
}

View File

@ -6,6 +6,7 @@ extern "C" {
#include "network.h"
#if KN_USE_WINSOCK
#include <winsock2.h>
#else

View File

@ -33,7 +33,7 @@ void test_network(){
test_knIPV4Address_fromStr(3,3,3,128);
knSocket* s;
tryLast(knSocket_open(knSocketType_TCP), maybeS)
tryLast(knSocket_open(knSocketProtocol_TCP), maybeS)
s=maybeS.value.VoidPtr;
printf("\e[92mTCP socket created\n");
tryLast(knSocket_close(s);,_);