Delete src/Network directory

This commit is contained in:
Timerix22 2022-08-24 13:48:48 +06:00 committed by GitHub
parent af758fc318
commit 16dcadb3f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 0 additions and 311 deletions

View File

View File

@ -1,41 +0,0 @@
#include "knAddress.h"
Maybe knIPV4Address_fromStr(char* addrStr){
char* addrStr_src=addrStr;
char* errmsg_extra="wrong char";
uint8 c;
knIPV4Address addr;
addr.address=0;
uint16 n=0;
for(uint8 i=0; i<4; ){
c=*addrStr++;
switch (c){
case '\0':
if(i<3) {
errmsg_extra="end of string";
goto default_case;
}
case '.':
addr.bytes[i++]=n;
n=0;
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
n=n*10+c-'0';
if(n>255) {
errmsg_extra="one part of address > 255";
goto default_case;
}
break;
default_case:
default:
uint32 errmsgL=cptr_length(addrStr) + 80;
char* errmsg=malloc(errmsgL);
IFMSC(sprintf_s(errmsg, errmsgL, "wrong ip address string: %s\n %s", addrStr_src, errmsg_extra),
sprintf( errmsg, "wrong ip address string: %s\n %s", addrStr_src, errmsg_extra));
safethrow(errmsg,;);
break;
}
}
return SUCCESS(Uni(UInt64, (uint64)addr.address));
}

View File

@ -1,32 +0,0 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "../base/base.h"
typedef uint16 knPort;
typedef union knIPV4Address {
uint32 address;
char bytes[4];
} knIPV4Address;
#define knIPV4Address_fromBytes(A, B, C, D) (knIPV4Address){.bytes={A,B,C,D}}
///@return Maybe<uint64> as Maybe<knIPV4Address>
Maybe knIPV4Address_fromStr(char* addrStr);
typedef struct knIPV4Endpoint {
knIPV4Address address;
knPort port;
} knIPV4Endpoint;
#define knIPV4Endpoint_create(ADDR, PORT) (knIPV4Endpoint){ADDR, PORT}
#if __cplusplus
}
#endif

View File

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

View File

@ -1,46 +0,0 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "../base/base.h"
#define KNPAC_MAX_DATA_SIZE (65535-sizeof(knPackage)+sizeof(uint8*))
typedef enum __attribute__((__packed__)) knPacVersion {
knPac_V1
} knPacVersion;
static const char knPacHeader[5]={'k','n','p','a','c'};
typedef struct knPackage {
char header[5]; // knpac
knPacVersion version; // protocol version
uint16 data_size; // size of data block in bytes (1-KNPAC_MAX_DATA_SIZE)
uint32 channel_id; // id of knChannel in socket
uint32 package_num; // number in sequence of sent packages
uint64 data_hash; // hash64 of data
uint8* data; // ptr to data
} knPackage;
typedef struct knPackageQueueElem knPackageQueueElem;
struct knPackageQueueElem {
union {
knPackage;
knPackage package;
};
knPackageQueueElem* previousElem;
knPackageQueueElem* nextElem;
};
typedef struct knChannel {
knPackageQueueElem* queueStart;
} knChannel;
#if __cplusplus
}
#endif

View File

