Allow HTTP Connector to reconnect to different buffers - closes #31
This commit is contained in:
parent
e7e37105b6
commit
942a4136d2
2 changed files with 44 additions and 16 deletions
|
@ -53,13 +53,16 @@ void Socket::Connection::setBlocking(bool blocking){
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Close connection. The internal socket is closed and then set to -1.
|
/// Close connection. The internal socket is closed and then set to -1.
|
||||||
|
/// If the connection is already closed, nothing happens.
|
||||||
void Socket::Connection::close(){
|
void Socket::Connection::close(){
|
||||||
#if DEBUG >= 6
|
if (connected()){
|
||||||
fprintf(stderr, "Socket closed.\n");
|
#if DEBUG >= 6
|
||||||
#endif
|
fprintf(stderr, "Socket closed.\n");
|
||||||
shutdown(sock, SHUT_RDWR);
|
#endif
|
||||||
::close(sock);
|
shutdown(sock, SHUT_RDWR);
|
||||||
sock = -1;
|
::close(sock);
|
||||||
|
sock = -1;
|
||||||
|
}
|
||||||
}//Socket::Connection::close
|
}//Socket::Connection::close
|
||||||
|
|
||||||
/// Returns internal socket number.
|
/// Returns internal socket number.
|
||||||
|
@ -214,7 +217,7 @@ signed int Socket::Connection::ready(){
|
||||||
/// The connection status is updated after every read/write attempt, when errors occur
|
/// The connection status is updated after every read/write attempt, when errors occur
|
||||||
/// and when the socket is closed manually.
|
/// and when the socket is closed manually.
|
||||||
/// \returns True if socket is connected, false otherwise.
|
/// \returns True if socket is connected, false otherwise.
|
||||||
bool Socket::Connection::connected(){
|
bool Socket::Connection::connected() const{
|
||||||
return (sock >= 0);
|
return (sock >= 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -451,6 +454,24 @@ std::string Socket::Connection::getHost(){
|
||||||
return remotehost;
|
return remotehost;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns true if these sockets are the same socket.
|
||||||
|
/// Does not check the internal stats - only the socket itself.
|
||||||
|
bool Socket::Connection::operator== (const Connection &B) const{
|
||||||
|
return sock == B.sock;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns true if these sockets are not the same socket.
|
||||||
|
/// Does not check the internal stats - only the socket itself.
|
||||||
|
bool Socket::Connection::operator!= (const Connection &B) const{
|
||||||
|
return sock != B.sock;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns true if the socket is valid.
|
||||||
|
/// Aliases for Socket::Connection::connected()
|
||||||
|
Socket::Connection::operator bool() const{
|
||||||
|
return connected();
|
||||||
|
}
|
||||||
|
|
||||||
/// Create a new base Server. The socket is never connected, and a placeholder for later connections.
|
/// Create a new base Server. The socket is never connected, and a placeholder for later connections.
|
||||||
Socket::Server::Server(){
|
Socket::Server::Server(){
|
||||||
sock = -1;
|
sock = -1;
|
||||||
|
@ -661,13 +682,16 @@ Socket::Connection Socket::Server::accept(bool nonblock){
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Close connection. The internal socket is closed and then set to -1.
|
/// Close connection. The internal socket is closed and then set to -1.
|
||||||
|
/// If the connection is already closed, nothing happens.
|
||||||
void Socket::Server::close(){
|
void Socket::Server::close(){
|
||||||
#if DEBUG >= 6
|
if (connected()){
|
||||||
fprintf(stderr, "ServerSocket closed.\n");
|
#if DEBUG >= 6
|
||||||
#endif
|
fprintf(stderr, "ServerSocket closed.\n");
|
||||||
shutdown(sock, SHUT_RDWR);
|
#endif
|
||||||
::close(sock);
|
shutdown(sock, SHUT_RDWR);
|
||||||
sock = -1;
|
::close(sock);
|
||||||
|
sock = -1;
|
||||||
|
}
|
||||||
}//Socket::Server::close
|
}//Socket::Server::close
|
||||||
|
|
||||||
/// Returns the connected-state for this socket.
|
/// Returns the connected-state for this socket.
|
||||||
|
@ -675,7 +699,7 @@ void Socket::Server::close(){
|
||||||
/// The connection status is updated after every accept attempt, when errors occur
|
/// The connection status is updated after every accept attempt, when errors occur
|
||||||
/// and when the socket is closed manually.
|
/// and when the socket is closed manually.
|
||||||
/// \returns True if socket is connected, false otherwise.
|
/// \returns True if socket is connected, false otherwise.
|
||||||
bool Socket::Server::connected(){
|
bool Socket::Server::connected() const{
|
||||||
return (sock >= 0);
|
return (sock >= 0);
|
||||||
}//Socket::Server::connected
|
}//Socket::Server::connected
|
||||||
|
|
||||||
|
|
|
@ -37,7 +37,7 @@ namespace Socket{
|
||||||
bool canRead(); ///< Calls poll() on the socket, checking if data is available.
|
bool canRead(); ///< Calls poll() on the socket, checking if data is available.
|
||||||
bool canWrite(); ///< Calls poll() on the socket, checking if data can be written.
|
bool canWrite(); ///< Calls poll() on the socket, checking if data can be written.
|
||||||
signed int ready(); ///< Returns the ready-state for this socket.
|
signed int ready(); ///< Returns the ready-state for this socket.
|
||||||
bool connected(); ///< Returns the connected-state for this socket.
|
bool connected() const; ///< Returns the connected-state for this socket.
|
||||||
bool read(const void * buffer, int len); ///< Reads data from socket.
|
bool read(const void * buffer, int len); ///< Reads data from socket.
|
||||||
bool read(const void * buffer, int width, int count); ///< Read call that is compatible with file access syntax.
|
bool read(const void * buffer, int width, int count); ///< Read call that is compatible with file access syntax.
|
||||||
bool write(const void * buffer, int len); ///< Writes data to socket.
|
bool write(const void * buffer, int len); ///< Writes data to socket.
|
||||||
|
@ -62,6 +62,10 @@ namespace Socket{
|
||||||
friend class Server;
|
friend class Server;
|
||||||
bool Error; ///< Set to true if a socket error happened.
|
bool Error; ///< Set to true if a socket error happened.
|
||||||
bool Blocking; ///< Set to true if a socket is currently or wants to be blocking.
|
bool Blocking; ///< Set to true if a socket is currently or wants to be blocking.
|
||||||
|
//overloaded operators
|
||||||
|
bool operator== (const Connection &B) const;
|
||||||
|
bool operator!= (const Connection &B) const;
|
||||||
|
operator bool() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// This class is for easily setting up listening socket, either TCP or Unix.
|
/// This class is for easily setting up listening socket, either TCP or Unix.
|
||||||
|
@ -75,7 +79,7 @@ namespace Socket{
|
||||||
Server(int port, std::string hostname = "0.0.0.0", bool nonblock = false); ///< Create a new TCP Server.
|
Server(int port, std::string hostname = "0.0.0.0", bool nonblock = false); ///< Create a new TCP Server.
|
||||||
Server(std::string adres, bool nonblock = false); ///< Create a new Unix Server.
|
Server(std::string adres, bool nonblock = false); ///< Create a new Unix Server.
|
||||||
Connection accept(bool nonblock = false); ///< Accept any waiting connections.
|
Connection accept(bool nonblock = false); ///< Accept any waiting connections.
|
||||||
bool connected(); ///< Returns the connected-state for this socket.
|
bool connected() const; ///< Returns the connected-state for this socket.
|
||||||
void close(); ///< Close connection.
|
void close(); ///< Close connection.
|
||||||
int getSocket(); ///< Returns internal socket number.
|
int getSocket(); ///< Returns internal socket number.
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue