BMA Server Framework
/home/barant/Development/BMA/server_core/ServerCore/BMASocket.h
1 #ifndef __BMASocket_h__
2 #define __BMASocket_h__
3 
4 #include "includes"
5 #include "BMAObject.h"
6 #include "BMAEvent.h"
7 #include "BMAProperty.h"
8 class BMAEPoll;
9 
30 
31 class BMASocket : public std::streambuf,
32  public BMAObject {
33 
34  public:
35 
36  BMASocket(BMAEPoll &ePoll);
37  ~BMASocket();
38 
39  void setDescriptor(int descriptor);
40 
41  int getDescriptor();
42 
43  class {
44  int value;
45 
46  public:
47  int & operator = (const int &i) { return value = i; }
48  operator int () const { return value; }
49 
50  } bufferSize;
51 
61 
62  void eventReceived(struct epoll_event event);
63 
67 
68  void write(std::string data);
69  void write(char *buffer, int length);
70 
71  void output(std::stringstream &out);
72 
77 
78  virtual void onRegistered();
79 
86 
87  virtual void onUnregistered();
88 
89  void enable(bool mode);
90 
91  protected:
92 
93  BMAEPoll &ePoll; // The EPoll control object.
94 
95  bool shutDown = false;
96 
97  void setBufferSize(int length);
98 
104 
105  virtual void onConnected();
106 
107  virtual void onTLSInit();
108 
112 
113 // virtual void onDisconnected(); ///< Called when socket is closing and no longer ready to communicate.
114 
122 
123  virtual void onDataReceived(std::string data) = 0;
124 
125  void shutdown();
126 
131 
132  virtual void receiveData(char *buffer, int bufferLength);
133 
134  private:
135 
136  int descriptor = -1;
137  std::mutex lock;
138 
139  struct epoll_event event; // Event selection construction structure.
140 
141  //--------------------------------------------------
142  // These are used to schedule the socket activity.
143  //--------------------------------------------------
144 
145  void setRead();
146  void setWrite();
147  void setReadWrite();
148  void resetRead();
149  void resetWrite();;
150  void resetReadWrite(int x);
151  void clear();
152 
153  //-------------------------------------------------------------------------------------
154  // the writeSocket is called when epoll has received a write request for a socket.
155  // Writing data to this socket is queued in the streambuf and permission is requested
156  // to write to the socket. This routine handles the writing of the streambuf data
157  // buffer to the socket.
158  //-------------------------------------------------------------------------------------
159 
160  void writeSocket();
161 
162  // int_type underflow();
163 // int_type uflow();
164 // int_type pbackfail(int_type ch);
165 // streamsize showmanyc();
166 
167  char *buffer; // This is a pointer to the managed buffer space.
168  int length; // This is the length of the buffer.
169 
170 // const char * const begin_;
171 // const char * const end_;
172 // const char * const current_;
173 
174  std::queue<std::string> fifo;
175 
176  bool active = false;
177 
178 };
179 
180 #endif
181 
Definition: BMASocket.h:31
void eventReceived(struct epoll_event event)
Parse epoll event and call specified callbacks.
Definition: BMASocket.cpp:46
virtual void onConnected()
Called when socket is open and ready to communicate.
Definition: BMASocket.cpp:116
int getDescriptor()
Get the descriptor for the socket.
Definition: BMASocket.cpp:27
virtual void onUnregistered()
Called when the socket has finished unregistering for the epoll processing.
Definition: BMASocket.cpp:42
Definition: BMAEPoll.h:29
virtual void onRegistered()
Called when the socket has finished registering with the epoll processing.
Definition: BMASocket.cpp:38
void write(std::string data)
Definition: BMASocket.cpp:129
void setDescriptor(int descriptor)
Set the descriptor for the socket.
Definition: BMASocket.cpp:19
void enable(bool mode)
Enable the socket to read or write based upon buffer.
Definition: BMASocket.cpp:72
virtual void receiveData(char *buffer, int bufferLength)
Definition: BMASocket.cpp:84
Definition: BMAObject.h:6
virtual void onDataReceived(std::string data)=0
Called when data is received from the socket.