project_files/frontlib/net/netbase.c
author Medo <smaxein@googlemail.com>
Tue, 19 Jun 2012 21:17:05 +0200
changeset 7234 613998625a3c
child 7275 15f722e0b96f
permissions -rw-r--r--
frontlib: Started work on the server connection code
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7234
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
#include "netbase.h"
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
#include "../util/buffer.h"
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
     3
#include "../util/logging.h"
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
     4
#include "../util/util.h"
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
     5
#include "../socket.h"
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
     6
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
     7
#include <string.h>
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
     8
#include <stdbool.h>
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
     9
#include <stdlib.h>
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
#include <stdio.h>
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
#define NET_READBUFFER_LIMIT (1024*1024)
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
struct _flib_netbase {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
	flib_vector *readBuffer;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
	flib_tcpsocket *sock;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
};
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    18
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    19
flib_netbase *flib_netbase_create(const char *server, uint16_t port) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
	flib_netbase *result = NULL;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
	flib_netbase *newNet =  flib_calloc(1, sizeof(flib_netbase));
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
	if(newNet) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
		newNet->readBuffer = flib_vector_create();
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
		newNet->sock = flib_socket_connect(server, port);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
		if(newNet->readBuffer && newNet->sock) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
			flib_log_i("Connected to server %s:%u", server, (unsigned)port);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
			result = newNet;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
			newNet = NULL;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
		}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
	}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
	flib_netbase_destroy(newNet);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
	return result;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
void flib_netbase_destroy(flib_netbase *net) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
	if(net) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
		flib_socket_close(net->sock);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
		flib_vector_destroy(net->readBuffer);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
		free(net);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
	}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
bool flib_netbase_connected(flib_netbase *net) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
	if(!net) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
		flib_log_e("null parameter in flib_netbase_connected");
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
		return false;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
	} else if(net->sock) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
		return true;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
	} else {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
		return false;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    53
	}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
