fixed typos
This commit is contained in:
parent
6135f3030f
commit
1b93c0ca22
@ -1 +1,4 @@
|
|||||||
# kerep network
|
# kerep network
|
||||||
|
|
||||||
|
Don't forget to call `kn_tryInit()` before doint anything with network.
|
||||||
|
|
||||||
27
src/Network/network.c
Normal file
27
src/Network/network.c
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#include "network.h"
|
||||||
|
#include "stdSocketHeaders.h"
|
||||||
|
|
||||||
|
Maybe kn_tryInit(){
|
||||||
|
#if _WIN32
|
||||||
|
// Initialize Winsock
|
||||||
|
WSADATA wsaData = {0};
|
||||||
|
int startupResult = WSAStartup(MAKEWORD(2,2), &wsaData);
|
||||||
|
if (startupResult != 0) {
|
||||||
|
char* errcode = toString_hex(&startupResult, sizeof(int), 0 , 1, 1);
|
||||||
|
safethrow(cptr_concat("WSAStartup failed with error: ", errcode), ;);
|
||||||
|
}
|
||||||
|
return SUCCESS(UniNull);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
Maybe kt_tryDispose(){
|
||||||
|
#if _WIN32
|
||||||
|
// Deinitialize Winsock
|
||||||
|
int cleanupResult = WSACleanup();
|
||||||
|
if (cleanupResult != 0) {
|
||||||
|
char* errcode = toString_hex(&cleanupResult, sizeof(int), 0, 1, 1);
|
||||||
|
safethrow(cptr_concat("WSAStartup failed with error: ", errcode), ;);
|
||||||
|
}
|
||||||
|
return SUCCESS(UniNull);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
@ -11,6 +11,9 @@ extern "C" {
|
|||||||
#include "sockets/knSocketUDP.h"
|
#include "sockets/knSocketUDP.h"
|
||||||
#include "sockets/knSocketChanneled.h"
|
#include "sockets/knSocketChanneled.h"
|
||||||
|
|
||||||
|
Maybe kn_tryInit();
|
||||||
|
Maybe kt_tryDispose();
|
||||||
|
|
||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -56,4 +56,4 @@ Maybe knSocketChanneled_accept(knSocketChanneled* socket);
|
|||||||
|
|
||||||
Maybe knSocketChanneled_send(knSocketChanneled* socket, u16 destinationIndex, u8* data, u32 dataLength);
|
Maybe knSocketChanneled_send(knSocketChanneled* socket, u16 destinationIndex, u8* data, u32 dataLength);
|
||||||
|
|
||||||
Maybe knSocketChanneled_recieve(knSocketChanneled* socket, u16 destinationIndex, u8* buffer, u32 bufferLength);
|
Maybe knSocketChanneled_receive(knSocketChanneled* socket, u16 destinationIndex, u8* buffer, u32 bufferLength);
|
||||||
@ -76,8 +76,8 @@ Maybe knSocketChanneled_send(knSocketChanneled* socket, u16 destinationIndex, u8
|
|||||||
|
|
||||||
///@param buffer buffer for recieving data
|
///@param buffer buffer for recieving data
|
||||||
///@param bufferLength 0-4294967295
|
///@param bufferLength 0-4294967295
|
||||||
///@return Maybe<u64> recieved bytes amount
|
///@return Maybe<u64> received bytes amount
|
||||||
Maybe knSocketChanneled_recieve(knSocketChanneled* socket, u16 destinationIndex, u8* buffer, u32 bufferLength);
|
Maybe knSocketChanneled_receive(knSocketChanneled* socket, u16 destinationIndex, u8* buffer, u32 bufferLength);
|
||||||
|
|
||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,6 +10,7 @@ Maybe knSocketTCP_open(){
|
|||||||
newSocket->socketfd=socket(AF_INET, SOCK_STREAM, 0);
|
newSocket->socketfd=socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if(newSocket->socketfd==-1)
|
if(newSocket->socketfd==-1)
|
||||||
safethrow("can't create TCP socket", free(newSocket));
|
safethrow("can't create TCP socket", free(newSocket));
|
||||||
|
|
||||||
return SUCCESS(UniHeapPtr(knSocketTCP, newSocket));
|
return SUCCESS(UniHeapPtr(knSocketTCP, newSocket));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,5 +70,5 @@ Maybe knSocketTCP_send(knSocketTCP* socket, char* data, u32 dataLength);
|
|||||||
|
|
||||||
///@param buffer buffer for recieving data
|
///@param buffer buffer for recieving data
|
||||||
///@param bufferLength 0-4294967295
|
///@param bufferLength 0-4294967295
|
||||||
///@return Maybe<u64> recieved bytes amount
|
///@return Maybe<u64> received bytes amount
|
||||||
Maybe knSocketTCP_recieve(knSocketTCP* socket, char* buffer, u32 bufferLength);
|
Maybe knSocketTCP_receive(knSocketTCP* socket, char* buffer, u32 bufferLength);
|
||||||
|
|||||||
@ -37,8 +37,8 @@ Maybe knSocketTCP_send(knSocketTCP* socket, char* data, u32 dataLength);
|
|||||||
|
|
||||||
///@param buffer buffer for recieving data
|
///@param buffer buffer for recieving data
|
||||||
///@param bufferLength 0-4294967295
|
///@param bufferLength 0-4294967295
|
||||||
///@return Maybe<u64> recieved bytes amount
|
///@return Maybe<u64> received bytes amount
|
||||||
Maybe knSocketTCP_recieve(knSocketTCP* socket, char* buffer, u32 bufferLength);
|
Maybe knSocketTCP_receive(knSocketTCP* socket, char* buffer, u32 bufferLength);
|
||||||
|
|
||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,6 +9,7 @@ Maybe knSocketUDP_open(){
|
|||||||
newSocket->socketfd=socket(AF_INET, SOCK_DGRAM, 0);
|
newSocket->socketfd=socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
if(newSocket->socketfd==-1)
|
if(newSocket->socketfd==-1)
|
||||||
safethrow("can't create UDP socket", free(newSocket));
|
safethrow("can't create UDP socket", free(newSocket));
|
||||||
|
|
||||||
return SUCCESS(UniHeapPtr(knSocketUDP, newSocket));
|
return SUCCESS(UniHeapPtr(knSocketUDP, newSocket));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,5 +26,5 @@ Maybe knSocketUDP_sendto(knSocketUDP* socket, char* data, u32 dataLength, knIPV4
|
|||||||
|
|
||||||
///@param buffer buffer for recieving data
|
///@param buffer buffer for recieving data
|
||||||
///@param bufferLength 0-4294967295
|
///@param bufferLength 0-4294967295
|
||||||
///@return Maybe<u64> recieved bytes amount
|
///@return Maybe<u64> received bytes amount
|
||||||
Maybe knSocketUDP_recieve(knSocketUDP* socket, char* buffer, u32 bufferLength);
|
Maybe knSocketUDP_receive(knSocketUDP* socket, char* buffer, u32 bufferLength);
|
||||||
|
|||||||
@ -31,8 +31,8 @@ Maybe knSocketUDP_sendto(knSocketUDP* socket, char* data, u32 dataLength, knIPV4
|
|||||||
|
|
||||||
///@param buffer buffer for recieving data
|
///@param buffer buffer for recieving data
|
||||||
///@param bufferLength 0-4294967295
|
///@param bufferLength 0-4294967295
|
||||||
///@return Maybe<u64> recieved bytes amount
|
///@return Maybe<u64> received bytes amount
|
||||||
Maybe knSocketUDP_recieve(knSocketUDP* socket, char* buffer, u32 bufferLength);
|
Maybe knSocketUDP_receive(knSocketUDP* socket, char* buffer, u32 bufferLength);
|
||||||
|
|
||||||
|
|
||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
|
|||||||
@ -39,8 +39,8 @@ Maybe __next_toString(kp_fmt f, void* object){
|
|||||||
}
|
}
|
||||||
|
|
||||||
Maybe check_argsN(u8 n){
|
Maybe check_argsN(u8 n){
|
||||||
if(n%2 != 0) safethrow("kprint recieved non-even number of arguments",;);
|
if(n%2 != 0) safethrow("kprint received non-even number of arguments",;);
|
||||||
if(n > 32) safethrow("kprint recieved >32 number of arguments",;);
|
if(n > 32) safethrow("kprint received >32 number of arguments",;);
|
||||||
return MaybeNull;
|
return MaybeNull;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,8 @@ i32 main(){
|
|||||||
kt_beginInit();
|
kt_beginInit();
|
||||||
kt_initKerepTypes();
|
kt_initKerepTypes();
|
||||||
kt_endInit();
|
kt_endInit();
|
||||||
test_all();
|
// test_all();
|
||||||
|
test_network();
|
||||||
kt_free();
|
kt_free();
|
||||||
kprintf("\e[37m\n");
|
kprintf("\e[37m\n");
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@ -22,24 +22,33 @@ void __test_knIPV4Address_fromStr(char* addrStr, u8 a, u8 b, u8 c, u8 d){
|
|||||||
void test_network(){
|
void test_network(){
|
||||||
optime(__func__,1,({
|
optime(__func__,1,({
|
||||||
kprintf("\e[96m------------[test_network]------------\n");
|
kprintf("\e[96m------------[test_network]------------\n");
|
||||||
|
tryLast(kn_tryInit(), _mjj64g, ;);
|
||||||
|
kprintf("\e[92m\nkerepNetwork initialized");
|
||||||
|
|
||||||
PRINT_SIZEOF(knIPV4Address);
|
PRINT_SIZEOF(knIPV4Address);
|
||||||
PRINT_SIZEOF(knPort);
|
PRINT_SIZEOF(knPort);
|
||||||
PRINT_SIZEOF(knIPV4Endpoint);
|
PRINT_SIZEOF(knIPV4Endpoint);
|
||||||
|
PRINT_SIZEOF(knSocketTCP);
|
||||||
|
PRINT_SIZEOF(knSocketUDP);
|
||||||
PRINT_SIZEOF(knPackage);
|
PRINT_SIZEOF(knPackage);
|
||||||
PRINT_SIZEOF(knChannel);
|
PRINT_SIZEOF(knChannel);
|
||||||
PRINT_SIZEOF(knSocketTCP);
|
|
||||||
|
|
||||||
test_knIPV4Address_fromStr(127,0,0,1);
|
test_knIPV4Address_fromStr(127,0,0,1);
|
||||||
test_knIPV4Address_fromStr(34,255,45,0);
|
test_knIPV4Address_fromStr(34,255,45,0);
|
||||||
test_knIPV4Address_fromStr(3,3,3,128);
|
test_knIPV4Address_fromStr(3,3,3,128);
|
||||||
/*
|
|
||||||
knSocketTCP* s;
|
knSocketTCP* s;
|
||||||
tryLast(knSocket_open(knSocketProtocol_TCP), maybeS)
|
tryLast(knSocketTCP_open(), maybeS, ;);
|
||||||
s=maybeS.value.VoidPtr;
|
s=maybeS.value.VoidPtr;
|
||||||
kprintf("\e[92mTCP socket created\n");
|
kprintf("\e[92mTCP socket created\n");
|
||||||
tryLast(knSocket_close(s);,_);
|
|
||||||
|
knIPV4Endpoint localEnd = knIPV4Endpoint_create(knIPV4Address_fromBytes(127,0,0,1), 4444);
|
||||||
|
tryLast(knSocketTCP_listen(s, localEnd), _m81775, ;)
|
||||||
|
|
||||||
|
tryLast(knSocketTCP_close(s), _m676, ;);
|
||||||
kprintf("\e[92mTCP socket closed\n");
|
kprintf("\e[92mTCP socket closed\n");
|
||||||
*/
|
|
||||||
|
tryLast(kt_tryDispose(), _m88ag, ;);
|
||||||
|
kprintf("\e[92mkerepNetwork disposed\n");
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user