knNetwork file structure changed
This commit is contained in:
parent
a55b2245bd
commit
c2b3e3997d
@ -1,5 +1,8 @@
|
|||||||
#include "knAddress.h"
|
#include "knAddress.h"
|
||||||
|
|
||||||
|
ktId_define(knIPV4Address);
|
||||||
|
ktId_define(knIPV4Endpoint);
|
||||||
|
|
||||||
Maybe knIPV4Address_fromStr(char* addrStr){
|
Maybe knIPV4Address_fromStr(char* addrStr){
|
||||||
char* addrStr_src=addrStr;
|
char* addrStr_src=addrStr;
|
||||||
char* errmsg_extra="wrong char";
|
char* errmsg_extra="wrong char";
|
||||||
@ -37,5 +40,5 @@ Maybe knIPV4Address_fromStr(char* addrStr){
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return SUCCESS(Uni(UInt64, (uint64)addr.address));
|
return SUCCESS(UniStack(knIPV4Address, addr));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,16 +13,18 @@ typedef union knIPV4Address {
|
|||||||
uint32 address;
|
uint32 address;
|
||||||
char bytes[4];
|
char bytes[4];
|
||||||
} knIPV4Address;
|
} knIPV4Address;
|
||||||
|
ktId_declare(knIPV4Address);
|
||||||
|
|
||||||
#define knIPV4Address_fromBytes(A, B, C, D) (knIPV4Address){.bytes={A,B,C,D}}
|
#define knIPV4Address_fromBytes(A, B, C, D) (knIPV4Address){.bytes={A,B,C,D}}
|
||||||
|
|
||||||
///@return Maybe<uint64> as Maybe<knIPV4Address>
|
///@return Maybe<knIPV4Address> as Maybe<knIPV4Address>
|
||||||
Maybe knIPV4Address_fromStr(char* addrStr);
|
Maybe knIPV4Address_fromStr(char* addrStr);
|
||||||
|
|
||||||
typedef struct knIPV4Endpoint {
|
typedef struct knIPV4Endpoint {
|
||||||
knIPV4Address address;
|
knIPV4Address address;
|
||||||
knPort port;
|
knPort port;
|
||||||
} knIPV4Endpoint;
|
} knIPV4Endpoint;
|
||||||
|
ktId_declare(knIPV4Endpoint);
|
||||||
|
|
||||||
#define knIPV4Endpoint_create(ADDR, PORT) (knIPV4Endpoint){ADDR, PORT}
|
#define knIPV4Endpoint_create(ADDR, PORT) (knIPV4Endpoint){ADDR, PORT}
|
||||||
|
|
||||||
|
|||||||
@ -1,21 +0,0 @@
|
|||||||
#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
|
|
||||||
@ -1,2 +0,0 @@
|
|||||||
#include "knPackage.h"
|
|
||||||
|
|
||||||
@ -1,45 +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
|
|
||||||
@ -1,91 +1,5 @@
|
|||||||
#include "knSocket.h"
|
#include "network.h"
|
||||||
#include "stdSocketHeaders.h"
|
#include "stdSocketHeaders.h"
|
||||||
|
|
||||||
|
ktId_define(knSocketProtocol);
|
||||||
Maybe knSocket_open(knSocketProtocol sockType){
|
ktId_define(knSocket);
|
||||||
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 knSocketProtocol_TCP:
|
|
||||||
newSocket->socketfd=socket(AF_INET, SOCK_STREAM, 0);
|
|
||||||
if(newSocket->socketfd==-1)
|
|
||||||
safethrow("can't create TCP socket", free(newSocket));
|
|
||||||
break;
|
|
||||||
case knSocketProtocol_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){
|
|
||||||
|
|
||||||
}*/
|
|
||||||
|
|||||||
@ -6,12 +6,12 @@ extern "C" {
|
|||||||
|
|
||||||
#include "../base/base.h"
|
#include "../base/base.h"
|
||||||
#include "knAddress.h"
|
#include "knAddress.h"
|
||||||
#include "knPackage.h"
|
|
||||||
|
|
||||||
|
|
||||||
typedef enum __attribute__((__packed__)) knSocketProtocol {
|
typedef enum __attribute__((__packed__)) knSocketProtocol {
|
||||||
knSocketProtocol_TCP, knSocketProtocol_UDP, knSocket_Channeled
|
knSocketProtocol_TCP, knSocketProtocol_UDP, knSocket_Channeled
|
||||||
} knSocketProtocol;
|
} knSocketProtocol;
|
||||||
|
ktId_declare(knSocketProtocol);
|
||||||
|
|
||||||
typedef struct knSocket {
|
typedef struct knSocket {
|
||||||
knSocketProtocol type;
|
knSocketProtocol type;
|
||||||
@ -21,6 +21,7 @@ typedef struct knSocket {
|
|||||||
// uint16 channelsAmount;
|
// uint16 channelsAmount;
|
||||||
// knChannel** channels;
|
// knChannel** channels;
|
||||||
} knSocket;
|
} knSocket;
|
||||||
|
ktId_declare(knSocket);
|
||||||
|
|
||||||
///@return Maybe<knSocket*> new socket
|
///@return Maybe<knSocket*> new socket
|
||||||
Maybe knSocket_open(knSocketProtocol sockType);
|
Maybe knSocket_open(knSocketProtocol sockType);
|
||||||
@ -28,9 +29,6 @@ Maybe knSocket_open(knSocketProtocol sockType);
|
|||||||
///@return Maybe<void> error or nothing
|
///@return Maybe<void> error or nothing
|
||||||
Maybe knSocket_close(knSocket* socket);
|
Maybe knSocket_close(knSocket* socket);
|
||||||
|
|
||||||
///@return Maybe<uint64> channel index
|
|
||||||
Maybe knSocket_createChannel(knSocket* socket);
|
|
||||||
|
|
||||||
///sets socket local endpoint
|
///sets socket local endpoint
|
||||||
///@return Maybe<void> error or nothing
|
///@return Maybe<void> error or nothing
|
||||||
Maybe knSocket_bind(knSocket* socket, knIPV4Endpoint localEndp);
|
Maybe knSocket_bind(knSocket* socket, knIPV4Endpoint localEndp);
|
||||||
|
|||||||
96
src/Network/knSocketChanneled.c
Normal file
96
src/Network/knSocketChanneled.c
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
#include "network.h"
|
||||||
|
#include "stdSocketHeaders.h"
|
||||||
|
|
||||||
|
ktId_define(knPacVersion);
|
||||||
|
ktId_define(knPackage);
|
||||||
|
ktId_define(knPackageQueueElem);
|
||||||
|
ktId_define(knChannel);
|
||||||
|
ktId_define(knSocketChanneled);
|
||||||
|
|
||||||
|
Maybe knSocketChanneled_open(knSocketProtocol sockType){
|
||||||
|
knSocketChanneled* newSocket=malloc(sizeof(knSocketChanneled));
|
||||||
|
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 knSocketProtocol_TCP:
|
||||||
|
newSocket->socketfd=socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
if(newSocket->socketfd==-1)
|
||||||
|
safethrow("can't create TCP socket", free(newSocket));
|
||||||
|
break;
|
||||||
|
case knSocketProtocol_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(knSocketChanneledPtr, newSocket));
|
||||||
|
}
|
||||||
|
|
||||||
|
Maybe knSocketChanneled_close(knSocketChanneled* 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 knSocketChanneled_createChannel(knSocketChanneled* 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(UniUInt64(channelsAmountPrev));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*Maybe knSocketChanneled_bind(knSocketChanneled* 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 knSocketChanneled_connect(knSocketChanneled* sock, knIPV4Endpoint remoteEndp){
|
||||||
|
if(sock->remoteEndpoint.address.address!=0)
|
||||||
|
safethrow("socket is connected already",;);
|
||||||
|
|
||||||
|
sock->remoteEndpoint=remoteEndp;
|
||||||
|
return SUCCESS(UniNull);
|
||||||
|
}
|
||||||
|
|
||||||
|
Maybe knSocketChanneled_accept(knSocketChanneled* sock){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Maybe knSocketChanneled_send(knSocketChanneled* sock, uint16 destinationIndex, uint8* data, uint32 dataLength){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Maybe knSocketChanneled_recieve(knSocketChanneled* sock, uint16 destinationIndex, uint8* buffer, uint32 bufferLength){
|
||||||
|
|
||||||
|
}*/
|
||||||
89
src/Network/knSocketChanneled.h
Normal file
89
src/Network/knSocketChanneled.h
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "../base/base.h"
|
||||||
|
#include "knSocket.h"
|
||||||
|
|
||||||
|
#define KNPAC_MAX_DATA_SIZE (65535-sizeof(knPackage)+sizeof(uint8*))
|
||||||
|
|
||||||
|
|
||||||
|
typedef enum __attribute__((__packed__)) knPacVersion {
|
||||||
|
knPac_V1
|
||||||
|
} knPacVersion;
|
||||||
|
ktId_declare(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;
|
||||||
|
ktId_declare(knPackage);
|
||||||
|
|
||||||
|
typedef struct knPackageQueueElem knPackageQueueElem;
|
||||||
|
struct knPackageQueueElem {
|
||||||
|
union {
|
||||||
|
knPackage;
|
||||||
|
knPackage package;
|
||||||
|
};
|
||||||
|
knPackageQueueElem* previousElem;
|
||||||
|
knPackageQueueElem* nextElem;
|
||||||
|
};
|
||||||
|
ktId_declare(knPackageQueueElem);
|
||||||
|
|
||||||
|
typedef struct knChannel {
|
||||||
|
knPackageQueueElem* queueStart;
|
||||||
|
} knChannel;
|
||||||
|
ktId_declare(knChannel);
|
||||||
|
|
||||||
|
typedef struct knSocketChanneled{
|
||||||
|
union {
|
||||||
|
knSocket;
|
||||||
|
knSocket base;
|
||||||
|
};
|
||||||
|
uint16 channelsAmount;
|
||||||
|
knChannel** channels;
|
||||||
|
} knSocketChanneled;
|
||||||
|
ktId_declare(knSocketChanneled);
|
||||||
|
|
||||||
|
|
||||||
|
///@return Maybe<knSocket*> new socket
|
||||||
|
Maybe knSocketChanneled_open(knSocketProtocol sockType);
|
||||||
|
|
||||||
|
///@return Maybe<void> error or nothing
|
||||||
|
Maybe knSocketChanneled_close(knSocket* socket);
|
||||||
|
|
||||||
|
///@return Maybe<uint64> channel index
|
||||||
|
Maybe knSocketChanneled_createChannel(knSocket* socket);
|
||||||
|
|
||||||
|
///sets socket local endpoint
|
||||||
|
///@return Maybe<void> error or nothing
|
||||||
|
Maybe knSocketChanneled_bind(knSocket* socket, knIPV4Endpoint localEndp);
|
||||||
|
|
||||||
|
///sets socket remote endpoint
|
||||||
|
///@return Maybe<void> error or nothing
|
||||||
|
Maybe knSocketChanneled_connect(knSocket* socket, knIPV4Endpoint remoteEndp);
|
||||||
|
|
||||||
|
///@return Maybe<knSocket*> new socket connected to client
|
||||||
|
Maybe knSocketChanneled_accept(knSocket* socket);
|
||||||
|
|
||||||
|
///@param dataLength 0-4294967295
|
||||||
|
///@return Maybe<void>
|
||||||
|
Maybe knSocketChanneled_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 knSocketChanneled_recieve(knSocket* socket, uint16 destinationIndex, uint8* buffer, uint32 bufferLength);
|
||||||
|
|
||||||
|
#if __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@ -11,9 +11,8 @@ extern "C" {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "knAddress.h"
|
#include "knAddress.h"
|
||||||
#include "knPackage.h"
|
|
||||||
#include "knSocket.h"
|
#include "knSocket.h"
|
||||||
#include "knChanneledSocket.h"
|
#include "knSocketChanneled.h"
|
||||||
|
|
||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,9 +4,6 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "network.h"
|
|
||||||
|
|
||||||
|
|
||||||
#if KN_USE_WINSOCK
|
#if KN_USE_WINSOCK
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#else
|
#else
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user