windows fixes

This commit is contained in:
Nicolas Dextraze 2018-03-20 18:34:23 -07:00
parent 2088fd4082
commit 1ca51948c2
5 changed files with 102 additions and 32 deletions

View File

@ -8,7 +8,6 @@
#include <stdarg.h> #include <stdarg.h>
#include "esc.h" #include "esc.h"
#include "utils/socket.h" #include "utils/socket.h"
#include "utils/buffer.h"
#include "proto.h" #include "proto.h"
#include "tcp_package.h" #include "tcp_package.h"
#include "utils/thread.h" #include "utils/thread.h"
@ -18,6 +17,7 @@
#include "tcp_messages.h" #include "tcp_messages.h"
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h>
#define usleep Sleep #define usleep Sleep
#endif #endif
#ifdef __linux__ #ifdef __linux__
@ -96,7 +96,11 @@ const node_endpoints_t* static_discover(const static_endpoint_discoverer_t* disc
//TODO partial transfer //TODO partial transfer
ssize_t connection_send_tcp_package(esc_connection_t conn, tcp_package_t pkg) { ssize_t connection_send_tcp_package(esc_connection_t conn, tcp_package_t pkg) {
char uuid_buf[37]; char uuid_buf[37];
fprintf(stderr, "connection_send_tcp_package: %u %u %s %lu ", pkg->command, pkg->flags, esc_uuid_format(pkg->correlation_id, uuid_buf, 37), buffer_size(pkg->data)); #ifdef _WIN32
fprintf(stderr, "connection_send_tcp_package: %s %u %s %llu ", get_string_for_tcp_message(pkg->command), pkg->flags, esc_uuid_format(pkg->correlation_id, uuid_buf, 37), buffer_size(pkg->data));
#else
fprintf(stderr, "connection_send_tcp_package: %s %u %s %lu ", get_string_for_tcp_message(pkg->command), pkg->flags, esc_uuid_format(pkg->correlation_id, uuid_buf, 37), buffer_size(pkg->data));
#endif
buffer_t send_buffer = tcp_package_to_buffer(pkg); buffer_t send_buffer = tcp_package_to_buffer(pkg);
size_t send_buffer_size = buffer_size(send_buffer); size_t send_buffer_size = buffer_size(send_buffer);
@ -125,7 +129,11 @@ tcp_package_t connection_recv_tcp_package(esc_connection_t conn) {
uint32_t recv_size; uint32_t recv_size;
ssize_t rc; ssize_t rc;
if ((rc = socket_recv(conn->tcp_conn, (char *)&recv_size, sizeof(uint32_t))) != 4) { if ((rc = socket_recv(conn->tcp_conn, (char *)&recv_size, sizeof(uint32_t))) != 4) {
#ifdef _WIN32
fprintf(stderr, "connection_recv_tcp_package: %lld %d\n", rc, socket_error(conn->tcp_conn));
#else
fprintf(stderr, "connection_recv_tcp_package: %ld %d\n", rc, socket_error(conn->tcp_conn)); fprintf(stderr, "connection_recv_tcp_package: %ld %d\n", rc, socket_error(conn->tcp_conn));
#endif
return 0; return 0;
} }
buffer_t recv_buffer = buffer_create(recv_size); buffer_t recv_buffer = buffer_create(recv_size);
@ -139,7 +147,11 @@ tcp_package_t connection_recv_tcp_package(esc_connection_t conn) {
tcp_package_t recv_pkg = tcp_package_from_buffer(recv_buffer); tcp_package_t recv_pkg = tcp_package_from_buffer(recv_buffer);
buffer_destroy(recv_buffer); buffer_destroy(recv_buffer);
char uuid_buf[37]; char uuid_buf[37];
fprintf(stderr, "connection_recv_tcp_package: %u %u %s %lu\n", recv_pkg->command, recv_pkg->flags, esc_uuid_format(recv_pkg->correlation_id, uuid_buf, 37), recv_buffer_size); #ifdef _WIN32
fprintf(stderr, "connection_recv_tcp_package: %s %u %s %llu\n", get_string_for_tcp_message(recv_pkg->command), recv_pkg->flags, esc_uuid_format(recv_pkg->correlation_id, uuid_buf, 37), buffer_size(recv_pkg->data));
#else
fprintf(stderr, "connection_recv_tcp_package: %s %u %s %lu\n", get_string_for_tcp_message(recv_pkg->command), recv_pkg->flags, esc_uuid_format(recv_pkg->correlation_id, uuid_buf, 37), buffer_size(recv_pkg->data));
#endif
return recv_pkg; return recv_pkg;
} }
@ -274,8 +286,8 @@ int esc_connection_connect(esc_connection_t conn) {
return 0; return 0;
} }
const esc_credentials_t* esc_credentials_create(const char* username, const char* password) { esc_credentials_t esc_credentials_create(const char* username, const char* password) {
esc_credentials_t* creds = malloc(sizeof(struct st_credentials)); esc_credentials_t creds = malloc(sizeof(struct st_credentials));
creds->username = username; creds->username = username;
creds->password = password; creds->password = password;
return creds; return creds;
@ -304,7 +316,7 @@ esc_resolved_event_t* resolved_event_create(EventStore__Client__Messages__Resolv
return ev; return ev;
} }
const esc_all_events_slice_t* esc_connection_read_all_forward(esc_connection_t conn, const esc_position_t* last_checkpoint, unsigned int count, const esc_credentials_t* credentials) { esc_all_events_slice_t esc_connection_read_all_forward(esc_connection_t conn, const esc_position_t* last_checkpoint, unsigned int count, esc_credentials_t credentials) {
EventStore__Client__Messages__ReadAllEvents send_msg; EventStore__Client__Messages__ReadAllEvents send_msg;
event_store__client__messages__read_all_events__init(&send_msg); event_store__client__messages__read_all_events__init(&send_msg);
send_msg.prepare_position = last_checkpoint ? last_checkpoint->prepare_position : 0; send_msg.prepare_position = last_checkpoint ? last_checkpoint->prepare_position : 0;
@ -329,7 +341,7 @@ const esc_all_events_slice_t* esc_connection_read_all_forward(esc_connection_t c
EventStore__Client__Messages__ReadAllEventsCompleted *recv_msg = EventStore__Client__Messages__ReadAllEventsCompleted *recv_msg =
event_store__client__messages__read_all_events_completed__unpack(&conn->protobuf_c_allocator, data_size, data); event_store__client__messages__read_all_events_completed__unpack(&conn->protobuf_c_allocator, data_size, data);
esc_all_events_slice_t* result = malloc(sizeof(struct st_all_events_slice)); esc_all_events_slice_t result = malloc(sizeof(struct st_all_events_slice));
result->read_direction = "forward"; result->read_direction = "forward";
result->is_end_of_stream = (recv_msg->events == 0 || recv_msg->n_events == 0) ? 1 : 0; result->is_end_of_stream = (recv_msg->events == 0 || recv_msg->n_events == 0) ? 1 : 0;
result->from_position.prepare_position = recv_msg->prepare_position; result->from_position.prepare_position = recv_msg->prepare_position;
@ -351,6 +363,10 @@ void esc_connection_close(esc_connection_t conn) {
} }
const char* esc_position_format(const esc_position_t* position, char* buffer, size_t buf_size) { const char* esc_position_format(const esc_position_t* position, char* buffer, size_t buf_size) {
#ifdef _WIN32
snprintf(buffer, buf_size, "%llu/%llu", position->prepare_position, position->commit_position);
#else
snprintf(buffer, buf_size, "%lu/%lu", position->prepare_position, position->commit_position); snprintf(buffer, buf_size, "%lu/%lu", position->prepare_position, position->commit_position);
#endif
return buffer; return buffer;
} }

View File

@ -24,7 +24,7 @@ struct st_credentials {
const char* username; const char* username;
const char* password; const char* password;
}; };
typedef struct st_credentials esc_credentials_t; typedef struct st_credentials* esc_credentials_t;
struct st_esc_position { struct st_esc_position {
int64_t prepare_position; int64_t prepare_position;
@ -58,15 +58,15 @@ struct st_all_events_slice {
esc_resolved_event_t** events; esc_resolved_event_t** events;
int is_end_of_stream; int is_end_of_stream;
}; };
typedef struct st_all_events_slice esc_all_events_slice_t; typedef struct st_all_events_slice* esc_all_events_slice_t;
const esc_connection_settings_t* esc_default_connection_settings; const esc_connection_settings_t* esc_default_connection_settings;
// Connection // Connection
esc_connection_t esc_connection_create(const esc_connection_settings_t* connection_settings, const char* addr, const char* connection_name); esc_connection_t esc_connection_create(const esc_connection_settings_t* connection_settings, const char* addr, const char* connection_name);
int esc_connection_connect(esc_connection_t conn); int esc_connection_connect(esc_connection_t conn);
const esc_credentials_t* esc_credentials_create(const char* username, const char* password); esc_credentials_t esc_credentials_create(const char* username, const char* password);
const esc_all_events_slice_t* esc_connection_read_all_forward(esc_connection_t conn, const esc_position_t* last_checkpoint, unsigned int count, const esc_credentials_t* credentials); esc_all_events_slice_t esc_connection_read_all_forward(esc_connection_t conn, const esc_position_t* last_checkpoint, unsigned int count, esc_credentials_t credentials);
void esc_connection_close(esc_connection_t conn); void esc_connection_close(esc_connection_t conn);
// Utils // Utils

View File

@ -1,16 +1,14 @@
#include <stdio.h> #include <stdio.h>
#ifdef __linux__
#include <unistd.h>
#include <assert.h>
#endif
#include "utils/mutex.h"
#include "esc.h" #include "esc.h"
#include "utils/queue.h"
#ifdef _WIN32 #ifdef _WIN32
#include <winsock.h>
#include <windows.h>
#define usleep Sleep #define usleep Sleep
#endif #endif
#ifdef __linux__
#include <unistd.h>
#endif
int main() { int main() {
#ifdef _WIN32 #ifdef _WIN32
@ -25,9 +23,9 @@ int main() {
if (esc_connection_connect(conn) != 0) { if (esc_connection_connect(conn) != 0) {
return -2; return -2;
} }
const esc_credentials_t* credentials = esc_credentials_create("admin", "changeit"); esc_credentials_t credentials = esc_credentials_create("admin", "changeit");
const esc_all_events_slice_t *result = 0; esc_all_events_slice_t result = 0;
do { do {
result = esc_connection_read_all_forward(conn, result ? &result->next_position : NULL, 100, credentials); result = esc_connection_read_all_forward(conn, result ? &result->next_position : NULL, 100, credentials);
if (result == 0) { if (result == 0) {
@ -41,7 +39,11 @@ int main() {
result->is_end_of_stream); result->is_end_of_stream);
char uuid_buf[37]; char uuid_buf[37];
for (size_t i = 0; i < result->n_events; i++) { for (size_t i = 0; i < result->n_events; i++) {
#ifdef _WIN32
printf("%s %s %lld@%s %llu %llu\n", esc_uuid_format(result->events[i]->event->event_id, uuid_buf, 37),
#else
printf("%s %s %ld@%s %lu %lu\n", esc_uuid_format(result->events[i]->event->event_id, uuid_buf, 37), printf("%s %s %ld@%s %lu %lu\n", esc_uuid_format(result->events[i]->event->event_id, uuid_buf, 37),
#endif
result->events[i]->event->event_type, result->events[i]->event->event_type,
result->events[i]->event->event_number, result->events[i]->event->event_number,
result->events[i]->event->event_stream_id, result->events[i]->event->event_stream_id,

View File

@ -13,6 +13,7 @@ struct st_socket {
int af; int af;
int type; int type;
int proto; int proto;
int last_error;
}; };
socket_t socket_create(int type) { socket_t socket_create(int type) {
@ -23,16 +24,19 @@ socket_t socket_create(int type) {
s->proto = IPPROTO_TCP; s->proto = IPPROTO_TCP;
s->s = socket(s->af, s->type, s->proto); s->s = socket(s->af, s->type, s->proto);
if (s->s == INVALID_SOCKET) { if (s->s == INVALID_SOCKET) {
free(s); s->last_error = WSAGetLastError();
return NULL;
} }
return s; return s;
} }
return NULL; return NULL;
} }
void socket_close(socket_t s) { int socket_close(socket_t s) {
closesocket(s->s); int rc = closesocket(s->s);
if (rc != 0) {
s->last_error = WSAGetLastError();
}
return rc;
} }
int socket_connect(socket_t s, char* addr, unsigned short port) { int socket_connect(socket_t s, char* addr, unsigned short port) {
@ -43,30 +47,62 @@ int socket_connect(socket_t s, char* addr, unsigned short port) {
hints.ai_socktype = s->type; hints.ai_socktype = s->type;
hints.ai_protocol = s->proto; hints.ai_protocol = s->proto;
ADDRINFO* result; ADDRINFO* result;
if (getaddrinfo(addr, itoa(port, port_s, 10), &hints, &result) != 0) { int rc;
if ((rc = getaddrinfo(addr, itoa(port, port_s, 10), &hints, &result)) != 0) {
s->last_error = WSAGetLastError();
return -1; return -1;
} }
return connect(s->s, result->ai_addr, (int)result->ai_addrlen); if ((rc = connect(s->s, result->ai_addr, (int)result->ai_addrlen)) != 0) {
s->last_error = WSAGetLastError();
return -2;
}
return 0;
} }
ssize_t socket_send(socket_t s, char* data, size_t len) { ssize_t socket_send(socket_t s, char* data, size_t len) {
return send(s->s, data, (int)len, 0); ssize_t rc = send(s->s, data, (int)len, 0);
if (rc < 0) {
s->last_error = WSAGetLastError();
}
return rc;
} }
ssize_t socket_recv(socket_t s, char* buf, size_t len) { ssize_t socket_recv(socket_t s, char* buf, size_t len) {
return recv(s->s, buf, (int)len, 0); ssize_t rc = recv(s->s, buf, (int)len, 0);
if (rc < 0) {
s->last_error = WSAGetLastError();
}
return rc;
} }
int socket_readable(socket_t s) { int socket_readable(socket_t s) {
fd_set readable; fd_set readable;
FD_ZERO(&readable);
FD_SET(s->s, &readable); FD_SET(s->s, &readable);
struct timeval timeout = {0, 0}; struct timeval timeout = {0, 0};
return select(0, &readable, 0, 0, &timeout); int rc = select(0, &readable, 0, 0, &timeout);
if (rc < 0) {
s->last_error = WSAGetLastError();
}
return rc;
} }
int socket_error() { int socket_writable(socket_t s) {
return WSAGetLastError(); fd_set writable;
FD_ZERO(&writable);
FD_SET(s->s, &writable);
struct timeval timeout = {0, 0};
int rc = select(0, 0, &writable, 0, &timeout);
if (rc < 0) {
s->last_error = WSAGetLastError();
}
return rc;
}
int socket_error(socket_t s) {
return s->last_error;
} }
#endif #endif
#ifdef __linux__ #ifdef __linux__

View File

@ -10,14 +10,19 @@
#define UUID_SIZE 16 #define UUID_SIZE 16
int srand_called = 0;
struct st_uuid { struct st_uuid {
char data[UUID_SIZE]; char data[UUID_SIZE];
}; };
//TODO: fixme - this is not secure, use crypto random bytes instead of rand //TODO: fixme - this is not secure, use crypto random bytes instead of rand
esc_uuid_t esc_uuid_create() { esc_uuid_t esc_uuid_create() {
if (srand_called == 0) {
srand(time(NULL));
srand_called = 1;
}
esc_uuid_t result = malloc(sizeof(struct st_uuid)); esc_uuid_t result = malloc(sizeof(struct st_uuid));
srand(time(NULL));
for(int i=0;i<UUID_SIZE;i++) for(int i=0;i<UUID_SIZE;i++)
result->data[i] = (uint8_t)(rand() & 0xff); result->data[i] = (uint8_t)(rand() & 0xff);
return result; return result;
@ -45,6 +50,16 @@ int esc_uuid_compare(esc_uuid_t left, esc_uuid_t right) {
} }
const char* esc_uuid_format(esc_uuid_t uuid, char* buffer, size_t buf_size) { const char* esc_uuid_format(esc_uuid_t uuid, char* buffer, size_t buf_size) {
#ifdef _WIN32
snprintf(buffer, buf_size, "%08lx-%04hx-%04hx-%04hx-%08lx%04hx",
*(long*)&uuid->data[0],
*(short*)&uuid->data[4],
*(short*)&uuid->data[6],
*(short*)&uuid->data[8],
*(long*)&uuid->data[10],
*(short*)&uuid->data[14]);
#endif
#ifdef __linux__
snprintf(buffer, buf_size, "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx", snprintf(buffer, buf_size, "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
uuid->data[0], uuid->data[0],
uuid->data[1], uuid->data[1],
@ -62,5 +77,6 @@ const char* esc_uuid_format(esc_uuid_t uuid, char* buffer, size_t buf_size) {
uuid->data[13], uuid->data[13],
uuid->data[14], uuid->data[14],
uuid->data[15]); uuid->data[15]);
#endif
return buffer; return buffer;
} }