Commit 5de9de9d authored by Arnaud Blanchard's avatar Arnaud Blanchard

Add possibility to know the size of the received message. We should use better callback properties

parent 3ae87b39
......@@ -19,7 +19,7 @@
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include "blc_mem.h"
#include "blc_core.h"
/*By default it is a client*/
#define BLC_SERVER 1
......@@ -44,15 +44,13 @@ typedef struct blc_network
**port_name** is a port number as a string of a decimal number.
If buffer_size=0, you are responsible for allocating the memory in .data (use .allocate(size) for example).
*/
~blc_network();
void init(char const *address_name, char const *port_name, int mode, size_t buffer_size=0);
void send();
void send_buffer(void const* data, size_t size);
void get_remote_address(char *channel_name, socklen_t name_size, char *port_name, socklen_t port_name_size);
void close();
void close_and_free_buffer();
#else
{ blc_mem mem;
#endif
......@@ -87,6 +85,7 @@ typedef struct blc_server
pthread_t thread;
void(*callback)(blc_server*);
void *callback_arg;
size_t received_data;
}blc_server;
START_EXTERN_C
......@@ -100,4 +99,4 @@ void blc_server_allocate_mem_and_start(blc_server *server, char const *port_name
END_EXTERN_C
#endif
///@}
\ No newline at end of file
///@}
......@@ -26,6 +26,7 @@ static void* server_manager(void *arg)
{
//What if bufffer is too small ?
SYSTEM_ERROR_CHECK(received_data_size = recvfrom(server->socket_fd, server->data, server->size, 0, (struct sockaddr*)&server->remote_address, &server->remote_address_length), -1, "Error receiving data.");
server->received_data=received_data_size;
server->callback(server);
}
return NULL;
......@@ -126,12 +127,8 @@ void blc_network::send_buffer(void const* buffer, size_t size){
if ((size_t)ret!=size) EXIT_ON_ERROR("Only '%l' bytes sent instead of '%lu'", ret, size);
}
void blc_network::close(){
::close(socket_fd);
}
void blc_network::close_and_free_buffer(){
this->close();
blc_network::~blc_network(){
close(socket_fd);
}
void blc_server::start(char const *port_name, int protocol, void(*on_receive_data)(blc_server*), void *arg, size_t buffer_size){
......@@ -150,7 +147,7 @@ void blc_server::stop(){
PTHREAD_CHECK(pthread_cancel(thread), error, NULL);
/*We wait that the thread has stopped before closing the socket*/
PTHREAD_CHECK(pthread_join(thread, NULL), error, NULL);
::close(socket_fd);
close(socket_fd);
}
void blc_server::stop_and_free_buffer(){
......@@ -166,7 +163,6 @@ void blc_server::send_back_buffer(void const *buffer, size_t buffer_size){
SYSTEM_ERROR_CHECK(sendto(socket_fd, buffer, buffer_size , NO_FLAG, (struct sockaddr*)&remote_address, remote_address_length), -1, "socket %d", socket_fd);
}
/* Network */
START_EXTERN_C
void blc_network_init(blc_network *network, char const *address_name, char const *port_name, int mode){
......
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 to comment