move code to src

add linux implementation
This commit is contained in:
Nicolas Dextraze 2018-03-19 10:28:24 -07:00
parent 4ddb60472d
commit 9147e4a63f
19 changed files with 164 additions and 75 deletions

View File

@ -1,13 +1,17 @@
# cmake_minimum_required(VERSION <specify CMake version here>) cmake_minimum_required(VERSION 3.10)
project(esc C) project(esc C)
set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD 99)
include_directories(c:/Users/nicol/dev/thirdparty/protobuf-c) if(WIN32)
link_directories(c:/Users/nicol/dev/thirdparty/protobuf-c/protobuf-c/.libs) 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) if(WIN32)
target_link_libraries(esc wsock32 ws2_32 libprotobuf-c.a) target_link_libraries(esc wsock32 ws2_32 libprotobuf-c.a)
else()
target_link_libraries(esc protobuf-c)
endif() endif()

View File

@ -1,61 +0,0 @@
//
// Created by nicol on 2018-03-18.
//
#include "socket.h"
#ifdef _WIN32
#include <ws2tcpip.h>
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

View File

@ -2,14 +2,13 @@
// Created by nicol on 2018-03-18. // Created by nicol on 2018-03-18.
// //
#include <mem.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <winsock2.h> #include <string.h>
#include "esc.h" #include "esc.h"
#include "socket.h" #include "socket.h"
#include "buffer.h" #include "buffer.h"
#include "ClientMessageDtos.pb-c.h" #include "proto.h"
#include "tcp_package.h" #include "tcp_package.h"
struct st_connection_settings { struct st_connection_settings {

View File

@ -20,8 +20,8 @@ struct st_credentials {
typedef struct st_credentials esc_credentials_t; typedef struct st_credentials esc_credentials_t;
struct st_esc_position { struct st_esc_position {
__int64 prepare_position; int64_t prepare_position;
__int64 commit_position; int64_t commit_position;
}; };
typedef struct st_esc_position esc_position_t; typedef struct st_esc_position esc_position_t;

View File

View File

@ -5,6 +5,8 @@
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#endif #endif
#include <stdlib.h>
#include "mutex.h" #include "mutex.h"
#ifdef _WIN32 #ifdef _WIN32
@ -25,4 +27,23 @@ void mutex_unlock(mutex_t mutex) {
void mutex_destroy(mutex_t mutex) { void mutex_destroy(mutex_t mutex) {
DeleteCriticalSection(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 #endif

View File

@ -8,6 +8,9 @@
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
typedef LPCRITICAL_SECTION mutex_t; typedef LPCRITICAL_SECTION mutex_t;
#else
#include <pthread.h>
typedef pthread_mutex_t* mutex_t;
#endif #endif
mutex_t mutex_create(); mutex_t mutex_create();

View File

@ -6,7 +6,7 @@
#define PROTOBUF_C__NO_DEPRECATED #define PROTOBUF_C__NO_DEPRECATED
#endif #endif
#include "ClientMessageDtos.pb-c.h" #include "proto.h"
void event_store__client__messages__new_event__init void event_store__client__messages__new_event__init
(EventStore__Client__Messages__NewEvent *message) (EventStore__Client__Messages__NewEvent *message)
{ {

View File

@ -1,8 +1,8 @@
/* Generated by the protocol buffer compiler. DO NOT EDIT! */ /* Generated by the protocol buffer compiler. DO NOT EDIT! */
/* Generated from: Protos/ClientAPI/ClientMessageDtos.proto */ /* Generated from: Protos/ClientAPI/ClientMessageDtos.proto */
#ifndef PROTOBUF_C_Protos_2fClientAPI_2fClientMessageDtos_2eproto__INCLUDED #ifndef ESC_PROTO_H
#define PROTOBUF_C_Protos_2fClientAPI_2fClientMessageDtos_2eproto__INCLUDED #define ESC_PROTO_H
#include <protobuf-c/protobuf-c.h> #include <protobuf-c/protobuf-c.h>
@ -1690,4 +1690,4 @@ extern const ProtobufCMessageDescriptor event_store__client__messages__client_id
PROTOBUF_C__END_DECLS PROTOBUF_C__END_DECLS
#endif /* PROTOBUF_C_Protos_2fClientAPI_2fClientMessageDtos_2eproto__INCLUDED */ #endif /* ESC_PROTO_H */

123
src/socket.c Normal file
View File

@ -0,0 +1,123 @@
//
// Created by nicol on 2018-03-18.
//
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <netdb.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include "socket.h"
#ifdef _WIN32
#include <ws2tcpip.h>
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

View File

@ -4,7 +4,7 @@
#include <memory.h> #include <memory.h>
#include <stdlib.h> #include <stdlib.h>
#include <mem.h> #include <string.h>
#include "tcp_package.h" #include "tcp_package.h"
const uint32_t CommandOffset = 0; const uint32_t CommandOffset = 0;

View File

View File