AzureMXChip/MXCHIPAZ3166/core/lib/netxduo/addons/web/nx_tcpserver.c
UINT _nx_tcpserver_create(NX_IP *ip_ptr, NX_TCPSERVER *server_ptr, CHAR *name, 
                          ULONG type_of_service, ULONG fragment, UINT time_to_live, ULONG window_size,
                          VOID (*new_connection)(NX_TCPSERVER *server_ptr, NX_TCP_SESSION *session_ptr),
                          VOID (*receive_data)(NX_TCPSERVER *server_ptr, NX_TCP_SESSION *session_ptr),
                          VOID (*connection_end)(NX_TCPSERVER *server_ptr, NX_TCP_SESSION *session_ptr),
                          VOID (*connection_timeout)(NX_TCPSERVER *server_ptr, NX_TCP_SESSION *session_ptr),
                          ULONG timeout, VOID *stack_ptr, UINT stack_size,
                          VOID *sessions_buffer, UINT buffer_size, UINT thread_priority, ULONG accept_wait_option)
{
UINT            i;
UINT            status;

    /* Initialize server struct. */
    server_ptr -> nx_tcpserver_ip = ip_ptr;
    server_ptr -> nx_tcpserver_sessions = sessions_buffer;
    server_ptr -> nx_tcpserver_sessions_count = buffer_size / sizeof(NX_TCP_SESSION);
    server_ptr -> nx_tcpserver_listen_port = 0;
    server_ptr -> nx_tcpserver_listen_session = NX_NULL;
    server_ptr -> nx_tcpserver_new_connection = new_connection;
    server_ptr -> nx_tcpserver_receive_data = receive_data;
    server_ptr -> nx_tcpserver_connection_end = connection_end;
    server_ptr -> nx_tcpserver_connection_timeout = connection_timeout;
    server_ptr -> nx_tcpserver_timeout = timeout;
    server_ptr -> nx_tcpserver_accept_wait_option = accept_wait_option;

    /* Create the tcpserver thread. */
    status = tx_thread_create(&server_ptr -> nx_tcpserver_thread, "TCPSERVER Thread",
                              _nx_tcpserver_thread_entry, (ULONG)server_ptr, stack_ptr,
                              stack_size, thread_priority, thread_priority, 
                              TX_NO_TIME_SLICE, TX_DONT_START);

    /* Create the tcpserver event flags. */
    status += tx_event_flags_create(&server_ptr -> nx_tcpserver_event_flags, "TCPSERVER Events");

    /* Create the timeout timer. */
    status += tx_timer_create(&server_ptr -> nx_tcpserver_timer, "TCPSERVER Timer",
                              _nx_tcpserver_timeout, (ULONG)server_ptr,
                              (NX_IP_PERIODIC_RATE * NX_TCPSERVER_TIMEOUT_PERIOD),
                              (NX_IP_PERIODIC_RATE * NX_TCPSERVER_TIMEOUT_PERIOD), TX_NO_ACTIVATE);

    /* Initialize buffer. */
    memset(sessions_buffer, 0, buffer_size);

    /* Initialize TCP sockets. */
    for(i = 0; i < server_ptr -> nx_tcpserver_sessions_count; i++)
    {
        status += nx_tcp_socket_create(ip_ptr, &server_ptr -> nx_tcpserver_sessions[i].nx_tcp_session_socket, name, type_of_service, fragment, time_to_live, 
                                       window_size, NX_NULL, _nx_tcpserver_disconnect_present);

        status += nx_tcp_socket_receive_notify(&server_ptr -> nx_tcpserver_sessions[i].nx_tcp_session_socket, _nx_tcpserver_data_present);

        server_ptr -> nx_tcpserver_sessions[i].nx_tcp_session_socket.nx_tcp_socket_reserved_ptr = server_ptr;
    }

    return status;
}
/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    nx_tcpserver_create                                  PORTABLE C     */
/*                                                           6.1          */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function creates a socket server instance to handle multiple   */
/*    incoming TCP or TLS client connections. The server utilizes an      */
/*    internal thread and manages a queue of TCP sockets (and TLS         */
/*    sessions if enabled) that are used to handle incoming requests.     */
/*    The callback routines are used to handle individual requests. Each  */
/*    socket (and TLS session) receives identical state so that each      */
/*    incoming request is handled consistently.                           */
/*                                                                        */
/*    The number of available sockets and TLS sessions is determined by   */
/*    the size of the sessions buffer and can be found by dividing the    */
/*    buffer size by the size of the NX_TCP_SESSION structure.            */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    ip_ptr                                IP instance for this server   */
/*    server_ptr                            Pointer to server structure   */
/*    name                                  Name string for this server   */
/*    type_of_service                       Type of service for TCP       */
/*                                            sockets                     */
/*    fragment                              Flag to enable IP fragmenting */
/*    time_to_live                          Time to live value for socket */
/*    window_size                           Size of socket's receive      */
/*                                            window                      */
/*    new_connection                        Callback invoked for new      */
/*                                            client connections          */
/*    receive_data                          Callback invoked when data is */
/*                                            received from the client    */
/*    connection_end                        Callback invoked when a       */
/*                                            connection is closed        */
/*    connection_timeout                    Callback invoked when a       */
/*                                            connection times out        */
/*    timeout                               Timeout value for all sockets */
/*    stack_ptr                             Stack buffer for internal     */
/*                                            server thread               */
/*    stack_size                            Size of thread stack buffer   */
/*    sessions_buffer                       Buffer for per-session data   */
/*    buffer_size                           Size of per-session buffer,   */
/*                                            determines session number   */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    tx_thread_create                     Create server thread           */
/*    tx_event_flags_create                Create thread event flags      */
/*    tx_timer_create                      Create timeout timer           */
/*    nx_tcp_socket_create                 Create TCP sockets             */
/*    nx_tcp_socket_receive_notify         Set TCP notification callback  */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
/*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
/*                                            resulting in version 6.1    */
/*                                                                        */
/**************************************************************************/
FUNCTION                                               RELEASE