@ -1,91 +0,0 @@
#include "knSocket.h"
#include "stdSocketHeaders.h"
Maybe knSocket_open(knSocketType sockType){
knSocket* newSocket=malloc(sizeof(knSocket));
newSocket->type=sockType;
newSocket->channels=NULL;
newSocket->channelsAmount=0;
newSocket->localEndpoint=knIPV4Endpoint_create(knIPV4Address_fromBytes(0,0,0,0),0);
newSocket->remoteEndpoint=newSocket->localEndpoint;
switch(sockType){
default:
safethrow("unknown socket type", free(newSocket));
break;
case knSocketType_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:
newSocket->socketfd=socket(AF_INET, SOCK_DGRAM, 0);
if(newSocket->socketfd==-1)
safethrow("can't create UDP socket", free(newSocket));
break;
}
return SUCCESS(UniPtr(knSocketPtr, newSocket));
}
Maybe knSocket_close(knSocket* knsocket){
int rezult=
#if KN_USE_WINSOCK
closesocket
#else
close
#endif
(knsocket->socketfd);
if(rezult==-1) {
safethrow("can't close socket",;);
}
else return SUCCESS(UniNull);
}
knChannel* __createChannel(){
knChannel* ch=malloc(sizeof(knChannel));
ch->queueStart=NULL;
return ch;
}
Maybe knSocket_createChannel(knSocket* sock){
if(sock->channelsAmount == 65535)
safethrow("max amount of channels",;);
uint16 channelsAmountPrev=sock->channelsAmount;
sock->channelsAmount++;
if(channelsAmountPrev==0)
sock->channels=malloc(sizeof(knChannel*));
else
sock->channels=realloc(sock->channels, sock->channelsAmount*sizeof(knChannel*));
sock->channels[channelsAmountPrev]=__createChannel();
return SUCCESS(Uni(UInt64, channelsAmountPrev));
}
/*Maybe knSocket_bind(knSocket* sock, knIPV4Endpoint localEndp){
if(sock->localEndpoint.address.address!=0)
safethrow("socket is bound already",;);
struct sockaddr_in addr;
addr.sin_family=
bind(sock->socketfd);
sock->localEndpoint=localEndp;
return SUCCESS(UniNull);
}
Maybe knSocket_connect(knSocket* sock, knIPV4Endpoint remoteEndp){
if(sock->remoteEndpoint.address.address!=0)
safethrow("socket is connected already",;);
sock->remoteEndpoint=remoteEndp;
return SUCCESS(UniNull);
}
Maybe knSocket_accept(knSocket* sock){
}
Maybe knSocket_send(knSocket* sock, uint16 destinationIndex, uint8* data, uint32 dataLength){
}
Maybe knSocket_recieve(knSocket* sock, uint16 destinationIndex, uint8* buffer, uint32 bufferLength){
}*/

View File

@ -1,57 +0,0 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "../base/base.h"
#include "knAddress.h"
#include "knPackage.h"
typedef enum __attribute__((__packed__)) knSocketType {
knSocketType_TCP, knSocketType_UDP
} knSocketType;
typedef struct knSocket {
knSocketType type;
uint16 channelsAmount;
knChannel** channels;
int64 socketfd;
knIPV4Endpoint localEndpoint;
knIPV4Endpoint remoteEndpoint;
} knSocket;
///@return Maybe<knSocket*> new socket
Maybe knSocket_open(knSocketType sockType);
///@return Maybe<void> error or nothing
Maybe knSocket_close(knSocket* socket);
///@return Maybe<uint64> channel index
Maybe knSocket_createChannel(knSocket* socket);
///sets socket local endpoint
///@return Maybe<void> error or nothing
Maybe knSocket_bind(knSocket* socket, knIPV4Endpoint localEndp);
///sets socket remote endpoint
///@return Maybe<void> error or nothing
Maybe knSocket_connect(knSocket* socket, knIPV4Endpoint remoteEndp);
///@return Maybe<knSocket*> new socket connected to client
Maybe knSocket_accept(knSocket* socket);
///@param dataLength 0-4294967295
///@return Maybe<void>
Maybe knSocket_send(knSocket* socket, uint16 destinationIndex, uint8* data, uint32 dataLength);
///@param buffer buffer for recieving data
///@param bufferLength 0-4294967295
///@return Maybe<uint64> recieved bytes amount
Maybe knSocket_recieve(knSocket* socket, uint16 destinationIndex, uint8* buffer, uint32 bufferLength);
#if __cplusplus
}
#endif

View File

@ -1,19 +0,0 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "../base/base.h"
#if defined(_MSC_VER) || defined(_WIN64) || defined(_WIN32) || 0
#define KN_USE_WINSOCK 1
#endif
#include "knAddress.h"
#include "knPackage.h"
#include "knSocket.h"
#if __cplusplus
}
#endif

View File

@ -1,22 +0,0 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "network.h"
#if KN_USE_WINSOCK
#include <winsock2.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#endif
#if __cplusplus
}
#endif