bool test_websocket_connect_error (Test *test)
{
        NetWebsocket *websocket;
        NetClientConnection connection;
        NetClient *client;

        TITLE ();
        CATCH (!(websocket = net_websocket_create (&on_add, 
                                                   &on_close, 
                                                   &on_request)));
        websocket->test.ConnectError = true;
        // Connect with client.
        CATCH (!(client = net_client_create (&client_on_connect,
                                             &client_on_connect_error,
                                             &client_on_error,
                                             NULL)));
        connection.ip = "127.0.0.1";
        connection.port = 8888;
        net_client_connect (client, &connection);
        CATCH (!thread_signal_wait (&client_ready));
        net_client_destroy (client);
        net_websocket_destroy (websocket);
        CATCH (error_at (0).error != ErrorFunctionCall);
        CATCH (!string_equals (error_at (0).function, "server_on_connect"));
        PASS ();
}
Ejemplo n.º 2
0
void client_start (size_t count, size_t _package_size, size_t _seconds)
{
        connections_max = count;
        package_size = _package_size;
        seconds = _seconds;
        
        buffer = memory_create (sizeof (char) * package_size);
        connections = memory_create (sizeof (NetClientConnection) * count);
        streams = memory_create (sizeof (NetStreamConnection *) * count);
        stream = net_stream_create (&client_stream_on_add, 
                                    &client_stream_on_close, 
                                    &client_stream_on_read,
                                    NULL);
        client = net_client_create (&client_on_connect,
                                    &client_on_connect_error,
                                    &client_on_error,
                                    NULL);
        (void)thread_create (&worker, NULL);
}
Ejemplo n.º 3
0
static net_client *net_server_accept(net_server *s) {
    struct sockaddr_in addr;
    socklen_t addrlen = sizeof(struct sockaddr_in);
    int fd;
    net_client *c = NULL;
    net_file_event *fe = NULL;
    
    if((fd = accept(s->fd, (struct sockaddr *) &addr, &addrlen)) == -1)
        goto err;

    if(fcntl(fd, F_SETFL, O_NONBLOCK) != 0)
        goto err;

    if((c = net_client_create(fd, addr)) == NULL)
        goto err;

    if((fe = net_file_event_create((void *) c, net_file_event_type_client)) == NULL)
        goto err;

    struct epoll_event ee;
    memset(&ee, 0, sizeof(struct epoll_event));
    ee.events |= EPOLLIN;
    ee.events |= EPOLLOUT;
    ee.data.u64 = 0;
    ee.data.ptr = (void *) fe;

    if(epoll_ctl(s->epfd, EPOLL_CTL_ADD, fd, &ee) != 0)
        goto err;

    return c;

err:
    if(c) 
        net_client_close(s, c);
    else if(fd)
        close(fd);

    if(fe) 
        net_file_event_destroy(fe);

    return NULL;
}
bool test_poll_close_2 (Test *test)
{
        NetPoll *poll;
        NetServer *server;
        NetClientConnection connection;
        NetClient *client;

        TITLE ();
        monitor_success = false;
        close_success = false;
        poll_connection.closed = false;
        poll_connection.socket = 0;
        poll_connection.tag = NULL;
        CATCH (!(server = net_server_create ("127.0.0.1", 8888,
                                             &server_on_connect,
                                             &server_on_error,
                                             NULL)));
        CATCH (!(client = net_client_create (&client_on_connect,
                                             &client_on_connect_error,
                                             &client_on_error,
                                             NULL)));
        connection.ip = "127.0.0.1";
        connection.port = 8888;
        net_client_connect (client, &connection);
        thread_signal_wait (&server_signal);
        CATCH (!(poll = net_poll_create (&on_monitor, &on_close, NULL, NULL)));
        poll_connection.socket = server_socket;
        CATCH (!net_poll_monitor (poll, &poll_connection));
        thread_signal_wait (&monitor_signal);
        CATCH (!monitor_success);
        CATCH (!net_poll_close (poll, &poll_connection));
        thread_signal_wait (&close_signal);
        CATCH (!close_success);
        net_poll_destroy (poll);
        close (server_socket);
        close (client_socket);
        net_server_destroy (server);
        net_client_destroy (client);
        CATCH (error_count () != 0);
        PASS ();
}
bool test_poll_write_2 (Test *test)
{
        NetPoll *poll;
        NetServer *server;
        NetClient *client;
        NetClientConnection connect;
        size_t i;

        TITLE ();
        poll_client.closed = false;
        poll_server.closed = false;
        read_count = 0;
        CATCH (!(server = net_server_create ("127.0.0.1", 8888,
                                             &server_on_connect,
                                             &server_on_error,
                                             NULL)));
        CATCH (!(client = net_client_create (&client_on_connect,
                                             &client_on_connect_error,
                                             &client_on_error,
                                             NULL)));
        CATCH (!(poll = net_poll_create (&poll_on_monitor, 
                                         &poll_on_close, 
                                         &poll_on_read,
                                         &poll_on_write)));

        // Allocate buffer
        CATCH (!(write_buffer = memory_create (BufferSize)));
        for (i = 0; i < BufferSize; i++) {
                write_buffer[i] = (unsigned char)i;
        }

        // Connect client.
        connect.ip = "127.0.0.1";
        connect.port = 8888;
        net_client_connect (client, &connect);

        // Wait for connections to be completed.
        thread_signal_wait (&client_connect_signal);
        thread_signal_wait (&server_connect_signal);

        // Poll client and server connection.
        poll_client.socket = client_socket;
        poll_server.socket = server_socket;
        CATCH (!net_poll_monitor (poll, &poll_client));
        CATCH (!net_poll_monitor (poll, &poll_server));

        // Wait for monitors to be completed.
        thread_signal_wait (&client_monitor_signal);
        thread_signal_wait (&server_monitor_signal);

        // Write and wait.
        CATCH (!net_poll_write (poll, &poll_server, write_buffer, BufferSize));
        thread_signal_wait (&client_read_signal);

        // We're done, let's close poll client.
        net_poll_close (poll, &poll_client);

        // Now wait for client and server poll to close.
        thread_signal_wait (&client_close_signal);
        thread_signal_wait (&server_close_signal);

        net_poll_destroy (poll);
        net_server_destroy (server);
        net_client_destroy (client);
        memory_destroy (write_buffer);
        CATCH (error_count () != 0);
        PASS ();
}
Ejemplo n.º 6
0
bool test_write (Test *test)
{
        NetServer *server;
        NetClient *client;
        NetClientConnection connect;
        NetStream *stream_server;
        NetStream *stream_client;
        unsigned char *buffer = (unsigned char *)"x";
        size_t i;

        TITLE ();
        write_count = 2000;
        read_count = 0;
        CATCH (!(server = net_server_create ("127.0.0.1", 8888,
                                             &server_on_connect,
                                             &server_on_error,
                                             NULL)));
        CATCH (!(client = net_client_create (&client_on_connect,
                                             &client_on_connect_error,
                                             &client_on_error,
                                             NULL)));

        // Connect client.
        connect.ip = "127.0.0.1";
        connect.port = 8888;
        net_client_connect (client, &connect);

        // Wait for connections to be completed.
        thread_signal_wait (&client_connect_signal);
        thread_signal_wait (&server_connect_signal);

        // Create streams.
        CATCH (!(stream_server = net_stream_create (&server_stream_on_add, 
                                                    &server_stream_on_close, 
                                                    &server_stream_on_read,
                                                    NULL)));
        CATCH (!(stream_client = net_stream_create (&client_stream_on_add, 
                                                    &client_stream_on_close, 
                                                    NULL,
                                                    NULL)));

        // Add sockets to streams.
        CATCH (!net_stream_add (stream_server, server_socket));
        CATCH (!net_stream_add (stream_client, client_socket));

        // Wait for add to be completed.
        thread_signal_wait (&client_add_signal);
        thread_signal_wait (&server_add_signal);

        // Write 2000 times.
        for (i = 0; i < write_count; i++) {
                CATCH (!net_stream_write (stream_client, 
                                          stream_connection_client,
                                          buffer,
                                          1));
        }

        thread_signal_wait (&server_read_signal);

        // Close client connection, should cause server to close as well.
        net_stream_close (stream_client, stream_connection_client);

        // Wait for close to be completed.
        thread_signal_wait (&client_close_signal);
        thread_signal_wait (&server_close_signal);

        // Remove closed streams.
        net_stream_remove (stream_server, stream_connection_server);
        net_stream_remove (stream_client, stream_connection_client);

        // We're done, let's destroy streams.
        net_stream_destroy (stream_server);
        net_stream_destroy (stream_client);

        net_server_destroy (server);
        net_client_destroy (client);
        CATCH (error_count () != 0);
        PASS ();
}