nx_tcpserver_create                                  PORTABLE C
6.1
AUTHOR

Yuxin Zhou, Microsoft Corporation

DESCRIPTION

This function creates a socket server instance to handle multiple
incoming TCP or TLS client connections. The server utilizes an
internal thread and manages a queue of TCP sockets (and TLS
sessions if enabled) that are used to handle incoming requests.
The callback routines are used to handle individual requests. Each
socket (and TLS session) receives identical state so that each
incoming request is handled consistently.

The number of available sockets and TLS sessions is determined by
the size of the sessions buffer and can be found by dividing the
buffer size by the size of the NX_TCP_SESSION structure.

INPUT

ip_ptr                                IP instance for this server
server_ptr                            Pointer to server structure
name                                  Name string for this server
type_of_service                       Type of service for TCP
sockets
fragment                              Flag to enable IP fragmenting
time_to_live                          Time to live value for socket
window_size                           Size of socket's receive
window
new_connection                        Callback invoked for new
client connections
receive_data                          Callback invoked when data is
received from the client
connection_end                        Callback invoked when a
connection is closed
connection_timeout                    Callback invoked when a
connection times out
timeout                               Timeout value for all sockets
stack_ptr                             Stack buffer for internal
server thread
stack_size                            Size of thread stack buffer
sessions_buffer                       Buffer for per-session data
buffer_size                           Size of per-session buffer,
determines session number

OUTPUT

status                                Completion status

CALLS

tx_thread_create                     Create server thread
tx_event_flags_create                Create thread event flags
tx_timer_create                      Create timeout timer
nx_tcp_socket_create                 Create TCP sockets
nx_tcp_socket_receive_notify         Set TCP notification callback

CALLED BY

Application Code

RELEASE HISTORY

DATE              NAME                      DESCRIPTION

