[BACK]Return to NetConnection.h CVS log [TXT][DIR] Up to [Development] / fam / fam

Annotation of fam/fam/NetConnection.h, Revision 1.1

1.1     ! trev        1: //  Copyright (C) 1999 Silicon Graphics, Inc.  All Rights Reserved.
        !             2: //
        !             3: //  This program is free software; you can redistribute it and/or modify it
        !             4: //  under the terms of version 2 of the GNU General Public License as
        !             5: //  published by the Free Software Foundation.
        !             6: //
        !             7: //  This program is distributed in the hope that it would be useful, but
        !             8: //  WITHOUT ANY WARRANTY; without even the implied warranty of
        !             9: //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  Further, any
        !            10: //  license provided herein, whether implied or otherwise, is limited to
        !            11: //  this program in accordance with the express provisions of the GNU
        !            12: //  General Public License.  Patent licenses, if any, provided herein do not
        !            13: //  apply to combinations of this program with other product or programs, or
        !            14: //  any other product whatsoever.  This program is distributed without any
        !            15: //  warranty that the program is delivered free of the rightful claim of any
        !            16: //  third person by way of infringement or the like.  See the GNU General
        !            17: //  Public License for more details.
        !            18: //
        !            19: //  You should have received a copy of the GNU General Public License along
        !            20: //  with this program; if not, write the Free Software Foundation, Inc., 59
        !            21: //  Temple Place - Suite 330, Boston MA 02111-1307, USA.
        !            22:
        !            23: #ifndef NetConnection_included
        !            24: #define NetConnection_included
        !            25:
        !            26: #include <sys/types.h>
        !            27: #include "Boolean.h"
        !            28: #include <limits.h>
        !            29:
        !            30: //  NetConnection is an abstract base class that implements an event
        !            31: //  driven, flow controlled reliable datagram connection over an
        !            32: //  already open stream socket.
        !            33: //
        !            34: //  The incoming and outgoing data streams are broken into messages.
        !            35: //  The format of a message is four bytes of message length (MSB
        !            36: //  first) followed by "length" bytes of data.  The last byte of data
        !            37: //  should be NUL ('\0').
        !            38: //
        !            39: //  The mprintf() function outputs a message using printf-style
        !            40: //  formatting.  It appends a NUL byte to the message and prepends the
        !            41: //  message length.  It also automatically flushes the message.  If
        !            42: //  the connection has closed, mprintf() returns immediately.
        !            43: //
        !            44: //  When a complete message is received, the pure virtual function
        !            45: //  input_msg() is called with the length and the address of the
        !            46: //  message.  If EOF is read on the connection, input_msg() is called
        !            47: //  with a NULL address and count (analogous to read(2) returning 0
        !            48: //  bytes).
        !            49: //
        !            50: //  NetConnection implements flow control.  Whenever the output buffer
        !            51: //  is empty, read events are accepted from the Scheduler.  Whenever
        !            52: //  the output buffer can't be flushed, reading is suspended, and the
        !            53: //  Scheduler watches for the connection to be writable.  This means
        !            54: //  that input is only accepted when it's possible to send a reply.
        !            55: //  The unblock handler, if any, is called whenever output becomes
        !            56: //  unblocked.
        !            57:
        !            58: class NetConnection {
        !            59:
        !            60: public:
        !            61:
        !            62:     typedef void (*UnblockHandler)(void *closure);
        !            63:
        !            64:     NetConnection(int fd, UnblockHandler, void *closure);
        !            65:
        !            66:     virtual ~NetConnection();
        !            67:     bool ready_for_output() const;
        !            68:     void ready_for_input(bool);
        !            69:     int get_fd() const { return fd; }
        !            70:
        !            71: protected:
        !            72:
        !            73:     virtual bool input_msg(const char *data, unsigned nbytes) = 0;
        !            74:     void mprintf(const char *format, ...);
        !            75:
        !            76: private:
        !            77:
        !            78:     enum { MAXMSGSIZE = PATH_MAX + 40 };
        !            79:     typedef u_int32_t Length;
        !            80:     typedef struct msgList_s {
        !            81:         char msg[MAXMSGSIZE+5];  //  + 4 for 32-bit length, + 1 for overflow
        !            82:         int len;
        !            83:         msgList_s * next;
        !            84:     } msgList_t;
        !            85:
        !            86:     msgList_t *omsgList;
        !            87:     msgList_t *omsgListTail;
        !            88:
        !            89:     int fd;
        !            90:     bool iready, oready;
        !            91:     char ibuf[MAXMSGSIZE+5];     //  + 4 for 32-bit length, + 1 for overflow
        !            92:     char *iend;
        !            93:     char *itail;
        !            94:     UnblockHandler unblock_handler;
        !            95:     void *closure;
        !            96:
        !            97:     void set_handlers(bool new_iready, bool new_oready);
        !            98:     void shutdown(bool = true);
        !            99:
        !           100:     //  Input
        !           101:
        !           102:     void input();
        !           103:     void deliver_input();
        !           104:     static void read_handler(int fd, void *closure);
        !           105:
        !           106:     //  Output
        !           107:
        !           108:     void flush();
        !           109:     static void write_handler(int fd, void *closure);
        !           110:
        !           111:     NetConnection(const NetConnection&); // Do not copy
        !           112:     NetConnection& operator = (const NetConnection&);	 //  or assign.
        !           113:
        !           114: };
        !           115:
        !           116: #endif /* !NetConnection_included */

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