first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / octopi / library / sockets / socket_data.h
1 #ifndef SOCKET_DATA_CLASS
2 #define SOCKET_DATA_CLASS
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : socket_data                                                       *
7 *  Author : Chris Koeritz                                                     *
8 *                                                                             *
9 *  Purpose:                                                                   *
10 *                                                                             *
11 *    Tracks the partially transmitted data for transports based on a socket   *
12 *  metaphor, where a socket is merely a numerical designation of the channel. *
13 *    Note: this is a heavy-weight header.  don't include it in other headers. *
14 *                                                                             *
15 *******************************************************************************
16 * Copyright (c) 1991-$now By Author.  This program is free software; you can  *
17 * redistribute it and/or modify it under the terms of the GNU General Public  *
18 * License as published by the Free Software Foundation; either version 2 of   *
19 * the License or (at your option) any later version.  This is online at:      *
20 *     http://www.fsf.org/copyleft/gpl.html                                    *
21 * Please send any updates to: fred@gruntose.com                               *
22 \*****************************************************************************/
23
24 #include <basis/astring.h>
25 #include <basis/byte_array.h>
26 #include <basis/astring.h>
27 #include <timely/time_stamp.h>
28
29 namespace sockets {
30
31 class socket_data
32 {
33 public:
34   int _socket;  // the number of the socket we are managing here.
35   basis::byte_array _partially_sent;  // accumulates bytes from partial sends.
36   basis::byte_array _partially_received;  // accumulates bytes from partial receives.
37   basis::byte_array _receive_buffer;  // temporary that's used for reading.
38   bool _is_server;  // true if this socket is for a server.
39   int _registered_interests;
40     // the events being watched for on this socket.  the bitwise or'ed items
41     // in this are from the socket_interests enum.
42   bool _connection_pending;
43     // true if a connect or accept is pending.  the default is true since we
44     // do not want to try probing the socket until it has been connected, for
45     // sockets in connected mode.
46   int _server_socket;  // non-zero if socket was accepted on root server socket.
47   bool _connected_mode;  // true if this is a connected type of socket.
48   timely::time_stamp _last_conn_alert;
49     // when the connection was last given a check for a connected state.
50
51   socket_data(int socket = 0, bool server = true, int server_socket = 0,
52       bool connected_mode = true)
53       : _socket(socket), _is_server(server), _registered_interests(0),
54         _connection_pending(true), _server_socket(server_socket),
55         _connected_mode(connected_mode) {}
56   ~socket_data() {}
57
58   bool server() const { return _is_server; }
59   bool client() const { return !_is_server; }
60
61   basis::astring text_form() const;
62     // returns a descriptive list of the data contained here.
63 };
64
65 //////////////
66
67 // implementations.
68
69 basis::astring socket_data::text_form() const
70 {
71   return basis::a_sprintf("socket=%d, type=%s, send_pend=%d, recv_pend=%d, "
72       "interests=%s, conn_pending=%s",
73       _socket, _is_server? "server":"client", _partially_sent.length(),
74       _partially_received.length(),
75       raw_socket::interest_name(_registered_interests).s(),
76       _connection_pending? "true":"false");
77 }
78
79 } //namespace.
80
81 #endif
82