Commit 023dd212 authored by Arnaud Blanchard's avatar Arnaud Blanchard

Functions to receive ith timeout

parent 5de9de9d
......@@ -49,14 +49,19 @@ typedef struct 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);
// Return 1 if data as been read, 0 otherwise (timeout)
int recv_timeout(uint64_t us_timeout);
int recv_buffer_timeout(void *buffer, size_t size, uint64_t us_timeout);
void get_remote_address(char *channel_name, socklen_t name_size, char *port_name, socklen_t port_name_size);
#else
{ blc_mem mem;
#endif
struct sockaddr_storage remote_address;
socklen_t remote_address_length;
struct sockaddr_storage remote_address;
socklen_t remote_address_length;
int socket_fd;
}blc_network;
......
......@@ -6,6 +6,7 @@
//
//
#include "blc_network.h"
#include "blc_mem.h"
#include "blc_realtime.h"
......@@ -15,6 +16,44 @@
#include <netdb.h>
#include <sys/types.h>
int blc_network::recv_buffer_timeout(void *buffer, size_t buffer_size, uint64_t us_timeout){
fd_set fdset={{0}};
ssize_t ret;
struct timeval timeout;
div_t result;
FD_SET(socket_fd, &fdset);
result=div(us_timeout, 1000000);
timeout.tv_sec=result.quot;
timeout.tv_usec=result.rem;
SYSTEM_ERROR_CHECK(ret=select(socket_fd+1, &fdset, NULL, NULL, &timeout), -1, "Waiting server channel data"); //It would be nice to check errorfds
if (ret==0) return 0;
else{
SYSTEM_ERROR_CHECK(ret=read(socket_fd, buffer, buffer_size), -1, NULL);
if (ret != (ssize_t)buffer_size) EXIT_ON_ERROR("Reading data: '%lu' bytes instead of '%lu'", ret, buffer_size);
}
return 1;
}
int blc_network::recv_timeout(uint64_t us_timeout){
fd_set fdset = {{0}};
ssize_t ret;
struct timeval timeout;
div_t result;
FD_SET(socket_fd, &fdset);
result=div(us_timeout, 1000000);
timeout.tv_sec=result.quot;
timeout.tv_usec=result.rem;
SYSTEM_ERROR_CHECK(ret=select(socket_fd+1, &fdset, NULL, NULL, &timeout), -1, "Waiting server channel data"); //It would be nice to check errorfds
if (ret==0) return 0;
else{
SYSTEM_ERROR_CHECK(ret=read(socket_fd, data, size), -1, NULL);
if (ret != (ssize_t)size) EXIT_ON_ERROR("Reading data: '%lu' bytes instead of '%lu'", ret, size);
}
return 1;
}
static void* server_manager(void *arg)
{
blc_server *server=(blc_server*)arg;
......@@ -43,7 +82,7 @@ static int blc_vset_iovec(struct iovec *iov, void *data, size_t size, va_list ar
size = va_arg(arguments, size_t);
}
return len;
}
int blc_set_iovec(struct iovec *iovec, void *data, size_t size, ...)
{
......@@ -74,12 +113,12 @@ void blc_network::init(char const *address_name, char const *port_name, int mode
int ret;
struct sockaddr_storage address;
socklen_t address_size;
CLEAR(hints);
hints.ai_flags = AI_NUMERICSERV; //We use only port number not service name ( faster )
if (mode & BLC_SERVER) hints.ai_flags|= AI_PASSIVE; // if address is NULL it is ANY_ADDR otherwise. If not server it is loopback (~localhost)
switch (mode & BLC_NET_PROTOCOLE){
case BLC_UDP4:
hints.ai_family = PF_INET;
......@@ -93,10 +132,10 @@ void blc_network::init(char const *address_name, char const *port_name, int mode
if (ret !=0) EXIT_ON_ERROR(gai_strerror(ret)); //, 0, "setting address: %s", address_name));
if(results->ai_next != NULL) EXIT_ON_ERROR("There is more than one possible address. It is not yet implemented.");
SYSTEM_ERROR_CHECK(socket_fd = socket(results->ai_family, results->ai_socktype, results->ai_protocol), -1, NULL); //We suppose there is only one result in 'results'
address_size = sizeof(address);
SYSTEM_ERROR_CHECK(getsockname(socket_fd, (struct sockaddr*)&address, &address_size), -1, NULL);
if (mode & BLC_SERVER) SYSTEM_ERROR_CHECK(bind(socket_fd, results->ai_addr, results->ai_addrlen), -1, "Socket %d", socket_fd);
else { //We do not use connect to allow the port to also read answers
memcpy(&remote_address, (void*)results->ai_addr, results->ai_addrlen);
......@@ -148,7 +187,7 @@ void blc_server::stop(){
/*We wait that the thread has stopped before closing the socket*/
PTHREAD_CHECK(pthread_join(thread, NULL), error, NULL);
close(socket_fd);
}
}
void blc_server::stop_and_free_buffer(){
stop();
......@@ -159,7 +198,7 @@ void blc_server::send_back(){
}
void blc_server::send_back_buffer(void const *buffer, size_t buffer_size){
// printf("%s\n", (char*)buffer);
// printf("%s\n", (char*)buffer);
SYSTEM_ERROR_CHECK(sendto(socket_fd, buffer, buffer_size , NO_FLAG, (struct sockaddr*)&remote_address, remote_address_length), -1, "socket %d", socket_fd);
}
......
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