My Project
Socket.h
1 #ifndef __Socket_h__
2 #define __Socket_h__
3 
4 #include "includes"
5 #include "Object.h"
6 #include "ZString.h"
7 
8 namespace core {
9 
10  class EPoll;
11 
33 
34  class Socket {
35 
36  public:
37 
44 
45  Socket(EPoll &ePoll, std::string text = "");
46 
50 
51  virtual ~Socket();
52 
58 
59  void shutdown(std::string text = "unknown");
60 
65 
66  void setDescriptor(int descriptor);
67 
68  int getDescriptor();
69 
82 
83  bool eventReceived(struct epoll_event event, long long eventId);
84 
88 
89  int write(std::string data);
90  void write(char *buffer, int length);
91 
92  void output(std::stringstream &out);
93 
100 
101  virtual void onRegister();
102  virtual void onRegistered();
103 
104  virtual void onUnregister();
105 
111 
112  virtual void onUnregistered();
113 
114  bool needsToWrite();
115 
116  bool reset = false;
117 
118  volatile bool shutDown = false;
119 
120  void enableSocket();
121  void disableSocket();
122 
123  protected:
124 
125  EPoll &ePoll; // The EPoll control object.
126 
127  void setBufferSize(int length);
128 
129  int getBufferSize();
130 
136 
137 // virtual void onConnected(); ///< Called when socket is open and ready to communicate.
138 
142 
143 // virtual void onDisconnected(); ///< Called when socket is closing and no longer ready to communicate.
144 
152 
153  virtual void onDataReceived(std::string data);
154 
158 
159  virtual void onDataReceived(coreutils::ZString &data);
160 
165 
166  virtual void receiveData(coreutils::ZString &buffer);
167 
168  private:
169 
170  std::string text;
171  int descriptor = -1;
172  std::mutex outlock;
173  bool readHangup = false;
174  volatile bool inHandler = false;
175 
176  //-------------------------------------------------------------------------------------
177  // the writeSocket is called when epoll has received a write request for a socket.
178  // Writing data to this socket is queued in the streambuf and permission is requested
179  // to write to the socket. This routine handles the writing of the streambuf data
180  // buffer to the socket.
181  //-------------------------------------------------------------------------------------
182 
183  void writeSocket();
184 
185  // int_type underflow();
186 // int_type uflow();
187 // int_type pbackfail(int_type ch);
188 // streamsize showmanyc();
189 
190  char *buffer; // This is a pointer to the managed buffer space.
191  int length; // This is the length of the buffer.
192 
193  std::queue<std::string> fifo;
194 
195  void resetSocket();
196 
197  std::mutex lock;
198 
199  };
200 
201 }
202 
203 #endif
204 
Definition: EPoll.h:31
Definition: Socket.h:34
int getDescriptor()
Get the descriptor for the socket.
Definition: Socket.cpp:46
int write(std::string data)
Definition: Socket.cpp:158
bool eventReceived(struct epoll_event event, long long eventId)
Parse epoll event and call specified callbacks.
Definition: Socket.cpp:70
virtual void onRegistered()
Called after the socket has been registered with epoll processing.
Definition: Socket.cpp:64
virtual void receiveData(coreutils::ZString &buffer)
Definition: Socket.cpp:112
Socket(EPoll &ePoll, std::string text="")
Definition: Socket.cpp:12
virtual void onRegister()
Called before the socket has registered with the epoll processing.
Definition: Socket.cpp:62
virtual ~Socket()
Definition: Socket.cpp:18
void setDescriptor(int descriptor)
Set the descriptor for the socket.
Definition: Socket.cpp:30
virtual void onDataReceived(std::string data)
Called when data is received from the socket.
Definition: Socket.cpp:102
virtual void onUnregistered()
Called when the socket has finished unregistering for the epoll processing.
Definition: Socket.cpp:68
void shutdown(std::string text="unknown")
Definition: Socket.cpp:179