Socket lib restyle and fixes backported from 3.0
This commit is contained in:
parent
7b12a5a2b5
commit
7d83edd078
2 changed files with 128 additions and 124 deletions
120
lib/socket.h
120
lib/socket.h
|
@ -17,12 +17,12 @@
|
|||
#include <unistd.h>
|
||||
|
||||
#ifdef SSL
|
||||
#include "mbedtls/net.h"
|
||||
#include "mbedtls/ssl.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "mbedtls/debug.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/net.h"
|
||||
#include "mbedtls/ssl.h"
|
||||
#endif
|
||||
|
||||
// for being friendly with Socket::Connection down below
|
||||
|
@ -38,9 +38,9 @@ namespace Socket{
|
|||
bool matchIPv6Addr(const std::string &A, const std::string &B, uint8_t prefix);
|
||||
std::string getBinForms(std::string addr);
|
||||
/// Returns true if given human-readable address (address, not hostname) is a local address.
|
||||
bool isLocal(const std::string & host);
|
||||
bool isLocal(const std::string &host);
|
||||
/// Returns true if given human-readable hostname is a local address.
|
||||
bool isLocalhost(const std::string & host);
|
||||
bool isLocalhost(const std::string &host);
|
||||
bool checkTrueSocket(int sock);
|
||||
|
||||
/// A buffer made out of std::string objects that can be efficiently read from and written to.
|
||||
|
@ -49,7 +49,7 @@ namespace Socket{
|
|||
std::deque<std::string> data;
|
||||
|
||||
public:
|
||||
std::string splitter;///<String to automatically split on if encountered. \n by default
|
||||
std::string splitter; ///< String to automatically split on if encountered. \n by default
|
||||
Buffer();
|
||||
unsigned int size();
|
||||
unsigned int bytes(unsigned int max);
|
||||
|
@ -73,83 +73,83 @@ namespace Socket{
|
|||
protected:
|
||||
void clear(); ///< Clears the internal data structure. Meant only for use in constructors.
|
||||
bool isTrueSocket;
|
||||
int sSend; ///< Write end of socket.
|
||||
int sRecv; ///< Read end of socket.
|
||||
std::string remotehost; ///< Stores remote host address.
|
||||
int sSend; ///< Write end of socket.
|
||||
int sRecv; ///< Read end of socket.
|
||||
std::string remotehost; ///< Stores remote host address.
|
||||
std::string boundaddr; ///< Stores bound interface address.
|
||||
struct sockaddr_in6 remoteaddr;///< Stores remote host address.
|
||||
struct sockaddr_in6 remoteaddr; ///< Stores remote host address.
|
||||
uint64_t up;
|
||||
uint64_t down;
|
||||
long long int conntime;
|
||||
Buffer downbuffer; ///< Stores temporary data coming in.
|
||||
int iread(void *buffer, int len, int flags = 0); ///< Incremental read call.
|
||||
unsigned int iwrite(const void *buffer, int len); ///< Incremental write call.
|
||||
bool iread(Buffer &buffer, int flags = 0); ///< Incremental write call that is compatible with Socket::Buffer.
|
||||
bool iwrite(std::string &buffer); ///< Write call that is compatible with std::string.
|
||||
bool iread(Buffer &buffer, int flags = 0); ///< Incremental write call that is compatible with Socket::Buffer.
|
||||
bool iwrite(std::string &buffer); ///< Write call that is compatible with std::string.
|
||||
void setBoundAddr();
|
||||
#ifdef SSL
|
||||
/// optional extension that uses mbedtls for SSL
|
||||
/// optional extension that uses mbedtls for SSL
|
||||
protected:
|
||||
bool sslConnected;
|
||||
int ssl_iread(void *buffer, int len, int flags = 0); ///< Incremental read call.
|
||||
unsigned int ssl_iwrite(const void *buffer, int len); ///< Incremental write call.
|
||||
mbedtls_net_context * server_fd;
|
||||
mbedtls_entropy_context * entropy;
|
||||
mbedtls_ctr_drbg_context * ctr_drbg;
|
||||
mbedtls_ssl_context * ssl;
|
||||
mbedtls_ssl_config * conf;
|
||||
mbedtls_net_context *server_fd;
|
||||
mbedtls_entropy_context *entropy;
|
||||
mbedtls_ctr_drbg_context *ctr_drbg;
|
||||
mbedtls_ssl_context *ssl;
|
||||
mbedtls_ssl_config *conf;
|
||||
#endif
|
||||
|
||||
public:
|
||||
// friends
|
||||
friend class ::Buffer::user;
|
||||
// constructors
|
||||
Connection(); ///< Create a new disconnected base socket.
|
||||
Connection(int sockNo); ///< Create a new base socket.
|
||||
Connection(); ///< Create a new disconnected base socket.
|
||||
Connection(int sockNo); ///< Create a new base socket.
|
||||
Connection(std::string hostname, int port, bool nonblock, bool with_ssl = false); ///< Create a new TCP socket.
|
||||
Connection(std::string adres, bool nonblock = false); ///< Create a new Unix Socket.
|
||||
Connection(int write, int read); ///< Simulate a socket using two file descriptors.
|
||||
Connection(std::string adres, bool nonblock = false); ///< Create a new Unix Socket.
|
||||
Connection(int write, int read); ///< Simulate a socket using two file descriptors.
|
||||
// copy/assignment constructors
|
||||
Connection(const Connection& rhs);
|
||||
Connection& operator=(const Connection& rhs);
|
||||
Connection(const Connection &rhs);
|
||||
Connection &operator=(const Connection &rhs);
|
||||
// destructor
|
||||
~Connection();
|
||||
// generic methods
|
||||
void open(int sockNo);//Open from existing socket connection.
|
||||
void open(std::string hostname, int port, bool nonblock, bool with_ssl = false);//Open TCP connection.
|
||||
void open(std::string adres, bool nonblock = false);//Open Unix connection.
|
||||
void open(int write, int read);//Open from two existing file descriptors.
|
||||
void close(); ///< Close connection.
|
||||
void drop(); ///< Close connection without shutdown.
|
||||
void open(int sockNo); // Open from existing socket connection.
|
||||
void open(std::string hostname, int port, bool nonblock, bool with_ssl = false); // Open TCP connection.
|
||||
void open(std::string adres, bool nonblock = false); // Open Unix connection.
|
||||
void open(int write, int read); // Open from two existing file descriptors.
|
||||
void close(); ///< Close connection.
|
||||
void drop(); ///< Close connection without shutdown.
|
||||
void setBlocking(bool blocking); ///< Set this socket to be blocking (true) or nonblocking (false).
|
||||
bool isBlocking(); ///< Check if this socket is blocking (true) or nonblocking (false).
|
||||
std::string getHost() const; ///< Gets hostname for connection, if available.
|
||||
bool isBlocking(); ///< Check if this socket is blocking (true) or nonblocking (false).
|
||||
std::string getHost() const; ///< Gets hostname for connection, if available.
|
||||
std::string getBinHost();
|
||||
void setHost(std::string host); ///< Sets hostname for connection manually.
|
||||
std::string getBoundAddress() const;
|
||||
int getSocket(); ///< Returns internal socket number.
|
||||
int getPureSocket(); ///< Returns non-piped internal socket number.
|
||||
std::string getError(); ///< Returns a string describing the last error that occured.
|
||||
bool connected() const; ///< Returns the connected-state for this socket.
|
||||
int getSocket(); ///< Returns internal socket number.
|
||||
int getPureSocket(); ///< Returns non-piped internal socket number.
|
||||
std::string getError(); ///< Returns a string describing the last error that occured.
|
||||
bool connected() const; ///< Returns the connected-state for this socket.
|
||||
bool isAddress(const std::string &addr);
|
||||
bool isLocal(); ///< Returns true if remote address is a local address.
|
||||
// buffered i/o methods
|
||||
bool spool(); ///< Updates the downbufferinternal variables.
|
||||
bool peek(); ///< Clears the downbuffer and fills it with peek
|
||||
Buffer &Received(); ///< Returns a reference to the download buffer.
|
||||
void SendNow(const std::string &data); ///< Will not buffer anything but always send right away. Blocks.
|
||||
void SendNow(const char *data); ///< Will not buffer anything but always send right away. Blocks.
|
||||
void SendNow(const char *data, size_t len); ///< Will not buffer anything but always send right away. Blocks.
|
||||
bool spool(); ///< Updates the downbufferinternal variables.
|
||||
bool peek(); ///< Clears the downbuffer and fills it with peek
|
||||
Buffer &Received(); ///< Returns a reference to the download buffer.
|
||||
void SendNow(const std::string &data); ///< Will not buffer anything but always send right away. Blocks.
|
||||
void SendNow(const char *data); ///< Will not buffer anything but always send right away. Blocks.
|
||||
void SendNow(const char *data,
|
||||
size_t len); ///< Will not buffer anything but always send right away. Blocks.
|
||||
void skipBytes(uint32_t byteCount);
|
||||
uint32_t skipCount;
|
||||
// stats related methods
|
||||
unsigned int connTime(); ///< Returns the time this socket has been connected.
|
||||
uint64_t dataUp(); ///< Returns total amount of bytes sent.
|
||||
uint64_t dataDown(); ///< Returns total amount of bytes received.
|
||||
void resetCounter(); ///< Resets the up/down bytes counter to zero.
|
||||
unsigned int connTime(); ///< Returns the time this socket has been connected.
|
||||
uint64_t dataUp(); ///< Returns total amount of bytes sent.
|
||||
uint64_t dataDown(); ///< Returns total amount of bytes received.
|
||||
void resetCounter(); ///< Resets the up/down bytes counter to zero.
|
||||
void addUp(const uint32_t i);
|
||||
void addDown(const uint32_t i);
|
||||
std::string getStats(std::string C); ///< Returns a std::string of stats, ended by a newline.
|
||||
friend class Server;
|
||||
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.
|
||||
|
@ -162,18 +162,18 @@ namespace Socket{
|
|||
/// This class is for easily setting up listening socket, either TCP or Unix.
|
||||
class Server{
|
||||
private:
|
||||
std::string errors; ///< Stores errors that may have occured.
|
||||
int sock; ///< Internally saved socket number.
|
||||
std::string errors; ///< Stores errors that may have occured.
|
||||
int sock; ///< Internally saved socket number.
|
||||
bool IPv6bind(int port, std::string hostname, bool nonblock); ///< Attempt to bind an IPv6 socket
|
||||
bool IPv4bind(int port, std::string hostname, bool nonblock); ///< Attempt to bind an IPv4 socket
|
||||
public:
|
||||
Server(); ///< Create a new base Server.
|
||||
Server(int existingSock); ///< Create a new Server from existing socket.
|
||||
Server(int port, std::string hostname, bool nonblock = false); ///< Create a new TCP Server.
|
||||
Server(std::string adres, bool nonblock = false); ///< Create a new Unix Server.
|
||||
Connection accept(bool nonblock = false); ///< Accept any waiting connections.
|
||||
void setBlocking(bool blocking); ///< Set this socket to be blocking (true) or nonblocking (false).
|
||||
bool connected() const; ///< Returns the connected-state for this socket.
|
||||
Server(); ///< Create a new base Server.
|
||||
Server(int existingSock); ///< Create a new Server from existing socket.
|
||||
Server(int port, std::string hostname, bool nonblock = false); ///< Create a new TCP Server.
|
||||
Server(std::string adres, bool nonblock = false); ///< Create a new Unix Server.
|
||||
Connection accept(bool nonblock = false); ///< Accept any waiting connections.
|
||||
void setBlocking(bool blocking); ///< Set this socket to be blocking (true) or nonblocking (false).
|
||||
bool connected() const; ///< Returns the connected-state for this socket.
|
||||
bool isBlocking(); ///< Check if this socket is blocking (true) or nonblocking (false).
|
||||
void close(); ///< Close connection.
|
||||
void drop(); ///< Close connection without shutdown.
|
||||
|
@ -189,7 +189,10 @@ namespace Socket{
|
|||
unsigned int up; ///< Amount of bytes transferred up.
|
||||
unsigned int down; ///< Amount of bytes transferred down.
|
||||
unsigned int data_size; ///< The size in bytes of the allocated space in the data pointer.
|
||||
int family; ///<Current socket address family
|
||||
int family; ///< Current socket address family
|
||||
std::string boundAddr, boundMulti;
|
||||
int boundPort;
|
||||
|
||||
public:
|
||||
char *data; ///< Holds the last received packet.
|
||||
unsigned int data_len; ///< The size in bytes of the last received packet.
|
||||
|
@ -208,5 +211,4 @@ namespace Socket{
|
|||
void SendNow(const char *data);
|
||||
void SendNow(const char *data, size_t len);
|
||||
};
|
||||
}
|
||||
|
||||
}// namespace Socket
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue