MistServer  2.5.3-Pro-19-gf5e75b1 ( Generic_64)
socket.h
Go to the documentation of this file.
1 
5 #pragma once
6 #include <string>
7 #include <sstream>
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #include <sys/un.h>
11 #include <arpa/inet.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <errno.h>
15 #include <string.h>
16 #include <fcntl.h>
17 #include <deque>
18 
19 //for being friendly with Socket::Connection down below
20 namespace Buffer {
21  class user;
22 }
23 
25 namespace Socket {
26 
28  class Buffer {
29  private:
30  std::deque<std::string> data;
31  public:
32  unsigned int size();
33  unsigned int bytes(unsigned int max);
34  void append(const std::string & newdata);
35  void append(const char * newdata, const unsigned int newdatasize);
36  void prepend(const std::string & newdata);
37  void prepend(const char * newdata, const unsigned int newdatasize);
38  std::string & get();
39  bool available(unsigned int count);
40  std::string remove(unsigned int count);
41  std::string copy(unsigned int count);
42  void clear();
43  };
44  //Buffer
45 
47  class Connection {
48  private:
49  int sock;
50  int pipes[2];
51  std::string remotehost;
52  unsigned int up;
53  unsigned int down;
54  long long int conntime;
55  Buffer downbuffer;
56  int iread(void * buffer, int len, int flags = 0);
57  unsigned int iwrite(const void * buffer, int len);
58  bool iread(Buffer & buffer, int flags = 0);
59  bool iwrite(std::string & buffer);
60  public:
61  //friends
62  friend class ::Buffer::user;
63  //constructors
64  Connection();
65  Connection(int sockNo);
66  Connection(std::string hostname, int port, bool nonblock);
67  Connection(std::string adres, bool nonblock = false);
68  Connection(int write, int read);
69  //generic methods
70  void close();
71  void drop();
72  void setBlocking(bool blocking);
73  bool isBlocking();
74  std::string getHost() const;
75  std::string getBinHost();
76  void setHost(std::string host);
77  int getSocket();
78  int getPureSocket();
79  std::string getError();
80  bool connected() const;
81  bool isAddress(std::string addr);
82  //buffered i/o methods
83  bool spool();
84  bool peek();
85  Buffer & Received();
86  void SendNow(const std::string & data);
87  void SendNow(const char * data);
88  void SendNow(const char * data, size_t len);
89  //stats related methods
90  unsigned int connTime();
91  unsigned int dataUp();
92  unsigned int dataDown();
93  std::string getStats(std::string C);
94  friend class Server;
95  bool Error;
96  bool Blocking;
97  //overloaded operators
98  bool operator==(const Connection & B) const;
99  bool operator!=(const Connection & B) const;
100  operator bool() const;
101  };
102 
104  class Server {
105  private:
106  std::string errors;
107  int sock;
108  bool IPv6bind(int port, std::string hostname, bool nonblock);
109  bool IPv4bind(int port, std::string hostname, bool nonblock);
110  public:
111  Server();
112  Server(int port, std::string hostname = "0.0.0.0", bool nonblock = false);
113  Server(std::string adres, bool nonblock = false);
114  Connection accept(bool nonblock = false);
115  void setBlocking(bool blocking);
116  bool connected() const;
117  bool isBlocking();
118  void close();
119  void drop();
120  int getSocket();
121  };
122 
123  class UDPConnection {
124  private:
125  int sock;
126  std::string remotehost;
127  void * destAddr;
128  unsigned int destAddr_size;
129  unsigned int up;
130  unsigned int down;
131  unsigned int data_size;
132  bool isIPv6;//<<< True if IPv6 socket, false otherwise.
133  public:
134  char * data;
135  unsigned int data_len;
136  UDPConnection(const UDPConnection & o);
137  UDPConnection(bool nonblock = false);
138  ~UDPConnection();
139  int getSock();
140  int bind(int port, std::string iface = "", const std::string & multicastAddress = "");
141  void setBlocking(bool blocking);
142  void SetDestination(std::string hostname, uint32_t port);
143  void GetDestination(std::string & hostname, uint32_t & port);
144  bool Receive();
145  void SendNow(const std::string & data);
146  void SendNow(const char * data);
147  void SendNow(const char * data, size_t len);
148  };
149 
150 }
Definition: socket.h:20
Holds Socket tools.
Definition: socket.h:25
Canvas prototype clear
Definition: jquery.flot.min.js:1