Commit 3d91a4fa authored by Arnaud Blanchard's avatar Arnaud Blanchard

Improvements for blc_channels networt

parent 946abfc0
......@@ -22,7 +22,7 @@ set(sources
find_package(blc_core REQUIRED)
add_definitions(-Wall ${BL_DEFINITIONS})
add_definitions(-Wall ${BL_DEFINITIONS} -std=c++14 -W -Wall)
include_directories(include ${BL_INCLUDE_DIRS})
add_library(blc_network SHARED ${sources})
......
......@@ -13,6 +13,7 @@
#include <pthread.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/errno.h>
static int blc_vset_iovec(struct iovec *iov, void *data, size_t size, va_list arguments){
......@@ -49,12 +50,18 @@ void blc_set_msghdr(struct msghdr *msghdr, void *data, size_t size, ...)
}
blc_network::blc_network(char const *address_name, char const *port_name, int mode, ssize_t buffer_size){
init(address_name, port_name, mode,buffer_size);
}
/* In the default case (client) we do not use connect to be able to read response from the server*/
void blc_network::init(char const *address_name, char const *port_name, int mode, size_t buffer_size){
void blc_network::init(char const *address_name, char const *port_name, int mode, ssize_t buffer_size){
struct addrinfo hints, *results;
int ret;
struct sockaddr_storage address;
socklen_t address_size;
size_t system_buffer_size=0;
socklen_t len=sizeof(system_buffer_size);
CLEAR(hints);
hints.ai_flags = AI_NUMERICSERV; //We use only port number not service name ( faster )
......@@ -84,17 +91,33 @@ void blc_network::init(char const *address_name, char const *port_name, int mode
}
freeaddrinfo(results);
if (buffer_size) allocate(buffer_size);
if (buffer_size==-1) {
SYSTEM_ERROR_CHECK(getsockopt(socket_fd, SOL_SOCKET, SO_RCVBUF, &system_buffer_size, &len), -1, nullptr);
allocate(system_buffer_size);
}
else if (buffer_size!=0) allocate(buffer_size);
}
void blc_network::get_remote_address(char *channel_name, socklen_t name_size, char *port_name, socklen_t port_name_size){
void blc_network::get_remote_address(char *channel_name, socklen_t name_size, char *port_name, socklen_t port_name_size) const{
int ret;
ret=getnameinfo((const struct sockaddr *)&remote_address, remote_address_length, channel_name, name_size, port_name, port_name_size, NI_NUMERICSERV);
if (ret !=0) EXIT_ON_ERROR(gai_strerror(ret)); //, 0, "setting address: %s", address_name));
}
int blc_network::recv_buffer_timeout(void *buffer, size_t buffer_size, uint64_t us_timeout){
void blc_network::recv_buffer_block(void *buffer, size_t buffer_size){
ssize_t ret;
SYSTEM_ERROR_CHECK(ret=recv(socket_fd, buffer, buffer_size, MSG_TRUNC), -1, "Socket '%d', data: '%p', size: '%lu'", socket_fd, data, size);
if ((size_t)ret > buffer_size) EXIT_ON_ERROR("Buffer too small '%lu' bytes with a buffer of '%lu' bytes", ret, size);
received_size=ret;
}
void blc_network::recv_block(){
recv_buffer_block(data, size);
}
bool 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;
......@@ -105,31 +128,45 @@ int blc_network::recv_buffer_timeout(void *buffer, size_t buffer_size, uint64_t
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;
if (ret==0) return false;
else{
SYSTEM_ERROR_CHECK(ret=recv(socket_fd, buffer, buffer_size, MSG_TRUNC), -1, "Socket '%d', data: '%p', size: '%lu'", socket_fd, data, size);
if (ret > (ssize_t)buffer_size) EXIT_ON_ERROR("Buffer too small '%lu' bytes with a buffer of '%lu' bytes", ret, size);
received_data=ret;
}
return 1;
if ((size_t)ret > size) EXIT_ON_ERROR("Buffer too small '%lu' bytes with a buffer of '%lu' bytes", ret, size);
}
return true;
}
int blc_network::recv_timeout(uint64_t us_timeout){
return recv_buffer_timeout(data,size, us_timeout );
}
void blc_network::send(){
if (this->data==NULL) EXIT_ON_ERROR("You have not allocated buffer.");
SYSTEM_ERROR_CHECK(sendto(socket_fd, this->data, this->size, 0, (struct sockaddr*)&remote_address, remote_address_length), -1, "socket_fd '%d', request first size '%lu'", socket_fd, remote_address_length);
bool blc_network::recv_timeout(uint64_t us_timeout){
return recv_buffer_timeout(data, size, us_timeout );
}
void blc_network::send_buffer(void const* buffer, size_t size){
void blc_network::send_buffer(void const* buffer, size_t buffer_size){
ssize_t ret;
size_t system_buffer_size=0;
socklen_t len=sizeof(system_buffer_size);
SYSTEM_ERROR_CHECK(ret=sendto(socket_fd, buffer, size, NO_FLAG, (struct sockaddr*)&remote_address, remote_address_length), -1, "socket_fd '%d', request size '%u'", socket_fd, size);
if ((size_t)ret!=size) EXIT_ON_ERROR("Only '%l' bytes sent instead of '%lu'", ret, size);
ret=sendto(socket_fd, buffer, buffer_size, NO_FLAG, (struct sockaddr*)&remote_address, remote_address_length);
if (ret==-1){
SYSTEM_ERROR_CHECK(getsockopt(socket_fd, SOL_SOCKET, SO_SNDBUF, &system_buffer_size, &len), -1, nullptr);
allocate(system_buffer_size);
if (errno==EMSGSIZE) EXIT_ON_SYSTEM_ERROR("socket_fd '%d', request size '%u' SNDBUFFER '%d'", socket_fd, buffer_size, system_buffer_size );
}
else if ((size_t)ret!=buffer_size) EXIT_ON_ERROR("Only '%l' bytes sent instead of '%lu'", ret, size);
}
void blc_network::send(ssize_t send_size){
if (this->data==NULL) EXIT_ON_ERROR("You have not allocated buffer.");
if (send_size==-1) send_size=size; //By default we send all
if ((size_t)send_size > this->size) EXIT_ON_ERROR("You try to send more '%ld' than the buffer size '%lu'", send_size, size);
send_buffer(this->data, send_size);
}
blc_network::~blc_network(){
close(socket_fd);
}
......
......@@ -17,22 +17,27 @@
static void* server_manager(void *arg)
{
blc_server *server=(blc_server*)arg;
ssize_t received_data_size;
ssize_t ret;
if (server->data==NULL) EXIT_ON_ERROR("You have not allocated the receive buffer");
server->remote_address_length=sizeof(struct sockaddr_storage);
while(1) //pthread_cancel is used to stop it
while(1) //pthread_cancel is used to stop it even inside the recv
{
//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;
SYSTEM_ERROR_CHECK(ret = recvfrom(server->socket_fd, server->data , server->size, 0, (struct sockaddr*)&server->remote_address, &server->remote_address_length), -1, "Error receiving data. fd '%d' buffer size '%lu'", server->socket_fd, server->size);
server->received_size=(size_t)ret;
server->callback(server);
}
return NULL;
}
void blc_server::start(char const *port_name, int protocol, void(*on_receive_data)(blc_server*), void *arg, size_t buffer_size){
blc_server::blc_server(char const *port_name, int protocol, void(*on_receive_data)(blc_server*), void *arg, ssize_t buffer_size){
start(port_name, protocol, on_receive_data, arg, buffer_size);
};
void blc_server::start(char const *port_name, int protocol, void(*on_receive_data)(blc_server*), void *arg, ssize_t buffer_size){
callback = on_receive_data;
callback_arg = arg;
......@@ -50,17 +55,8 @@ void blc_server::stop(){
close(socket_fd);
}
void blc_server::stop_and_free_buffer(){
stop();
}
void blc_server::send_back(){
SYSTEM_ERROR_CHECK(sendto(socket_fd, data, size , NO_FLAG, (struct sockaddr*)&remote_address, remote_address_length), -1, "socket %d", socket_fd);
}
void blc_server::send_back_buffer(void const *buffer, size_t buffer_size){
// 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);
blc_server::~blc_server(){
if (thread) stop();
}
/* Network */
......
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