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 
79 
80  bool eventReceived(struct epoll_event event);
81 
85 
86  int write(std::string data);
87  void write(char *buffer, int length);
88 
89  void output(std::stringstream &out);
90 
97 
98  virtual void onRegister();
99  virtual void onRegistered();
100 
101  virtual void onUnregister();
102 
109 
110  virtual void onUnregistered();
111 
112  bool needsToWrite();
113 
114  bool reset = false;
115 
116  protected:
117 
118  EPoll &ePoll; // The EPoll control object.
119 
120  bool shutDown = false;
121 
122  void setBufferSize(int length);
123 
124  int getBufferSize();
125 
131 
132 // virtual void onConnected(); ///< Called when socket is open and ready to communicate.
133 
137 
138 // virtual void onDisconnected(); ///< Called when socket is closing and no longer ready to communicate.
139 
147 
148  virtual void onDataReceived(std::string data);
149 
153 
154  virtual void onDataReceived(coreutils::ZString &data);
155 
160 
161  virtual void receiveData(coreutils::ZString &buffer);
162 
163  private:
164 
165  std::string text;
166  int descriptor = -1;
167  std::mutex lock;
168  std::mutex outlock;
169  bool readHangup = false;
170 
171 // struct epoll_event event; // Event selection construction structure.
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 // const char * const begin_;
191 // const char * const end_;
192 // const char * const current_;
193 
194  std::queue<std::string> fifo;
195 
196  };
197 
198 }
199 
200 #endif
201 
core::Socket
Definition: Socket.h:34
core::Socket::write
int write(std::string data)
Definition: Socket.cpp:154
core::EPoll
Definition: EPoll.h:31
core::Socket::onRegistered
virtual void onRegistered()
Called after the socket has been registered with epoll processing.
Definition: Socket.cpp:57
core::Socket::receiveData
virtual void receiveData(coreutils::ZString &buffer)
Definition: Socket.cpp:103
core::Socket::onDataReceived
virtual void onDataReceived(std::string data)
Called when data is received from the socket.
Definition: Socket.cpp:95
core::Socket::Socket
Socket(EPoll &ePoll, std::string text="")
Definition: Socket.cpp:9
core::Socket::shutdown
void shutdown(std::string text="unknown")
Definition: Socket.cpp:170
core::Socket::onRegister
virtual void onRegister()
Called before the socket has registered with the epoll processing.
Definition: Socket.cpp:55
core::Socket::eventReceived
bool eventReceived(struct epoll_event event)
Parse epoll event and call specified callbacks.
Definition: Socket.cpp:63
core::Socket::onUnregistered
virtual void onUnregistered()
Called when the socket has finished unregistering for the epoll processing.
Definition: Socket.cpp:61
core::Socket::getDescriptor
int getDescriptor()
Get the descriptor for the socket.
Definition: Socket.cpp:41
core::Socket::~Socket
virtual ~Socket()
Definition: Socket.cpp:15
core::Socket::setDescriptor
void setDescriptor(int descriptor)
Set the descriptor for the socket.
Definition: Socket.cpp:25