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  protected:
121 
122  EPoll &ePoll; // The EPoll control object.
123 
124  void setBufferSize(int length);
125 
126  int getBufferSize();
127 
133 
134 // virtual void onConnected(); ///< Called when socket is open and ready to communicate.
135 
139 
140 // virtual void onDisconnected(); ///< Called when socket is closing and no longer ready to communicate.
141 
149 
150  virtual void onDataReceived(std::string data);
151 
155 
156  virtual void onDataReceived(coreutils::ZString &data);
157 
162 
163  virtual void receiveData(coreutils::ZString &buffer);
164 
165  private:
166 
167  std::string text;
168  int descriptor = -1;
169  std::mutex outlock;
170  bool readHangup = false;
171  volatile bool inHandler = false;
172 
173  //-------------------------------------------------------------------------------------
174  // the writeSocket is called when epoll has received a write request for a socket.
175  // Writing data to this socket is queued in the streambuf and permission is requested
176  // to write to the socket. This routine handles the writing of the streambuf data
177  // buffer to the socket.
178  //-------------------------------------------------------------------------------------
179 
180  void writeSocket();
181 
182  // int_type underflow();
183 // int_type uflow();
184 // int_type pbackfail(int_type ch);
185 // streamsize showmanyc();
186 
187  char *buffer; // This is a pointer to the managed buffer space.
188  int length; // This is the length of the buffer.
189 
190  std::queue<std::string> fifo;
191 
192  };
193 
194 }
195 
196 #endif
197 
Definition: EPoll.h:31
Definition: Socket.h:34
int getDescriptor()
Get the descriptor for the socket.
Definition: Socket.cpp:44
int write(std::string data)
Definition: Socket.cpp:137
bool eventReceived(struct epoll_event event, long long eventId)
Parse epoll event and call specified callbacks.
Definition: Socket.cpp:65
virtual void onRegistered()
Called after the socket has been registered with epoll processing.
Definition: Socket.cpp:59
virtual void receiveData(coreutils::ZString &buffer)
Definition: Socket.cpp:94
Socket(EPoll &ePoll, std::string text="")
Definition: Socket.cpp:11
virtual void onRegister()
Called before the socket has registered with the epoll processing.
Definition: Socket.cpp:57
virtual ~Socket()
Definition: Socket.cpp:16
void setDescriptor(int descriptor)
Set the descriptor for the socket.
Definition: Socket.cpp:29
virtual void onDataReceived(std::string data)
Called when data is received from the socket.
Definition: Socket.cpp:86
virtual void onUnregistered()
Called when the socket has finished unregistering for the epoll processing.
Definition: Socket.cpp:63
void shutdown(std::string text="unknown")
Definition: Socket.cpp:155