dol: initial dol commit
[jump.git] / dol / src / dol / visitor / hdsd / scd / scd_socket.h
1 #ifndef SCD_SOCKET_H
2 #define SCD_SOCKET_H
3
4 #include <string>
5 #include <sys/types.h>
6 #include <netdb.h>
7
8 const int SCD_MAXCONN = 100;
9
10 // forward declaration for friends
11 class scd_sock_poller;
12
13 /**
14  * Berkeley TCP socket API wrapper class. The socket is put into non blocking
15  * mode.
16  */
17 class scd_socket
18 {
19     friend class scd_sock_poller;
20
21 public:
22     scd_socket();
23     virtual ~scd_socket();
24
25     /**
26      * Returns true if this socket has been successfully created and
27      * has not been closed since then. Use this function to check if an
28      * error occured.
29      */
30     bool is_valid() const;
31
32     /**
33      * Creates the socket.
34      * \return false if the socket is already valid
35      * \exception scd_exception if unexpected errors occured
36      */
37     bool create();
38
39     /**
40      * Closes the socket and marks it as invalid.
41      */
42     void close();
43
44     /**
45      * Binds the socket to a local address and port.
46      * \param loc_name the local IP or hostname or empty string
47      * \param loc_port the local port or 0
48      * \return false if the socket is not valid or an invalid address
49      * was specified
50      * \exception scd_exception if ::bind() failed for unexpected reason
51      */
52     bool bind(const std::string &loc_name, const uint16_t loc_port);
53     
54     /**
55      * Binds the socket to a port.
56      * \param loc_port the local port
57      * \exception scd_exception if ::bind() failed for unexpected reason
58      * \return false if the sockat is not valid
59      */
60     bool bind(const uint16_t loc_port);
61
62     /**
63      * Marks a previously bound socket as listening.
64      * \return false if the socket is not valid
65      * \exception scd_exception if ::listen() failed
66      */
67     bool listen();
68
69     /**
70      * Accept a new connection from a listening socket.
71      * \param new_sock the object to hold the new connection
72      * \return true if a new connection was accepted
73      * \exception scd_exception if unexpected errors occured
74      */
75     bool accept(scd_socket &new_sock);
76
77     /**
78      * Initiates a connection attempt to a remote host. Use is_connecting()
79      * to check whether this attempt is still in progress or is_connected()
80      * to check whether the socket is connected. If the connection attempt
81      * is in progress, the socket has to be polled for a write event.
82      * If this event occures its handler shall call connected_event()
83      * to check if the connection was established successully.
84      * It is not possible to try to connect again if the write event
85      * is not polled and processed by calling connected_event() after
86      * is_connecting() has indicated that the attempt is still in progress.
87      * \param rem_name the hostname of the remote host (IP or host name)
88      * \param rem_port the port to connect to
89      * \return true if the connection attempt was started successfully,
90      * false if the hostname could not be resolved or a connection attempt
91      * was not possible
92      * \exception scd_exception if unexpected errors occured
93      */
94     bool connect(const std::string &rem_name, const uint16_t rem_port);
95
96     /**
97      * Indicates if a previous connection attempt is still in progress.
98      * \return true if a conneciton attempt is still in progress
99      */
100     bool is_connecting();
101
102     /**
103      * Indicates if this socket is connected to a peer either while it has
104      * been accepted from an incoming connection or the outgoing connection
105      * is established.
106      * \return true if the socket is valid and connected
107      */
108     bool is_connected();
109
110     /**
111      * Check if a connection attempt has succeeded on a write event. Shall
112      * only be called from the write event handler. Especially after a
113      * connection attempt was in progress. Can also be called if the
114      * connection has already been established.
115      * \return true if the socket is valid and the connection is established
116      * \exception scd_exception if unexpected errors occured
117      */
118     bool connected_event();
119
120     /**
121      * Tries to send at most len data from the buffer.
122      * Might close the socket on errors.
123      * \return the number of successfully sent bytes
124      * \exception scd_exception if unexpected errors occured
125      */
126     size_t send(const void* buf, size_t len);
127
128     /**
129      * Tries to receive at most len data to the buffer.
130      * Might close the socket on errors.
131      * \return the number of successfully received bytes
132      * \exception scd_exception if unexpected errors occured
133      */
134     size_t recv(void* buf, size_t len);
135
136
137 private:
138     /* member variables */
139
140     int _socket;
141     bool _is_connecting;
142     bool _is_connected;
143
144     /* member functions */
145
146     /**
147      * Initializes a sockaddr_in struct. Can resolve hostnames.
148      */
149     bool _get_sockaddr(sockaddr_in &addr, const std::string &name,
150             const uint16_t port) const;
151
152     /**
153      * Sets the socket in blocking or non blocking mode.
154      * \param mode true to set blocking mode
155      * \return false if the socket is not valid
156      * \exception scd_exception if unexpected errors occured
157      */
158     bool _set_blocking(const bool mode);
159
160     /**
161      * Disables the Nagle algorithm which buffers outgoing data up to 200ms
162      * before sending it. Disabling causes more network traffic but
163      * decreases the delay.
164      */
165     bool _set_nodelay();
166 };
167
168 #endif