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, Socket *sender);
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  std::mutex lock;
171  bool readHangup = false;
172  volatile bool inHandler = false;
173 
174  //-------------------------------------------------------------------------------------
175  // the writeSocket is called when epoll has received a write request for a socket.
176  // Writing data to this socket is queued in the streambuf and permission is requested
177  // to write to the socket. This routine handles the writing of the streambuf data
178  // buffer to the socket.
179  //-------------------------------------------------------------------------------------
180 
181  void writeSocket();
182 
183  // int_type underflow();
184 // int_type uflow();
185 // int_type pbackfail(int_type ch);
186 // streamsize showmanyc();
187 
188  char *buffer; // This is a pointer to the managed buffer space.
189  int length; // This is the length of the buffer.
190 
191  std::queue<std::string> fifo;
192 
193  };
194 
195 }
196 
197 #endif
198 
Definition: EPoll.h:31
Definition: Socket.h:34
int getDescriptor()
Get the descriptor for the socket.
Definition: Socket.cpp:43
bool eventReceived(struct epoll_event event, long long eventId)
Parse epoll event and call specified callbacks.
Definition: Socket.cpp:64
virtual void onRegistered()
Called after the socket has been registered with epoll processing.
Definition: Socket.cpp:58
virtual void receiveData(coreutils::ZString &buffer)
Definition: Socket.cpp:94
Socket(EPoll &ePoll, std::string text="")
Definition: Socket.cpp:11
int write(std::string data, Socket *sender)
Definition: Socket.cpp:137
virtual void onRegister()
Called before the socket has registered with the epoll processing.
Definition: Socket.cpp:56
virtual ~Socket()
Definition: Socket.cpp:16
void setDescriptor(int descriptor)
Set the descriptor for the socket.
Definition: Socket.cpp:28
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:62
void shutdown(std::string text="unknown")
Definition: Socket.cpp:156