Network/* structure changed
This commit is contained in:
parent
d96bebaf45
commit
4e162bd474
@ -1,4 +1,4 @@
|
||||
#include "network.h"
|
||||
#include "knAddress.h"
|
||||
|
||||
Maybe knIPV4Address_fromStr(char* addrStr){
|
||||
char* addrStr_src=addrStr;
|
||||
@ -31,7 +31,7 @@ Maybe knIPV4Address_fromStr(char* addrStr){
|
||||
default:
|
||||
uint32 errmsgL=cptr_length(addrStr) + 80;
|
||||
char* errmsg=malloc(errmsgL);
|
||||
IFWIN(sprintf_s(errmsg, errmsgL, "wrong ip address string: %s\n %s", addrStr_src, errmsg_extra),
|
||||
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;
|
||||
32
src/Network/knAddress.h
Normal file
32
src/Network/knAddress.h
Normal file
@ -0,0 +1,32 @@
|
||||
#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 __attribute__((__packed__)) knIPV4Endpoint {
|
||||
knIPV4Address address;
|
||||
knPort port;
|
||||
} knIPV4Endpoint;
|
||||
|
||||
#define knIPV4Endpoint_create(ADDR, PORT) (knIPV4Endpoint){ADDR, PORT}
|
||||
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
3
src/Network/knPackage.c
Normal file
3
src/Network/knPackage.c
Normal file
@ -0,0 +1,3 @@
|
||||
#include "knPackage.h"
|
||||
|
||||
|
||||
46
src/Network/knPackage.h
Normal file
46
src/Network/knPackage.h
Normal file
@ -0,0 +1,46 @@
|
||||
#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 +1,42 @@
|
||||
#include "network.h"
|
||||
#include "knSocket.h"
|
||||
#include "stdSocketHeaders.h"
|
||||
|
||||
|
||||
Maybe knSocket_open(knSockType 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 knSockType_TCP:
|
||||
newSocket->socketfd=socket(AF_INET, SOCK_STREAM, 0);
|
||||
if(newSocket->socketfd==-1)
|
||||
safethrow("can't create TCP socket", free(newSocket));
|
||||
break;
|
||||
case knSockType_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);
|
||||
}
|
||||
57
src/Network/knSocket.h
Normal file
57
src/Network/knSocket.h
Normal file
@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../base/base.h"
|
||||
#include "knAddress.h"
|
||||
#include "knPackage.h"
|
||||
|
||||
|
||||
typedef enum __attribute__((__packed__)) knSockType {
|
||||
knSockType_TCP, knSockType_UDP
|
||||
} knSockType;
|
||||
|
||||
typedef struct knSocket {
|
||||
knSockType type;
|
||||
uint16 channelsAmount;
|
||||
knChannel* channels;
|
||||
int64 socketfd;
|
||||
knIPV4Endpoint localEndpoint;
|
||||
knIPV4Endpoint remoteEndpoint;
|
||||
} knSocket;
|
||||
|
||||
///@return Maybe<knSocket*> new socket
|
||||
Maybe knSocket_open(knSockType 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
|
||||
@ -4,112 +4,15 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../Hashtable/Hashtable.h"
|
||||
#include "../base/base.h"
|
||||
|
||||
|
||||
#if defined(_MSC_VER) || defined(_WIN64) || defined(_WIN32) || 1
|
||||
#if defined(_MSC_VER) || defined(_WIN64) || defined(_WIN32) || 0
|
||||
#define KN_USE_WINSOCK 1
|
||||
#endif
|
||||
|
||||
#if KN_USE_WINSOCK
|
||||
#include "winsock.h"
|
||||
#else
|
||||
#include "../Hashtable/Hashtable.h"
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#endif
|
||||
|
||||
#define KNPAC_MAX_DATA_SIZE (65535-sizeof(knPackage)+sizeof(uint8*))
|
||||
|
||||
|
||||
typedef enum __attribute__((__packed__)) knPacVersion {
|
||||
knPac_V1
|
||||
} knPacVersion;
|
||||
|
||||
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 package_num; // number in sequence of sent packages
|
||||
uint32 destination_hash; // hash32 of knDestination.name
|
||||
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 knDestination {
|
||||
knPackageQueueElem* queueStart;
|
||||
|
||||
} knDestination;
|
||||
|
||||
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 uint16 knPort;
|
||||
|
||||
typedef struct __attribute__((__packed__)) knIPV4Endpoint {
|
||||
knIPV4Address address;
|
||||
knPort port;
|
||||
} knIPV4Endpoint;
|
||||
|
||||
#define knIPV4Endpoint_create(ADDR, PORT) (knIPV4Endpoint){ADDR, PORT}
|
||||
|
||||
typedef enum __attribute__((__packed__)) knSockType {
|
||||
knSockType_TCP, knSockType_UDP
|
||||
} knSockType;
|
||||
|
||||
typedef struct knSocket {
|
||||
knDestination* destinations;
|
||||
int socketfd;
|
||||
knSockType type;
|
||||
knIPV4Endpoint localEndpoint;
|
||||
knIPV4Endpoint remoteEndpoint;
|
||||
} knSocket;
|
||||
|
||||
///@returns Maybe<knSocket*> new socket
|
||||
Maybe knSocket_open(knSockType sockType);
|
||||
|
||||
///@return Maybe<void> error or nothing
|
||||
Maybe knSocket_close(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, 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, uint8* buffer, uint32 bufferLength);
|
||||
|
||||
#include "knAddress.h"
|
||||
#include "knPackage.h"
|
||||
#include "knSocket.h"
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
|
||||
21
src/Network/stdSocketHeaders.h
Normal file
21
src/Network/stdSocketHeaders.h
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "network.h"
|
||||
|
||||
#if KN_USE_WINSOCK
|
||||
#include "winsock.h"
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#endif
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
@ -11,6 +11,7 @@ extern "C" {
|
||||
#include <locale.h>
|
||||
#include <time.h>
|
||||
#include <setjmp.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
#define dbg(N) printf("\e[95m%d\n",N)
|
||||
|
||||
@ -24,9 +24,9 @@ typedef enum __attribute__((__packed__)) my_type {
|
||||
UniversalType,
|
||||
AutoarrInt8Ptr, AutoarrUInt8Ptr, AutoarrInt16Ptr, AutoarrUInt16Ptr,
|
||||
AutoarrInt32Ptr, AutoarrUInt32Ptr, AutoarrInt64Ptr, AutoarrUInt64Ptr,
|
||||
AutoarrUnitypePtr, AutoarrKVPairPtr
|
||||
AutoarrUnitypePtr, AutoarrKVPairPtr, knSocketPtr
|
||||
} my_type;
|
||||
#define my_type_last AutoarrKVPairPtr
|
||||
#define my_type_last knSocketPtr
|
||||
|
||||
const char* my_type_name(my_type t);
|
||||
|
||||
|
||||
@ -22,8 +22,16 @@ void test_network(){
|
||||
printf("\e[96m------------[test_network]------------\n");
|
||||
if(sizeof(knIPV4Endpoint)!=sizeof(knPort)+sizeof(knIPV4Address))
|
||||
throw(ERR_WRONGTYPE);
|
||||
|
||||
test_knIPV4Address_fromStr(127,0,0,1);
|
||||
test_knIPV4Address_fromStr(34,255,45,0);
|
||||
test_knIPV4Address_fromStr(3,3,3,128);
|
||||
|
||||
knSocket* s;
|
||||
tryLast(knSocket_open(knSockType_TCP), maybeS)
|
||||
s=maybeS.value.VoidPtr;
|
||||
printf("\e[92mTCP socket created\n");
|
||||
tryLast(knSocket_close(s);,_);
|
||||
printf("\e[92mTCP socket closed\n");
|
||||
}));
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user