My Project
Socket.h
1 #ifndef __Socket_h__
2 #define __Socket_h__
3 
4 #include "includes"
5 #include "Object.h"
6 
7 namespace core {
8 
9  class EPoll;
10 
32 
33  class Socket : public core::Object {
34 
35  public:
36 
43 
44  Socket(EPoll &ePoll, std::string text = "");
45 
49 
50  ~Socket();
51 
57 
58  void shutdown(std::string text = "unknown");
59 
64 
65  void setDescriptor(int descriptor);
66 
67  int getDescriptor();
68 
78 
79  bool eventReceived(struct epoll_event event);
80 
84 
85  int write(std::string data);
86  void write(char *buffer, int length);
87 
88  void output(std::stringstream &out);
89 
96 
97  virtual void onRegister();
98  virtual void onRegistered();
99 
100  virtual void onUnregister();
101 
108 
109  virtual void onUnregistered();
110 
111  bool needsToWrite();
112 
113  bool reset = false;
114 
115  protected:
116 
117  EPoll &ePoll; // The EPoll control object.
118 
119  bool shutDown = false;
120 
121  void setBufferSize(int length);
122 
123  int getBufferSize();
124 
130 
131 // virtual void onConnected(); ///< Called when socket is open and ready to communicate.
132 
136 
137 // virtual void onDisconnected(); ///< Called when socket is closing and no longer ready to communicate.
138 
146 
147  virtual void onDataReceived(std::string data);
148 
149  virtual void onDataReceived(char *buffer, int len);
150 
155 
156  virtual void receiveData(char *buffer, int bufferLength);
157 
158  private:
159 
160  std::string text;
161  int descriptor = -1;
162  std::mutex lock;
163  std::mutex outlock;
164  bool readHangup = false;
165 
166 // struct epoll_event event; // Event selection construction structure.
167 
168  //-------------------------------------------------------------------------------------
169  // the writeSocket is called when epoll has received a write request for a socket.
170  // Writing data to this socket is queued in the streambuf and permission is requested
171  // to write to the socket. This routine handles the writing of the streambuf data
172  // buffer to the socket.
173  //-------------------------------------------------------------------------------------
174 
175  void writeSocket();
176 
177  // int_type underflow();
178 // int_type uflow();
179 // int_type pbackfail(int_type ch);
180 // streamsize showmanyc();
181 
182  char *buffer; // This is a pointer to the managed buffer space.
183  int length; // This is the length of the buffer.
184 
185 // const char * const begin_;
186 // const char * const end_;
187 // const char * const current_;
188 
189  std::queue<std::string> fifo;
190 
191  };
192 
193 }
194 
195 #endif
196 
core::Socket
Definition: Socket.h:33
core::Socket::write
int write(std::string data)
Definition: Socket.cpp:155
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:55
core::Socket::onDataReceived
virtual void onDataReceived(std::string data)
Called when data is received from the socket.
Definition: Socket.cpp:102
core::Socket::Socket
Socket(EPoll &ePoll, std::string text="")
Definition: Socket.cpp:8
core::Socket::shutdown
void shutdown(std::string text="unknown")
Definition: Socket.cpp:174
core::Socket::onRegister
virtual void onRegister()
Called before the socket has registered with the epoll processing.
Definition: Socket.cpp:53
core::Socket::eventReceived
bool eventReceived(struct epoll_event event)
Parse epoll event and call specified callbacks.
Definition: Socket.cpp:61
core::Socket::receiveData
virtual void receiveData(char *buffer, int bufferLength)
Definition: Socket.cpp:110
core::Socket::onUnregistered
virtual void onUnregistered()
Called when the socket has finished unregistering for the epoll processing.
Definition: Socket.cpp:59
core::Object
Definition: Object.h:8
core::Socket::getDescriptor
int getDescriptor()
Get the descriptor for the socket.
Definition: Socket.cpp:40
core::Socket::~Socket
~Socket()
Definition: Socket.cpp:14
core::Socket::setDescriptor
void setDescriptor(int descriptor)
Set the descriptor for the socket.
Definition: Socket.cpp:24