Commit 75318a0a authored by Arnaud Blanchard's avatar Arnaud Blanchard

various changes for blc_channels

parent 3d91a4fa
......@@ -22,17 +22,19 @@ typedef struct blc_server
Each time the server receive data on the associated port it will call the callback with the data server as parameter.
You can therefore read the data with server->data getting the size with server->receive_data, your arg with server->callback_arg, ...
*/
void start(char const *port_name, int mode, void(*on_receive_data)(blc_server*) , void *arg, size_t buffer_size=0);
blc_server() = default;
//buffer_size '-1' for automatic size (sizeo of the rechv buffer)
blc_server(char const *port_name, int protocol, void(*on_receive_data)(blc_server*), void *arg, ssize_t buffer_size=0);
~blc_server();
//buffer_size '-1' for automatic size (sizeo of the rechv buffer)
void start(char const *port_name, int mode, void(*on_receive_data)(blc_server*) , void *arg, ssize_t buffer_size=0);
/**stop (close(socket_fd)) the server*/
void stop();
/** stop the server and free the allocated receive buffer*/
void stop_and_free_buffer();
void send_back();
// void send_back();
void send_back_buffer(void const *buffer, size_t size);
// void send_back_buffer(void const *buffer, size_t size);
#else
{ blc_network network;
#endif
......
......@@ -40,17 +40,20 @@ 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() = default;
blc_network(char const *address_name, char const *port_name, int mode, ssize_t buffer_size);
~blc_network();
void init(char const *address_name, char const *port_name, int mode, size_t buffer_size=0);
void send();
void init(char const *address_name, char const *port_name, int mode, ssize_t buffer_size=0);
void send(ssize_t send_size=-1);
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);
// Return 1 if data has been read, 0 otherwise (timeout)
bool recv_buffer_timeout(void *buffer, size_t size, uint64_t us_timeout);
bool recv_timeout(uint64_t us_timeout);
void recv_buffer_block(void *buffer, size_t size);
void recv_block();
void get_remote_address(char *channel_name, socklen_t name_size, char *port_name, socklen_t port_name_size) const;
int get_mtu();
#else
{ blc_mem mem;
......@@ -59,7 +62,7 @@ typedef struct blc_network
struct sockaddr_storage remote_address;
socklen_t remote_address_length;
int socket_fd;
size_t received_data;
size_t received_size;
}blc_network;
START_EXTERN_C
......
......@@ -14,6 +14,8 @@
#include <netdb.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <net/if.h>
static int blc_vset_iovec(struct iovec *iov, void *data, size_t size, va_list arguments){
......@@ -57,6 +59,7 @@ blc_network::blc_network(char const *address_name, char const *port_name, int mo
/* 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, ssize_t buffer_size){
struct addrinfo hints, *results;
int ret;
struct sockaddr_storage address;
socklen_t address_size;
......@@ -84,6 +87,8 @@ void blc_network::init(char const *address_name, char const *port_name, int mode
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);
......@@ -91,6 +96,8 @@ void blc_network::init(char const *address_name, char const *port_name, int mode
}
freeaddrinfo(results);
if (buffer_size==-1) {
SYSTEM_ERROR_CHECK(getsockopt(socket_fd, SOL_SOCKET, SO_RCVBUF, &system_buffer_size, &len), -1, nullptr);
allocate(system_buffer_size);
......@@ -98,6 +105,16 @@ void blc_network::init(char const *address_name, char const *port_name, int mode
else if (buffer_size!=0) allocate(buffer_size);
}
/** IN fact it is more complex, a specific connexion can use several interface therefore MTU
int blc_network::get_mtu(){
struct ifreq interface_info;
// SYSTEM_SUCCESS_CHECK (ioctl(socket_fd, SIOCGIFINDEX, &interface_info), 0, nullptr);
// SYSTEM_SUCCESS_CHECK (ioctl(socket_fd, SIOCGIFALIAS, &interface_info), 0, nullptr);
interface_info.
SYSTEM_SUCCESS_CHECK (ioctl(socket_fd, SIOCSIFMTU, &interface_info), 0, nullptr);
return interface_info.ifr_mtu;
}*/
void blc_network::get_remote_address(char *channel_name, socklen_t name_size, char *port_name, socklen_t port_name_size) const{
int ret;
......@@ -151,7 +168,6 @@ void blc_network::send_buffer(void const* buffer, size_t buffer_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);
......
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