tcp_server
—
generic TCP server
PDEL Library (libpdel, -lpdel)
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <pdel/net/tcp_server.h>
struct tcp_server *
tcp_server_start
(struct
pevent_ctx *ctx, void
*cookie, const char
*mtype, struct in_addr
ip, u_int16_t port,
u_int max_conn,
u_int conn_timeout,
tcp_setup_t *setup,
tcp_handler_t *handler,
tcp_teardown_t
*teardown);
void
tcp_server_stop
(struct
tcp_server **servp);
void *
tcp_server_get_cookie
(struct
tcp_server *serv);
struct tcp_server *
tcp_connection_get_server
(struct
tcp_connection *conn);
void *
tcp_connection_get_cookie
(struct
tcp_connection *conn);
int
tcp_connection_get_fd
(struct
tcp_connection *conn);
FILE *
tcp_connection_get_fp
(struct
tcp_connection *conn);
void
tcp_connection_get_peer
(struct
tcp_connection *conn,
struct sockaddr_in
*sin);
These functions provide support for implementing TCP servers.
tcp_server_start
() creates a new TCP
server listening for connections on IP address ip and
port port. Memory for the
tcp_server
is dynamically allocated with
typed_mem(3)
type mtype. At most max_conn
simultaneous connections will be allowed. If
conn_timeout is non-zero, then if an attempt to read
from or write to a peer blocks for more than
conn_timeout seconds, the connection will be
terminated. ctx is a
pevent(3)
event context used to listen for incoming connections.
The setup, handler,
and teardown arguments are pointers to functions
having these types:
typedef void *tcp_setup_t(struct tcp_connection *conn);
typedef void tcp_handler_t(struct tcp_connection *conn);
typedef void tcp_teardown_t(struct tcp_connection *conn);
Each connection is represented by a
tcp_connection
object. For each new connection,
setup
() is called to initialize user state for the
connection. If setup is unsuccessful, setup
() should
return NULL
and set errno
appropriately, and the connection will be closed. Otherwise, it should
return a non-NULL cookie; this cookie can be accessed later in
handler
() and teardown
() by
calling tcp_connection_get_cookie
().
handler
() is used to service the
connection. The thread calling this function may be canceled at any
cancellation point; therefore, handler
() may need to
register cleanup hooks to clean up allocated resources should that
happen.
teardown
() is called when
handler
() returns, or the thread is canceled. If
setup
() returns successfully,
teardown
() is guaranteed to be called exactly
once.
tcp_server_stop
() stops and destroys a
tcp_server
. All existing connection threads are
canceled and the current thread blocks until
teardown
() has been called for each connection.
tcp_connection_get_peer
() retrieves the
peer's address.
tcp_connection_get_server
() returns the
tcp_server
object associated with the connection
conn.
tcp_connection_get_fd
() returns the TCP
socket associated with the connection.
tcp_connection_get_fp
() returns a stream
opened on top of the socket for use with stream based I/O.
The user code should not close either the socket
or the stream; these will be closed automatically when the connection is
terminated.
tcp_server_start
() returns NULL
to indicate an error, with errno set appropriately.
The PDEL library was developed at Packet Design, LLC.
http://www.packetdesign.com/
Archie Cobbs ⟨archie@freebsd.org⟩