Example #1
0
/*
Description: Initialization function to create the socket handles
and set the buffer size

Arguments:
	node_sock(int*)				- socket handle array
	numNodes (int)				- number of nodes
*/
void nodes_initialize(int* node_sock , int numNodes){

	if(!initialized){

	   // initalized is a static global variable
	   // all sockets are memset'ed with this function call
       init_wl_mex_udp_transport();
  	}



	int num;
	for (num= 0; num < numNodes; num++){
		node_sock[num] = init_socket(); // socket handle for each node


		int REQUESTED_BUFF_SIZE = pow(2,24);

		// send buffer config
		set_send_buffer_size( node_sock[num], REQUESTED_BUFF_SIZE );
		int os_size = get_send_buffer_size(node_sock[num]);

		if  (os_size < REQUESTED_BUFF_SIZE){
			 // printf("OS reduced send buff for sock %d to %d \n", node_sock[num], os_size);
		}

		// receive buffer config
		set_receive_buffer_size( node_sock[num], REQUESTED_BUFF_SIZE );
		os_size = get_receive_buffer_size( node_sock[num]);

		if  (os_size < REQUESTED_BUFF_SIZE){
			 // printf("OS reduced send buff for sock %d to %d \n", node_sock[num], os_size);
		}
	}
}
int linux_event_loop<handler>::add_tcp(
    std::string ip, 
    std::string port,
    int rec_buf_size,
    int send_buf_size,
    bool enabled)
{ 

    int err;
    struct addrinfo hint;
    bzero(&hint, sizeof(struct addrinfo));    
    hint.ai_family = AF_INET;
    hint.ai_socktype = SOCK_STREAM;
    struct addrinfo * ailist;
    if((err = getaddrinfo(ip.c_str(), port.c_str(), &hint, &ailist)) < 0)
    {
        std::cerr << "cannot add_tcp : getaddrinfo error: " << std::endl;
        return -1;
    }

    int socket_fd;
    if( (socket_fd = socket(AF_INET, SOCK_STREAM, 0 )) < 0 ) 
    {
        std::cerr << "cannot create socket:  socket error: " << std::endl; 
        return -1;
    }

    if(!set_recv_buffer_size(socket_fd, rec_buf_size))
        return -1;

    if(!set_send_buffer_size(socket_fd, send_buf_size))
        return -1;

    err = connect(socket_fd, ailist->ai_addr, ailist->ai_addrlen);

    if(err < 0)
    {
        std::cerr << "connect: " << strerror(err) << std::endl;
        close(socket_fd);
        return -1;
    }
    
    connection c;
    c.ip = ip;
    c.port = port;
    c.type = TCP;
    c.is_enabled = false;
    c.ev.events = EPOLLIN | EPOLLRDHUP;
    c.ev.data.fd = socket_fd;
    _cons[socket_fd] =  c;

    if( enabled && ( enable( socket_fd ) < 0 ) )
        return -1;
    
    return socket_fd; 
}