This commit is contained in:
malloc 2019-01-15 16:24:52 -06:00
parent ed72a09f08
commit b627b59b03
7 changed files with 94 additions and 3 deletions

View file

@ -11,7 +11,8 @@ find_package(OpenSSL)
add_executable(server ${server_src})
target_include_directories(server
PRIVATE ${OPENSSL_INCLUDE_DIR})
PRIVATE ${OPENSSL_INCLUDE_DIR}
PRIVATE ${PROJECT_SOURCE_DIR}/src/server)
target_link_libraries(server ${OPENSSL_LIBRARIES})
install(TARGETS server RUNTIME DESTINATION bin/server)

View file

@ -27,10 +27,14 @@
#endif
#include <stdint.h>
#include "util/ipaddr.h"
#define GLV_TCP_FLAG_TYPE 1
#define GLV_TCP_FLAG_NBIO 2
#define GLV_TCP_RECV_APPEND 1
#define GLV_TCP_RECV_BLOCK 2
typedef struct {
glv_sock_t socket;
glv_addr_t addr;
@ -40,8 +44,15 @@ typedef struct {
glv_tcp_t* glv_tcp_create_server();
glv_tcp_t* glv_tcp_create_client();
int glv_tcp_send(glv_tcp_t* sock, char* data, unsigned int length);
int glv_tcp_recv(glv_tcp_t* sock, char* data, unsigned int length, int flags);
int glv_tcp_data_ready(glv_tcp_t* sock);
int glv_tcp_is_open(glv_tcp_t* sock);
int glv_tcp_is_secure(glv_tcp_t* sock);
void glv_tcp_destroy(glv_tcp_t* socket);
ipaddr_t glv_tcp_get_ip(glv_tcp_t* sock);
void glv_tcp_destroy(glv_tcp_t* sock);
#endif

View file

@ -1,4 +1,6 @@
#ifndef _WIN32
#include "tcp.h"
#endif

2
src/server/util/ipaddr.c Normal file
View file

@ -0,0 +1,2 @@
#include "ipaddr.h"

20
src/server/util/ipaddr.h Normal file
View file

@ -0,0 +1,20 @@
#ifndef GLV_UTIL_IPADDR_H
#define GLV_UTIL_IPADDR_H
#include <stdint.h>
typedef struct {
uint8_t addr[16];
uint8_t cidr;
} ipaddr_t;
ipaddr_t glv_ip_aton(char* addr, int length);
ipaddr_t glv_ip_raw(uint8_t* addr, uint8_t cidr);
int glv_ip_compare(ipaddr_t* lhs, ipaddr_t* rhs);
int glv_ip_identical(ipaddr_t* lhs, ipaddr_t* rhs);
int glv_ip_isv4(ipaddr_t* addr);
int glv_ip_ntoa(ipaddr_t* addr, char* out);
#endif

View file

@ -18,7 +18,7 @@ glv_thread_t* glv_thread_create(glv_func_t func, void* args) {
proxy->func = func;
proxy->args = args;
glv_thread_t* thread = malloc(sizeof(thread));
glv_thread_t* thread = malloc(sizeof(glv_thread_t));
if(pthread_create(thread, NULL, _wrapper_func, proxy) == 0)
return thread;
else
@ -38,7 +38,24 @@ void glv_thread_destroy(glv_thread_t* thread) {
/** BEGIN MUTX IMPL **/
glv_mutex_t* glv_mutex_create() {
glv_mutex_t* mtx = malloc(sizeof(glv_mutex_t));
if(pthread_mutex_init(mtx, NULL) == 0)
return mtx;
else
return NULL;
}
void glv_mutex_lock(glv_mutex_t* mutex) {
pthread_mutex_lock(mutex);
}
void glv_mutex_unlock(glv_mutex_t* mutex) {
pthread_mutex_unlock(mutex);
}
void glv_mutex_destroy(glv_mutex_t* mutex) {
pthread_mutex_destroy(mutex);
free(mutex);
}
#endif

View file

@ -1,6 +1,44 @@
#ifdef _WIN32
#include "thread.h"
glv_thread_t* glv_thread_create(glv_func_t func, void* args) {
glv_thread_t* thread = malloc(sizeof(glv_thread_t));
*thread = (glv_thread_t)_beginthread(func, 0, args);
return thread;
}
void glv_thread_join(glv_thread_t* thread) {
WaitForSingleObject(*thread, INFINITE);
}
void glv_thread_destroy(glv_thread_t* thread) {
CloseHandle(*thread);
free(thread);
}
/** END THREAD IMPL **/
/*********************/
/** BEGIN MUTX IMPL **/
glv_mutex_t* glv_mutex_create() {
glv_mutex_t* mutex = malloc(sizeof(glv_mutex_t));
*mutex = CreateMutexA(NULL, NULL, NULL);
return mutex;
}
void glv_mutex_lock(glv_mutex_t* mutex) {
WaitForSingleObject(*mutex, INFINITE);
}
void glv_mutex_unlock(glv_mutex_t* mutex) {
ReleaseMutex(*mutex);
}
void glv_mutex_destroy(glv_mutex_t* mutex) {
CloseHandle(*mutex);
free(mutex);
}
#endif