Annotation of embedaddon/libpdel/net/tcp_server.3, revision 1.1

1.1     ! misho       1: .\" Copyright (c) 2001-2002 Packet Design, LLC.
        !             2: .\" All rights reserved.
        !             3: .\" 
        !             4: .\" Subject to the following obligations and disclaimer of warranty,
        !             5: .\" use and redistribution of this software, in source or object code
        !             6: .\" forms, with or without modifications are expressly permitted by
        !             7: .\" Packet Design; provided, however, that:
        !             8: .\" 
        !             9: .\"    (i)  Any and all reproductions of the source or object code
        !            10: .\"         must include the copyright notice above and the following
        !            11: .\"         disclaimer of warranties; and
        !            12: .\"    (ii) No rights are granted, in any manner or form, to use
        !            13: .\"         Packet Design trademarks, including the mark "PACKET DESIGN"
        !            14: .\"         on advertising, endorsements, or otherwise except as such
        !            15: .\"         appears in the above copyright notice or in the software.
        !            16: .\" 
        !            17: .\" THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND
        !            18: .\" TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO
        !            19: .\" REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING
        !            20: .\" THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED
        !            21: .\" WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
        !            22: .\" OR NON-INFRINGEMENT.  PACKET DESIGN DOES NOT WARRANT, GUARANTEE,
        !            23: .\" OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS
        !            24: .\" OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY,
        !            25: .\" RELIABILITY OR OTHERWISE.  IN NO EVENT SHALL PACKET DESIGN BE
        !            26: .\" LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE
        !            27: .\" OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT,
        !            28: .\" INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL
        !            29: .\" DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF
        !            30: .\" USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF
        !            31: .\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
        !            32: .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
        !            33: .\" THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF
        !            34: .\" THE POSSIBILITY OF SUCH DAMAGE.
        !            35: .\"
        !            36: .\" Author: Archie Cobbs <archie@freebsd.org>
        !            37: .\"
        !            38: .\" $Id: tcp_server.3,v 1.4 2004/06/02 17:24:37 archie Exp $
        !            39: .\"
        !            40: .Dd April 22, 2002
        !            41: .Dt TCP_SERVER 3
        !            42: .Os
        !            43: .Sh NAME
        !            44: .Nm tcp_server
        !            45: .Nd generic TCP server
        !            46: .Sh LIBRARY
        !            47: PDEL Library (libpdel, \-lpdel)
        !            48: .Sh SYNOPSIS
        !            49: .In sys/types.h
        !            50: .In netinet/in.h
        !            51: .In stdio.h
        !            52: .In pdel/net/tcp_server.h
        !            53: .Ft "struct tcp_server *"
        !            54: .Fn 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"
        !            55: .Ft "void"
        !            56: .Fn tcp_server_stop "struct tcp_server **servp"
        !            57: .Ft "void *"
        !            58: .Fn tcp_server_get_cookie "struct tcp_server *serv"
        !            59: .Ft "struct tcp_server *"
        !            60: .Fn tcp_connection_get_server "struct tcp_connection *conn"
        !            61: .Ft "void *"
        !            62: .Fn tcp_connection_get_cookie "struct tcp_connection *conn"
        !            63: .Ft "int"
        !            64: .Fn tcp_connection_get_fd "struct tcp_connection *conn"
        !            65: .Ft "FILE *"
        !            66: .Fn tcp_connection_get_fp "struct tcp_connection *conn"
        !            67: .Ft "void"
        !            68: .Fn tcp_connection_get_peer "struct tcp_connection *conn" "struct sockaddr_in *sin"
        !            69: .Sh DESCRIPTION
        !            70: These functions provide support for implementing TCP servers.
        !            71: .Pp
        !            72: .Fn tcp_server_start
        !            73: creates a new TCP server listening for connections on IP address
        !            74: .Fa ip
        !            75: and port
        !            76: .Fa port .
        !            77: Memory for the
        !            78: .Nm tcp_server
        !            79: is dynamically allocated with
        !            80: .Xr typed_mem 3
        !            81: type
        !            82: .Fa mtype .
        !            83: At most
        !            84: .Fa max_conn
        !            85: simultaneous connections will be allowed.
        !            86: If
        !            87: .Fa conn_timeout
        !            88: is non-zero, then if an attempt to read from or write to a peer blocks
        !            89: for more than
        !            90: .Fa conn_timeout
        !            91: seconds, the connection will be terminated.
        !            92: .Fa ctx
        !            93: is a
        !            94: .Xr pevent 3
        !            95: event context used to listen for incoming connections.
        !            96: .Pp
        !            97: The
        !            98: .Fa setup ,
        !            99: .Fa handler ,
        !           100: and
        !           101: .Fa teardown
        !           102: arguments are pointers to functions having these types:
        !           103: .Pp
        !           104: .Bd -literal -compact -offset 3n
        !           105: typedef void *tcp_setup_t(struct tcp_connection *conn);
        !           106: typedef void tcp_handler_t(struct tcp_connection *conn);
        !           107: typedef void tcp_teardown_t(struct tcp_connection *conn);
        !           108: .Ed
        !           109: .Pp
        !           110: Each connection is represented by a
        !           111: .Nm tcp_connection
        !           112: object.
        !           113: For each new connection,
        !           114: .Fn setup
        !           115: is called to initialize user state for the connection.
        !           116: If setup is unsuccessful,
        !           117: .Fn setup
        !           118: should return
        !           119: .Dv NULL
        !           120: and set
        !           121: .Va errno
        !           122: appropriately, and the connection will be closed.
        !           123: Otherwise, it should return a non-NULL cookie; this cookie can be
        !           124: accessed later in
        !           125: .Fn handler
        !           126: and
        !           127: .Fn teardown
        !           128: by calling 
        !           129: .Fn tcp_connection_get_cookie .
        !           130: .Pp
        !           131: .Fn handler
        !           132: is used to service the connection.
        !           133: The thread calling this function may be canceled at any cancellation point;
        !           134: therefore,
        !           135: .Fn handler
        !           136: may need to register cleanup hooks to clean up allocated resources should
        !           137: that happen.
        !           138: .Pp
        !           139: .Fn teardown
        !           140: is called when
        !           141: .Fn handler
        !           142: returns, or the thread is canceled.
        !           143: If
        !           144: .Fn setup
        !           145: returns successfully, 
        !           146: .Fn teardown
        !           147: is guaranteed to be called exactly once.
        !           148: .Pp
        !           149: .Fn tcp_server_stop
        !           150: stops and destroys a
        !           151: .Nm tcp_server .
        !           152: All existing connection threads are canceled and the current thread
        !           153: blocks until
        !           154: .Fn teardown
        !           155: has been called for each connection.
        !           156: .Pp
        !           157: .Fn tcp_connection_get_peer
        !           158: retrieves the peer's address.
        !           159: .Pp
        !           160: .Fn tcp_connection_get_server
        !           161: returns the
        !           162: .Nm tcp_server
        !           163: object associated with the connection
        !           164: .Fa conn .
        !           165: .Pp
        !           166: .Fn tcp_connection_get_fd
        !           167: returns the TCP socket associated with the connection.
        !           168: .Pp
        !           169: .Fn tcp_connection_get_fp
        !           170: returns a stream opened on top of the socket for use with stream based I/O.
        !           171: .Pp
        !           172: The user code should
        !           173: .Em not
        !           174: close either the socket or the stream; these will be closed automatically
        !           175: when the connection is terminated.
        !           176: .Sh RETURN VALUES
        !           177: .Fn tcp_server_start
        !           178: returns
        !           179: .Dv NULL
        !           180: to indicate an error, with
        !           181: .Va errno
        !           182: set appropriately.
        !           183: .Sh SEE ALSO
        !           184: .Xr libpdel 3 ,
        !           185: .Xr pevent 3 ,
        !           186: .Xr typed_mem 3
        !           187: .Sh HISTORY
        !           188: The PDEL library was developed at Packet Design, LLC.
        !           189: .Dv "http://www.packetdesign.com/"
        !           190: .Sh AUTHORS
        !           191: .An Archie Cobbs Aq archie@freebsd.org

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>