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);
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 
112 
113  virtual void onUnregistered();
114 
115  bool needsToWrite();
116 
117  bool reset = false;
118 
119  protected:
120 
121  EPoll &ePoll; // The EPoll control object.
122 
123  bool shutDown = false;
124 
125  void setBufferSize(int length);
126 
127  int getBufferSize();
128 
134 
135 // virtual void onConnected(); ///< Called when socket is open and ready to communicate.
136 
140 
141 // virtual void onDisconnected(); ///< Called when socket is closing and no longer ready to communicate.
142 
150 
151  virtual void onDataReceived(std::string data);
152 
156 
157  virtual void onDataReceived(coreutils::ZString &data);
158 
163 
164  virtual void receiveData(coreutils::ZString &buffer);
165 
166  private:
167 
168  std::string text;
169  int descriptor = -1;
170 // std::mutex lock;
171  std::mutex outlock;
172  bool readHangup = false;
173  bool inHandler = false;
174 // struct epoll_event event; // Event selection construction structure.
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 // const char * const begin_;
194 // const char * const end_;
195 // const char * const current_;
196 
197  std::queue<std::string> fifo;
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:41
int write(std::string data)
Definition: Socket.cpp:138
virtual void onRegistered()
Called after the socket has been registered with epoll processing.
Definition: Socket.cpp:57
virtual void receiveData(coreutils::ZString &buffer)
Definition: Socket.cpp:91
Socket(EPoll &ePoll, std::string text="")
Definition: Socket.cpp:11
bool eventReceived(struct epoll_event event)
Parse epoll event and call specified callbacks.
Definition: Socket.cpp:63
virtual void onRegister()
Called before the socket has registered with the epoll processing.
Definition: Socket.cpp:55
virtual ~Socket()
Definition: Socket.cpp:17
void setDescriptor(int descriptor)
Set the descriptor for the socket.
Definition: Socket.cpp:27
virtual void onDataReceived(std::string data)
Called when data is received from the socket.
Definition: Socket.cpp:83
virtual void onUnregistered()
Called when the socket has finished unregistering for the epoll processing.
Definition: Socket.cpp:61
void shutdown(std::string text="unknown")
Definition: Socket.cpp:155