8 const int SCD_MAXCONN = 100;
10 // forward declaration for friends
11 class scd_sock_poller;
14 * Berkeley TCP socket API wrapper class. The socket is put into non blocking
19 friend class scd_sock_poller;
23 virtual ~scd_socket();
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
30 bool is_valid() const;
34 * \return false if the socket is already valid
35 * \exception scd_exception if unexpected errors occured
40 * Closes the socket and marks it as invalid.
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
50 * \exception scd_exception if ::bind() failed for unexpected reason
52 bool bind(const std::string &loc_name, const uint16_t loc_port);
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
60 bool bind(const uint16_t loc_port);
63 * Marks a previously bound socket as listening.
64 * \return false if the socket is not valid
65 * \exception scd_exception if ::listen() failed
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
75 bool accept(scd_socket &new_sock);
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
92 * \exception scd_exception if unexpected errors occured
94 bool connect(const std::string &rem_name, const uint16_t rem_port);
97 * Indicates if a previous connection attempt is still in progress.
98 * \return true if a conneciton attempt is still in progress
100 bool is_connecting();
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
106 * \return true if the socket is valid and connected
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
118 bool connected_event();
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
126 size_t send(const void* buf, size_t len);
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
134 size_t recv(void* buf, size_t len);
138 /* member variables */
144 /* member functions */
147 * Initializes a sockaddr_in struct. Can resolve hostnames.
149 bool _get_sockaddr(sockaddr_in &addr, const std::string &name,
150 const uint16_t port) const;
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
158 bool _set_blocking(const bool mode);
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.