From 9147e4a63ff670536a8b6c09473bc469170198db Mon Sep 17 00:00:00 2001 From: Nicolas Dextraze Date: Mon, 19 Mar 2018 10:28:24 -0700 Subject: [PATCH] move code to src add linux implementation --- CMakeLists.txt | 12 ++- socket.c | 61 ------------ buffer.c => src/buffer.c | 0 buffer.h => src/buffer.h | 0 esc.c => src/esc.c | 5 +- esc.h => src/esc.h | 4 +- main.c => src/main.c | 0 mutex.c => src/mutex.c | 21 ++++ mutex.h => src/mutex.h | 3 + ClientMessageDtos.pb-c.c => src/proto.c | 2 +- ClientMessageDtos.pb-c.h => src/proto.h | 6 +- src/socket.c | 123 ++++++++++++++++++++++++ socket.h => src/socket.h | 0 tcp_package.c => src/tcp_package.c | 2 +- tcp_package.h => src/tcp_package.h | 0 thread.c => src/thread.c | 0 thread.h => src/thread.h | 0 uuid.c => src/uuid.c | 0 uuid.h => src/uuid.h | 0 19 files changed, 164 insertions(+), 75 deletions(-) delete mode 100644 socket.c rename buffer.c => src/buffer.c (100%) rename buffer.h => src/buffer.h (100%) rename esc.c => src/esc.c (99%) rename esc.h => src/esc.h (96%) rename main.c => src/main.c (100%) rename mutex.c => src/mutex.c (54%) rename mutex.h => src/mutex.h (83%) rename ClientMessageDtos.pb-c.c => src/proto.c (99%) rename ClientMessageDtos.pb-c.h => src/proto.h (99%) create mode 100644 src/socket.c rename socket.h => src/socket.h (100%) rename tcp_package.c => src/tcp_package.c (99%) rename tcp_package.h => src/tcp_package.h (100%) rename thread.c => src/thread.c (100%) rename thread.h => src/thread.h (100%) rename uuid.c => src/uuid.c (100%) rename uuid.h => src/uuid.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9dcbc20..77eec9e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,13 +1,17 @@ -# cmake_minimum_required(VERSION ) +cmake_minimum_required(VERSION 3.10) project(esc C) set(CMAKE_C_STANDARD 99) -include_directories(c:/Users/nicol/dev/thirdparty/protobuf-c) -link_directories(c:/Users/nicol/dev/thirdparty/protobuf-c/protobuf-c/.libs) +if(WIN32) + include_directories(c:/Users/nicol/dev/thirdparty/protobuf-c) + link_directories(c:/Users/nicol/dev/thirdparty/protobuf-c/protobuf-c/.libs) +endif() -add_executable(esc main.c mutex.c mutex.h thread.c thread.h socket.c socket.h esc.c esc.h ClientMessageDtos.pb-c.c ClientMessageDtos.pb-c.h uuid.c uuid.h buffer.c buffer.h tcp_package.c tcp_package.h) +add_executable(esc src/main.c src/mutex.c src/mutex.h src/thread.c src/thread.h src/socket.c src/socket.h src/esc.c src/esc.h src/proto.c src/proto.h src/uuid.c src/uuid.h src/buffer.c src/buffer.h src/tcp_package.c src/tcp_package.h) if(WIN32) target_link_libraries(esc wsock32 ws2_32 libprotobuf-c.a) +else() + target_link_libraries(esc protobuf-c) endif() \ No newline at end of file diff --git a/socket.c b/socket.c deleted file mode 100644 index 6b819a9..0000000 --- a/socket.c +++ /dev/null @@ -1,61 +0,0 @@ -// -// Created by nicol on 2018-03-18. -// - -#include "socket.h" - -#ifdef _WIN32 -#include -struct st_socket { - SOCKET s; - int af; - int type; - int proto; -}; - -socket_t socket_create(int type) { - if (type == SOCKET_TYPE_TCP) { - socket_t s = malloc(sizeof(struct st_socket)); - s->af = AF_INET; - s->type = SOCK_STREAM; - s->proto = IPPROTO_TCP; - s->s = socket(s->af, s->type, s->proto); - if (s->s == INVALID_SOCKET) { - free(s); - return NULL; - } - return s; - } - return NULL; -} - -void socket_close(socket_t s) { - closesocket(s->s); -} - -int socket_connect(socket_t s, char* addr, unsigned short port) { - char port_s[6]; - ADDRINFO hints; - memset(&hints, 0, sizeof(ADDRINFO)); - hints.ai_family = s->af; - hints.ai_socktype = s->type; - hints.ai_protocol = s->proto; - ADDRINFO* result; - if (getaddrinfo(addr, itoa(port, port_s, 10), &hints, &result) != 0) { - return -1; - } - return connect(s->s, result->ai_addr, (int)result->ai_addrlen); -} - -int socket_send(socket_t s, char* data, int len) { - return send(s->s, data, len, 0); -} - -int socket_recv(socket_t s, char* buf, int len) { - return recv(s->s, buf, len, 0); -} - -int socket_error() { - return WSAGetLastError(); -} -#endif \ No newline at end of file diff --git a/buffer.c b/src/buffer.c similarity index 100% rename from buffer.c rename to src/buffer.c diff --git a/buffer.h b/src/buffer.h similarity index 100% rename from buffer.h rename to src/buffer.h diff --git a/esc.c b/src/esc.c similarity index 99% rename from esc.c rename to src/esc.c index 6e2ff47..d40c3d8 100644 --- a/esc.c +++ b/src/esc.c @@ -2,14 +2,13 @@ // Created by nicol on 2018-03-18. // -#include #include #include -#include +#include #include "esc.h" #include "socket.h" #include "buffer.h" -#include "ClientMessageDtos.pb-c.h" +#include "proto.h" #include "tcp_package.h" struct st_connection_settings { diff --git a/esc.h b/src/esc.h similarity index 96% rename from esc.h rename to src/esc.h index 0e8aa52..9f54e42 100644 --- a/esc.h +++ b/src/esc.h @@ -20,8 +20,8 @@ struct st_credentials { typedef struct st_credentials esc_credentials_t; struct st_esc_position { - __int64 prepare_position; - __int64 commit_position; + int64_t prepare_position; + int64_t commit_position; }; typedef struct st_esc_position esc_position_t; diff --git a/main.c b/src/main.c similarity index 100% rename from main.c rename to src/main.c diff --git a/mutex.c b/src/mutex.c similarity index 54% rename from mutex.c rename to src/mutex.c index 902431e..372ab0d 100644 --- a/mutex.c +++ b/src/mutex.c @@ -5,6 +5,8 @@ #ifdef _WIN32 #include #endif + +#include #include "mutex.h" #ifdef _WIN32 @@ -25,4 +27,23 @@ void mutex_unlock(mutex_t mutex) { void mutex_destroy(mutex_t mutex) { DeleteCriticalSection(mutex); } +#else +mutex_t mutex_create() { + mutex_t mutex = malloc(sizeof(mutex_t)); + pthread_mutex_init(mutex, 0); + return mutex; +} + +void mutex_lock(mutex_t mutex) { + pthread_mutex_lock(mutex); +} + +void mutex_unlock(mutex_t mutex) { + pthread_mutex_unlock(mutex); +} + +void mutex_destroy(mutex_t mutex) { + pthread_mutex_destroy(mutex); + free(mutex); +} #endif \ No newline at end of file diff --git a/mutex.h b/src/mutex.h similarity index 83% rename from mutex.h rename to src/mutex.h index fc2ec45..e8711e2 100644 --- a/mutex.h +++ b/src/mutex.h @@ -8,6 +8,9 @@ #ifdef _WIN32 #include typedef LPCRITICAL_SECTION mutex_t; +#else +#include +typedef pthread_mutex_t* mutex_t; #endif mutex_t mutex_create(); diff --git a/ClientMessageDtos.pb-c.c b/src/proto.c similarity index 99% rename from ClientMessageDtos.pb-c.c rename to src/proto.c index 2db183f..5db01d7 100644 --- a/ClientMessageDtos.pb-c.c +++ b/src/proto.c @@ -6,7 +6,7 @@ #define PROTOBUF_C__NO_DEPRECATED #endif -#include "ClientMessageDtos.pb-c.h" +#include "proto.h" void event_store__client__messages__new_event__init (EventStore__Client__Messages__NewEvent *message) { diff --git a/ClientMessageDtos.pb-c.h b/src/proto.h similarity index 99% rename from ClientMessageDtos.pb-c.h rename to src/proto.h index 5a60736..a04b593 100644 --- a/ClientMessageDtos.pb-c.h +++ b/src/proto.h @@ -1,8 +1,8 @@ /* Generated by the protocol buffer compiler. DO NOT EDIT! */ /* Generated from: Protos/ClientAPI/ClientMessageDtos.proto */ -#ifndef PROTOBUF_C_Protos_2fClientAPI_2fClientMessageDtos_2eproto__INCLUDED -#define PROTOBUF_C_Protos_2fClientAPI_2fClientMessageDtos_2eproto__INCLUDED +#ifndef ESC_PROTO_H +#define ESC_PROTO_H #include @@ -1690,4 +1690,4 @@ extern const ProtobufCMessageDescriptor event_store__client__messages__client_id PROTOBUF_C__END_DECLS -#endif /* PROTOBUF_C_Protos_2fClientAPI_2fClientMessageDtos_2eproto__INCLUDED */ +#endif /* ESC_PROTO_H */ diff --git a/src/socket.c b/src/socket.c new file mode 100644 index 0000000..8088f92 --- /dev/null +++ b/src/socket.c @@ -0,0 +1,123 @@ +// +// Created by nicol on 2018-03-18. +// + +#include +#include +#include +#include +#include +#include +#include +#include +#include "socket.h" + +#ifdef _WIN32 +#include +struct st_socket { + SOCKET s; + int af; + int type; + int proto; +}; + +socket_t socket_create(int type) { + if (type == SOCKET_TYPE_TCP) { + socket_t s = malloc(sizeof(struct st_socket)); + s->af = AF_INET; + s->type = SOCK_STREAM; + s->proto = IPPROTO_TCP; + s->s = socket(s->af, s->type, s->proto); + if (s->s == INVALID_SOCKET) { + free(s); + return NULL; + } + return s; + } + return NULL; +} + +void socket_close(socket_t s) { + closesocket(s->s); +} + +int socket_connect(socket_t s, char* addr, unsigned short port) { + char port_s[6]; + ADDRINFO hints; + memset(&hints, 0, sizeof(ADDRINFO)); + hints.ai_family = s->af; + hints.ai_socktype = s->type; + hints.ai_protocol = s->proto; + ADDRINFO* result; + if (getaddrinfo(addr, itoa(port, port_s, 10), &hints, &result) != 0) { + return -1; + } + return connect(s->s, result->ai_addr, (int)result->ai_addrlen); +} + +int socket_send(socket_t s, char* data, int len) { + return send(s->s, data, len, 0); +} + +int socket_recv(socket_t s, char* buf, int len) { + return recv(s->s, buf, len, 0); +} + +int socket_error() { + return WSAGetLastError(); +} +#else +struct st_socket { + int s; + int af; + int type; + int proto; +}; + +socket_t socket_create(int type) { + if (type == SOCKET_TYPE_TCP) { + socket_t s = malloc(sizeof(struct st_socket)); + s->af = AF_INET; + s->type = SOCK_STREAM; + s->proto = IPPROTO_TCP; + s->s = socket(s->af, s->type, s->proto); + if (s->s == -1) { + free(s); + return NULL; + } + return s; + } + return NULL; +} + +void socket_close(socket_t s) { + close(s->s); +} + +int socket_connect(socket_t s, char* addr, unsigned short port) { + struct addrinfo hints; + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = s->af; + hints.ai_socktype = s->type; + hints.ai_protocol = s->proto; + struct addrinfo* result; + char port_s[6]; + snprintf(port_s, 6, "%d", port); + if (getaddrinfo(addr, port_s, &hints, &result) != 0) { + return -1; + } + return connect(s->s, result->ai_addr, (int)result->ai_addrlen); +} + +int socket_send(socket_t s, char* data, int len) { + return (int)send(s->s, data, (size_t)len, 0); +} + +int socket_recv(socket_t s, char* buf, int len) { + return (int)recv(s->s, buf, (size_t)len, 0); +} + +int socket_error() { + return errno; +} +#endif \ No newline at end of file diff --git a/socket.h b/src/socket.h similarity index 100% rename from socket.h rename to src/socket.h diff --git a/tcp_package.c b/src/tcp_package.c similarity index 99% rename from tcp_package.c rename to src/tcp_package.c index ff4f8ba..c637289 100644 --- a/tcp_package.c +++ b/src/tcp_package.c @@ -4,7 +4,7 @@ #include #include -#include +#include #include "tcp_package.h" const uint32_t CommandOffset = 0; diff --git a/tcp_package.h b/src/tcp_package.h similarity index 100% rename from tcp_package.h rename to src/tcp_package.h diff --git a/thread.c b/src/thread.c similarity index 100% rename from thread.c rename to src/thread.c diff --git a/thread.h b/src/thread.h similarity index 100% rename from thread.h rename to src/thread.h diff --git a/uuid.c b/src/uuid.c similarity index 100% rename from uuid.c rename to src/uuid.c diff --git a/uuid.h b/src/uuid.h similarity index 100% rename from uuid.h rename to src/uuid.h