/**
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
 * Parses and returns a message, and removes it from the vector.
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
 */
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
static flib_netmsg *parseMessage(flib_vector *vec) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
	const uint8_t *partStart = flib_vector_data(vec);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    61
	const uint8_t *end = partStart+flib_vector_size(vec);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
	flib_netmsg *result = flib_netmsg_create();
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    63
	if(!result) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
		return NULL;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
	}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
	while(1) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
		const uint8_t *partEnd = memchr(partStart, '\n', end-partStart);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
		if(!partEnd) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    70
			// message incomplete
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
			flib_netmsg_destroy(result);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
			return NULL;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    73
		} else if(partEnd-partStart == 0) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    74
			// Zero-length part, message end marker. Remove the message from the vector.
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    75
			uint8_t *vectorStart = flib_vector_data(vec);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    76
			size_t msgLen = partEnd+1-vectorStart;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
			memmove(vectorStart, partEnd+1, flib_vector_size(vec)-msgLen);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
			flib_vector_resize(vec, flib_vector_size(vec)-msgLen);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
			return result;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    80
		} else {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    81
			if(flib_netmsg_append_part(result, partStart, partEnd-partStart)) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    82
				flib_netmsg_destroy(result);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    83
				return NULL;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    84
			}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    85
			partStart = partEnd+1; // Skip the '\n'
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    86
		}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    87
	}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    88
	return NULL; // Never reached
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    89
}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    90
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    91
/**
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    92
 * Receive some bytes and add them to the buffer.
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    93
 * Returns the number of bytes received.
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    94
 * Automatically closes the socket if an error occurs
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    95
 * and sets sock=NULL.
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    96
 */
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    97
static int receiveToBuffer(flib_netbase *net) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    98
	uint8_t buffer[256];
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
    99
	if(!net->sock) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   100
		return 0;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   101
	} else if(flib_vector_size(net->readBuffer) > NET_READBUFFER_LIMIT) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   102
		flib_log_e("Net connection closed: Net message too big");
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   103
		flib_socket_close(net->sock);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   104
		net->sock = NULL;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   105
		return 0;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   106
	} else {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   107
		int size = flib_socket_nbrecv(net->sock, buffer, sizeof(buffer));
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   108
		if(size>=0) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   109
			flib_vector_append(net->readBuffer, buffer, size);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   110
			return size;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   111
		} else {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   112
			flib_socket_close(net->sock);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   113
			net->sock = NULL;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   114
			return 0;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   115
		}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   116
	}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   117
}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   118
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   119
flib_netmsg *flib_netbase_recv_message(flib_netbase *net) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   120
	if(!net) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   121
		flib_log_e("null parameter in flib_netbase_recv_message");
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   122
		return NULL;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   123
	}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   124
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   125
	flib_netmsg *msg;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   126
	while(!(msg=parseMessage(net->readBuffer))
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   127
			&& receiveToBuffer(net)) {}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   128
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   129
	if(msg) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   130
		return msg;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   131
	} else if(!net->sock && flib_vector_size(net->readBuffer)>0) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   132
		// Connection is down and we didn't get a complete message, just flush the rest.
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   133
		flib_vector_resize(net->readBuffer, 0);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   134
	}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   135
	return NULL;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   136
}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   137
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   138
static void logSentMsg(const uint8_t *data, size_t len) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   139
	if(flib_log_isActive(FLIB_LOGLEVEL_DEBUG)) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   140
		flib_log_d("[NET OUT][%03u]%*.*s",(unsigned)len, (unsigned)len, (unsigned)len, data);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   141
	}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   142
}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   143
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   144
int flib_netbase_send_raw(flib_netbase *net, const void *data, size_t len) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   145
	if(!net || (!data && len>0)) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   146
		flib_log_e("null parameter in flib_netbase_send_raw");
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   147
		return -1;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   148
	}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   149
	if(!net->sock) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   150
		flib_log_w("flib_netbase_send_raw: Not connected.");
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   151
		return -1;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   152
	}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   153
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   154
	if(flib_socket_send(net->sock, data, len) == len) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   155
		logSentMsg(data, len);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   156
		return 0;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   157
	} else {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   158
		flib_log_w("Failed or incomplete write: net connection lost.");
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   159
		flib_socket_close(net->sock);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   160
		net->sock = NULL;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   161
		return -1;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   162
	}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   163
}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   164
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   165
int flib_netbase_send_message(flib_netbase *net, flib_netmsg *msg) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   166
	if(!net || !msg) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   167
		flib_log_e("null parameter in flib_netbase_send_message");
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   168
		return -1;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   169
	}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   170
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   171
	size_t totalSize = 0;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   172
	for(int i=0; i<msg->partCount; i++) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   173
		totalSize += strlen(msg->parts[i]) + 1;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   174
	}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   175
	totalSize++; // Last part ends in two '\n' instead of one
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   176
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   177
	uint8_t *buffer = flib_malloc(totalSize);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   178
	if(!buffer) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   179
		return -1;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   180
	}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   181
	size_t pos = 0;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   182
	for(int i=0; i<msg->partCount; i++) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   183
		size_t partsize = strlen(msg->parts[i]);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   184
		memcpy(buffer+pos, msg->parts[i], partsize);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   185
		pos += partsize;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   186
		buffer[pos++] = '\n';
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   187
	}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   188
	buffer[pos++] = '\n';
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   189
	return flib_netbase_send_raw(net, buffer, pos);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   190
}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   191
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   192
int flib_netbase_sendf(flib_netbase *net, const char *format, ...) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   193
	int result = -1;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   194
	if(!net || !format) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   195
		flib_log_e("null parameter in flib_netbase_sendf");
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   196
	} else {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   197
		va_list argp;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   198
		va_start(argp, format);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   199
		char *buffer = flib_vasprintf(format, argp);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   200
		if(buffer) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   201
			result = flib_netbase_send_raw(net, buffer, strlen(buffer));
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   202
		}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   203
		free(buffer);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   204
		va_end(argp);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   205
	}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   206
	return result;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   207
}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   208
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   209
flib_netmsg *flib_netmsg_create() {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   210
	flib_netmsg *result = flib_calloc(1, sizeof(flib_netmsg));
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   211
	if(result) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   212
		result->partCount = 0;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   213
		result->parts = NULL;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   214
		return result;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   215
	} else {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   216
		return NULL;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   217
	}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   218
}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   219
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   220
void flib_netmsg_destroy(flib_netmsg *msg) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   221
	if(msg) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   222
		for(int i=0; i<msg->partCount; i++) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   223
			free(msg->parts[i]);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   224
		}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   225
		free(msg->parts);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   226
		free(msg);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   227
	}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   228
}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   229
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   230
int flib_netmsg_append_part(flib_netmsg *msg, const void *part, size_t partlen) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   231
	int result = -1;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   232
	if(!msg) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   233
		flib_log_e("null parameter in flib_netmsg_append_part");
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   234
	} else {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   235
		char **newParts = realloc(msg->parts, (msg->partCount+1)*sizeof(*msg->parts));
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   236
		if(newParts) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   237
			msg->parts = newParts;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   238
			msg->parts[msg->partCount] = flib_malloc(partlen+1);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   239
			if(msg->parts[msg->partCount]) {
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   240
				memcpy(msg->parts[msg->partCount], part, partlen);
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   241
				msg->parts[msg->partCount][partlen] = 0;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   242
				msg->partCount++;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   243
				result = 0;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   244
			}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   245
		}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   246
	}
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   247
	return result;
613998625a3c frontlib: Started work on the server connection code
Medo <smaxein@googlemail.com>
parents:
diff changeset
   248
}