05-19-2020     Yuxin Zhou               Initial Version 6.0
09-30-2020     Yuxin Zhou               Modified comment(s),
resulting in version 6.1
FUNCTION RELEASE nx_tcpserver_create PORTABLE C 6 . 1 AUTHOR Yuxin Zhou Microsoft Corporation DESCRIPTION This function creates a socket server instance to handle multiple incoming TCP or TLS client connections . The server utilizes an internal thread and manages a queue of TCP sockets ( and TLS sessions if enabled ) that are used to handle incoming requests . The callback routines are used to handle individual requests . Each socket ( and TLS session ) receives identical state so that each incoming request is handled consistently . The number of available sockets and TLS sessions is determined by the size of the sessions buffer and can be found by dividing the buffer size by the size of the NX_TCP_SESSION structure . INPUT ip_ptr IP instance for this server server_ptr Pointer to server structure name Name string for this server type_of_service Type of service for TCP sockets fragment Flag to enable IP fragmenting time_to_live Time to live value for socket window_size Size of socket ' s receive window new_connection Callback invoked for new client connections receive_data Callback invoked when data is received from the client connection_end Callback invoked when a connection is closed connection_timeout Callback invoked when a connection times out timeout Timeout value for all sockets stack_ptr Stack buffer for internal server thread stack_size Size of thread stack buffer sessions_buffer Buffer for per - session data buffer_size Size of per - session buffer determines session number OUTPUT status Completion status CALLS tx_thread_create Create server thread tx_event_flags_create Create thread event flags tx_timer_create Create timeout timer nx_tcp_socket_create Create TCP sockets nx_tcp_socket_receive_notify Set TCP notification callback CALLED BY Application Code RELEASE HISTORY DATE NAME DESCRIPTION 05 - 19 - 2020 Yuxin Zhou Initial Version 6 . 0 09 - 30 - 2020 Yuxin Zhou Modified comment ( s ) resulting in version 6 . 1
UINT _nx_tcpserver_create(NX_IP *ip_ptr, NX_TCPSERVER *server_ptr, CHAR *name, 
                          ULONG type_of_service, ULONG fragment, UINT time_to_live, ULONG window_size,
                          VOID (*new_connection)(NX_TCPSERVER *server_ptr, NX_TCP_SESSION *session_ptr),
                          VOID (*receive_data)(NX_TCPSERVER *server_ptr, NX_TCP_SESSION *session_ptr),
                          VOID (*connection_end)(NX_TCPSERVER *server_ptr, NX_TCP_SESSION *session_ptr),
                          VOID (*connection_timeout)(NX_TCPSERVER *server_ptr, NX_TCP_SESSION *session_ptr),
                          ULONG timeout, VOID *stack_ptr, UINT stack_size,
                          VOID *sessions_buffer, UINT buffer_size, UINT thread_priority, ULONG accept_wait_option)
{
UINT            i;
UINT            status;
    server_ptr -> nx_tcpserver_ip = ip_ptr;
    server_ptr -> nx_tcpserver_sessions = sessions_buffer;
    server_ptr -> nx_tcpserver_sessions_count = buffer_size / sizeof(NX_TCP_SESSION);
    server_ptr -> nx_tcpserver_listen_port = 0;
    server_ptr -> nx_tcpserver_listen_session = NX_NULL;
    server_ptr -> nx_tcpserver_new_connection = new_connection;
    server_ptr -> nx_tcpserver_receive_data = receive_data;
    server_ptr -> nx_tcpserver_connection_end = connection_end;
    server_ptr -> nx_tcpserver_connection_timeout = connection_timeout;
    server_ptr -> nx_tcpserver_timeout = timeout;
    server_ptr -> nx_tcpserver_accept_wait_option = accept_wait_option;
    status = tx_thread_create(&server_ptr -> nx_tcpserver_thread, "TCPSERVER Thread",
                              _nx_tcpserver_thread_entry, (ULONG)server_ptr, stack_ptr,
                              stack_size, thread_priority, thread_priority, 
                              TX_NO_TIME_SLICE, TX_DONT_START);
    status += tx_event_flags_create(&server_ptr -> nx_tcpserver_event_flags, "TCPSERVER Events");
    status += tx_timer_create(&server_ptr -> nx_tcpserver_timer, "TCPSERVER Timer",
                              _nx_tcpserver_timeout, (ULONG)server_ptr,
                              (NX_IP_PERIODIC_RATE * NX_TCPSERVER_TIMEOUT_PERIOD),
                              (NX_IP_PERIODIC_RATE * NX_TCPSERVER_TIMEOUT_PERIOD), TX_NO_ACTIVATE);
    memset(sessions_buffer, 0, buffer_size);
    for(i = 0; i < server_ptr -> nx_tcpserver_sessions_count; i++)
    {
        status += nx_tcp_socket_create(ip_ptr, &server_ptr -> nx_tcpserver_sessions[i].nx_tcp_session_socket, name, type_of_service, fragment, time_to_live, 
                                       window_size, NX_NULL, _nx_tcpserver_disconnect_present);
        status += nx_tcp_socket_receive_notify(&server_ptr -> nx_tcpserver_sessions[i].nx_tcp_session_socket, _nx_tcpserver_data_present);
        server_ptr -> nx_tcpserver_sessions[i].nx_tcp_session_socket.nx_tcp_socket_reserved_ptr = server_ptr;
    }
    return status;
}
UINT _nx_tcpserver_create ( NX_IP * ip_ptr , NX_TCPSERVER * server_ptr , CHAR * name , ULONG type_of_service , ULONG fragment , UINT time_to_live , ULONG window_size , VOID ( * new_connection ) ( NX_TCPSERVER * server_ptr , NX_TCP_SESSION * session_ptr ) , VOID ( * receive_data ) ( NX_TCPSERVER * server_ptr , NX_TCP_SESSION * session_ptr ) , VOID ( * connection_end ) ( NX_TCPSERVER * server_ptr , NX_TCP_SESSION * session_ptr ) , VOID ( * connection_timeout ) ( NX_TCPSERVER * server_ptr , NX_TCP_SESSION * session_ptr ) , ULONG timeout , VOID * stack_ptr , UINT stack_size , VOID * sessions_buffer , UINT buffer_size , UINT thread_priority , ULONG accept_wait_option ) { UINT i ; UINT status ; server_ptr -> nx_tcpserver_ip = ip_ptr ; server_ptr -> nx_tcpserver_sessions = sessions_buffer ; server_ptr -> nx_tcpserver_sessions_count = buffer_size / sizeof ( NX_TCP_SESSION ) ; server_ptr -> nx_tcpserver_listen_port = 0 ; server_ptr -> nx_tcpserver_listen_session = NX_NULL ; server_ptr -> nx_tcpserver_new_connection = new_connection ; server_ptr -> nx_tcpserver_receive_data = receive_data ; server_ptr -> nx_tcpserver_connection_end = connection_end ; server_ptr -> nx_tcpserver_connection_timeout = connection_timeout ; server_ptr -> nx_tcpserver_timeout = timeout ; server_ptr -> nx_tcpserver_accept_wait_option = accept_wait_option ; status = tx_thread_create ( & server_ptr -> nx_tcpserver_thread , " " , _nx_tcpserver_thread_entry , ( ULONG ) server_ptr , stack_ptr , stack_size , thread_priority , thread_priority , TX_NO_TIME_SLICE , TX_DONT_START ) ; status += tx_event_flags_create ( & server_ptr -> nx_tcpserver_event_flags , " " ) ; status += tx_timer_create ( & server_ptr -> nx_tcpserver_timer , " " , _nx_tcpserver_timeout , ( ULONG ) server_ptr , ( NX_IP_PERIODIC_RATE * NX_TCPSERVER_TIMEOUT_PERIOD ) , ( NX_IP_PERIODIC_RATE * NX_TCPSERVER_TIMEOUT_PERIOD ) , TX_NO_ACTIVATE ) ; memset ( sessions_buffer , 0 , buffer_size ) ; for ( i = 0 ; i < server_ptr -> nx_tcpserver_sessions_count ; i ++ ) { status += nx_tcp_socket_create ( ip_ptr , & server_ptr -> nx_tcpserver_sessions [ i ] . nx_tcp_session_socket , name , type_of_service , fragment , time_to_live , window_size , NX_NULL , _nx_tcpserver_disconnect_present ) ; status += nx_tcp_socket_receive_notify ( & server_ptr -> nx_tcpserver_sessions [ i ] . nx_tcp_session_socket , _nx_tcpserver_data_present ) ; server_ptr -> nx_tcpserver_sessions [ i ] . nx_tcp_session_socket . nx_tcp_socket_reserved_ptr = server_ptr ; } return status ; }
FUNCTION                                               RELEASE
FUNCTION RELEASE
/* Initialize server struct. */ /* Create the tcpserver thread. */ /* Create the tcpserver event flags. */ /* Create the timeout timer. */ /* Initialize buffer. */ /* Initialize TCP sockets. */