Commit d6306e93 by Marcus Winter Committed by unknown

mtapi_network_c: added linux socket support

parent 54dfc55d
#include <embb_mtapi_network.h> #include <embb_mtapi_network.h>
#ifdef _WIN32
#include <WinSock2.h> #include <WinSock2.h>
#endif
int embb_mtapi_network_initialize() { int embb_mtapi_network_initialize() {
#ifdef _WIN32
WORD ver_request; WORD ver_request;
WSADATA wsa_data; WSADATA wsa_data;
int err; int err;
...@@ -15,8 +18,13 @@ int embb_mtapi_network_initialize() { ...@@ -15,8 +18,13 @@ int embb_mtapi_network_initialize() {
} else { } else {
return 1; return 1;
} }
#else
return 1;
#endif
} }
void embb_mtapi_network_finalize() { void embb_mtapi_network_finalize() {
#ifdef _WIN32
WSACleanup(); WSACleanup();
#endif
} }
...@@ -2,7 +2,19 @@ ...@@ -2,7 +2,19 @@
#include <embb_mtapi_network_socket.h> #include <embb_mtapi_network_socket.h>
#include <string.h> #include <string.h>
#ifdef _WIN32
#include <WinSock2.h> #include <WinSock2.h>
#else
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#endif
int embb_mtapi_network_socket_initialize( int embb_mtapi_network_socket_initialize(
embb_mtapi_network_socket_t * that) { embb_mtapi_network_socket_t * that) {
...@@ -17,7 +29,11 @@ int embb_mtapi_network_socket_initialize( ...@@ -17,7 +29,11 @@ int embb_mtapi_network_socket_initialize(
void embb_mtapi_network_socket_finalize( void embb_mtapi_network_socket_finalize(
embb_mtapi_network_socket_t * that) { embb_mtapi_network_socket_t * that) {
if (INVALID_SOCKET != that->handle) { if (INVALID_SOCKET != that->handle) {
#ifdef _WIN32
closesocket(that->handle); closesocket(that->handle);
#else
close(that->handle);
#endif
that->handle = INVALID_SOCKET; that->handle = INVALID_SOCKET;
} }
} }
...@@ -78,7 +94,11 @@ int embb_mtapi_network_socket_connect( ...@@ -78,7 +94,11 @@ int embb_mtapi_network_socket_connect(
if (SOCKET_ERROR == connect(that->handle, (struct sockaddr *)&addr, if (SOCKET_ERROR == connect(that->handle, (struct sockaddr *)&addr,
sizeof(addr))) { sizeof(addr))) {
#ifdef _WIN32
if (WSAEWOULDBLOCK != WSAGetLastError()) if (WSAEWOULDBLOCK != WSAGetLastError())
#else
if (EAGAIN != errno)
#endif
return 0; return 0;
} }
...@@ -101,9 +121,9 @@ int embb_mtapi_network_socket_select( ...@@ -101,9 +121,9 @@ int embb_mtapi_network_socket_select(
max_fd = that->handle; max_fd = that->handle;
if (timeout >= 0) { if (timeout >= 0) {
err = select(max_fd, &read_set, NULL, NULL, &tv); err = select(max_fd + 1, &read_set, NULL, NULL, &tv);
} else { } else {
err = select(max_fd, &read_set, NULL, NULL, NULL); err = select(max_fd + 1, &read_set, NULL, NULL, NULL);
} }
if (0 == err) { if (0 == err) {
// timeout // timeout
...@@ -134,9 +154,14 @@ int embb_mtapi_network_socket_sendbuffer( ...@@ -134,9 +154,14 @@ int embb_mtapi_network_socket_sendbuffer(
int embb_mtapi_network_socket_recvbuffer( int embb_mtapi_network_socket_recvbuffer(
embb_mtapi_network_socket_t * that, embb_mtapi_network_socket_t * that,
embb_mtapi_network_buffer_t * buffer) { embb_mtapi_network_buffer_t * buffer) {
u_long bytes_available = 0;
int err; int err;
#ifdef _WIN32
u_long bytes_available = 0;
if (0 != ioctlsocket(that->handle, FIONREAD, &bytes_available)) if (0 != ioctlsocket(that->handle, FIONREAD, &bytes_available))
#else
int bytes_available = 0;
if (0 != ioctl(that->handle, FIONREAD, &bytes_available))
#endif
return 0; return 0;
if (buffer->capacity > (int)bytes_available) if (buffer->capacity > (int)bytes_available)
return 0; return 0;
......
...@@ -36,7 +36,11 @@ extern "C" { ...@@ -36,7 +36,11 @@ extern "C" {
struct embb_mtapi_network_socket_struct { struct embb_mtapi_network_socket_struct {
#ifdef _WIN32
unsigned int handle; unsigned int handle;
#else
int handle;
#endif
}; };
typedef struct embb_mtapi_network_socket_struct embb_mtapi_network_socket_t; typedef struct embb_mtapi_network_socket_struct embb_mtapi_network_socket_t;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment