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 
31 
32  class Socket : public std::streambuf,
33  public core::Object {
34 
35  public:
36 
37  Socket(EPoll &ePoll);
38  ~Socket();
39 
41 
42  void shutdown();
43 
44  void setDescriptor(int descriptor);
45 
46  int getDescriptor();
47 
48  class {
49  int value;
50 
51  public:
52  int & operator = (const int &i) { return value = i; }
53  operator int () const { return value; }
54 
55  } bufferSize;
56 
66 
67  void eventReceived(struct epoll_event event);
68 
72 
73  void write(std::string data);
74  void write(char *buffer, int length);
75 
76  void output(std::stringstream &out);
77 
82 
83  virtual void onRegistered();
84 
91 
92  virtual void onUnregistered();
93 
94  void enable(bool mode);
95 
96  protected:
97 
98  EPoll &ePoll; // The EPoll control object.
99 
100  bool shutDown = false;
101 
102  void setBufferSize(int length);
103 
109 
110  virtual void onConnected();
111 
112  virtual void onTLSInit();
113 
117 
118 // virtual void onDisconnected(); ///< Called when socket is closing and no longer ready to communicate.
119 
127 
128  virtual void onDataReceived(std::string data) = 0;
129 
134 
135  virtual void receiveData(char *buffer, int bufferLength);
136 
137  private:
138 
139  int descriptor = -1;
140  std::mutex lock;
141  bool readHangup = false;
142 
143  struct epoll_event event; // Event selection construction structure.
144 
145  //--------------------------------------------------
146  // These are used to schedule the socket activity.
147  //--------------------------------------------------
148 
149  void setRead();
150  void setWrite();
151  void setReadWrite();
152  void resetRead();
153  void resetWrite();;
154  void resetReadWrite();
155  void clear();
156 
157  //-------------------------------------------------------------------------------------
158  // the writeSocket is called when epoll has received a write request for a socket.
159  // Writing data to this socket is queued in the streambuf and permission is requested
160  // to write to the socket. This routine handles the writing of the streambuf data
161  // buffer to the socket.
162  //-------------------------------------------------------------------------------------
163 
164  void writeSocket();
165 
166  // int_type underflow();
167 // int_type uflow();
168 // int_type pbackfail(int_type ch);
169 // streamsize showmanyc();
170 
171  char *buffer; // This is a pointer to the managed buffer space.
172  int length; // This is the length of the buffer.
173 
174 // const char * const begin_;
175 // const char * const end_;
176 // const char * const current_;
177 
178  std::queue<std::string> fifo;
179 
180  bool active = false;
181 
182  };
183 
184 }
185 
186 #endif
187 
Definition: EPoll.h:31
virtual void receiveData(char *buffer, int bufferLength)
Definition: Socket.cpp:87
virtual void onUnregistered()
Called when the socket has finished unregistering for the epoll processing.
Definition: Socket.cpp:44
Definition: Command.cpp:3
void enable(bool mode)
Enable the socket to read or write based upon buffer.
Definition: Socket.cpp:71
virtual void onDataReceived(std::string data)=0
Called when data is received from the socket.
void setDescriptor(int descriptor)
Set the descriptor for the socket.
Definition: Socket.cpp:21
void write(std::string data)
Definition: Socket.cpp:132
Definition: Socket.h:32
void eventReceived(struct epoll_event event)
Parse epoll event and call specified callbacks.
Definition: Socket.cpp:48
int getDescriptor()
Get the descriptor for the socket.
Definition: Socket.cpp:29
virtual void onRegistered()
Called when the socket has finished registering with the epoll processing.
Definition: Socket.cpp:40
Definition: Object.h:8
virtual void onConnected()
Called when socket is open and ready to communicate.
Definition: Socket.cpp:119