BMA Server Framework
/home/barant/Development/BMA/server_core/ServerCore/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 
40  void setDescriptor(int descriptor);
41 
42  int getDescriptor();
43 
44  class {
45  int value;
46 
47  public:
48  int & operator = (const int &i) { return value = i; }
49  operator int () const { return value; }
50 
51  } bufferSize;
52 
62 
63  void eventReceived(struct epoll_event event);
64 
68 
69  void write(std::string data);
70  void write(char *buffer, int length);
71 
72  void output(std::stringstream &out);
73 
78 
79  virtual void onRegistered();
80 
87 
88  virtual void onUnregistered();
89 
90  void enable(bool mode);
91 
92  protected:
93 
94  EPoll &ePoll; // The EPoll control object.
95 
96  bool shutDown = false;
97 
98  void setBufferSize(int length);
99 
105 
106  virtual void onConnected();
107 
108  virtual void onTLSInit();
109 
113 
114 // virtual void onDisconnected(); ///< Called when socket is closing and no longer ready to communicate.
115 
123 
124  virtual void onDataReceived(std::string data) = 0;
125 
126  void shutdown();
127 
132 
133  virtual void receiveData(char *buffer, int bufferLength);
134 
135  private:
136 
137  int descriptor = -1;
138  std::mutex lock;
139 
140  struct epoll_event event; // Event selection construction structure.
141 
142  //--------------------------------------------------
143  // These are used to schedule the socket activity.
144  //--------------------------------------------------
145 
146  void setRead();
147  void setWrite();
148  void setReadWrite();
149  void resetRead();
150  void resetWrite();;
151  void resetReadWrite(int x);
152  void clear();
153 
154  //-------------------------------------------------------------------------------------
155  // the writeSocket is called when epoll has received a write request for a socket.
156  // Writing data to this socket is queued in the streambuf and permission is requested
157  // to write to the socket. This routine handles the writing of the streambuf data
158  // buffer to the socket.
159  //-------------------------------------------------------------------------------------
160 
161  void writeSocket();
162 
163  // int_type underflow();
164 // int_type uflow();
165 // int_type pbackfail(int_type ch);
166 // streamsize showmanyc();
167 
168  char *buffer; // This is a pointer to the managed buffer space.
169  int length; // This is the length of the buffer.
170 
171 // const char * const begin_;
172 // const char * const end_;
173 // const char * const current_;
174 
175  std::queue<std::string> fifo;
176 
177  bool active = false;
178 
179  };
180 
181 }
182 
183 #endif
184 
Definition: EPoll.h:31
virtual void receiveData(char *buffer, int bufferLength)
Definition: Socket.cpp:86
virtual void onUnregistered()
Called when the socket has finished unregistering for the epoll processing.
Definition: Socket.cpp:44
Definition: Command.cpp:4
void enable(bool mode)
Enable the socket to read or write based upon buffer.
Definition: Socket.cpp:74
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:131
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:118