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 core::Object {
33 
34  public:
35 
36  Socket(EPoll &ePoll);
37  Socket(EPoll &ePoll, std::string text);
38  ~Socket();
39 
45 
46  void shutdown(std::string text = "unknown");
47 
52 
53  void setDescriptor(int descriptor);
54 
55  int getDescriptor();
56 
57  class {
58  int value;
59 
60  public:
61  int & operator = (const int &i) { return value = i; }
62  operator int () const { return value; }
63 
64  } bufferSize;
65 
75 
76  bool eventReceived(struct epoll_event event);
77 
81 
82  int write(std::string data);
83  void write(char *buffer, int length);
84 
85  void output(std::stringstream &out);
86 
93 
94  virtual void onRegister();
95  virtual void onRegistered();
96 
103 
104  virtual void onUnregister();
105 
106  bool needsToWrite();
107 
108  bool active = false;
109 
110  protected:
111 
112  EPoll &ePoll; // The EPoll control object.
113 
114  bool shutDown = false;
115 
116  void setBufferSize(int length);
117 
123 
124 // virtual void onConnected(); ///< Called when socket is open and ready to communicate.
125 
129 
130 // virtual void onDisconnected(); ///< Called when socket is closing and no longer ready to communicate.
131 
139 
140  virtual void onDataReceived(std::string data);
141 
142  virtual void onDataReceived(char *buffer, int len);
143 
148 
149  virtual void receiveData(char *buffer, int bufferLength);
150 
151  private:
152 
153  std::string text;
154  int descriptor = -1;
155  std::mutex lock;
156  std::mutex socketLock;
157  bool readHangup = false;
158 
159 // struct epoll_event event; // Event selection construction structure.
160 
161  //-------------------------------------------------------------------------------------
162  // the writeSocket is called when epoll has received a write request for a socket.
163  // Writing data to this socket is queued in the streambuf and permission is requested
164  // to write to the socket. This routine handles the writing of the streambuf data
165  // buffer to the socket.
166  //-------------------------------------------------------------------------------------
167 
168  void writeSocket();
169 
170  // int_type underflow();
171 // int_type uflow();
172 // int_type pbackfail(int_type ch);
173 // streamsize showmanyc();
174 
175  char *buffer; // This is a pointer to the managed buffer space.
176  int length; // This is the length of the buffer.
177 
178 // const char * const begin_;
179 // const char * const end_;
180 // const char * const current_;
181 
182  std::queue<std::string> fifo;
183 
184  };
185 
186 }
187 
188 #endif
189 
Definition: Command.cpp:4
Definition: EPoll.h:31
Definition: Socket.h:32
Definition: Object.h:8
virtual void onRegister()
Called when the socket has finished registering with the epoll processing.
Definition: Socket.cpp:57
virtual void receiveData(char *buffer, int bufferLength)
Definition: Socket.cpp:107
int write(std::string data)
Definition: Socket.cpp:154
bool eventReceived(struct epoll_event event)
Parse epoll event and call specified callbacks.
Definition: Socket.cpp:63
virtual void onDataReceived(std::string data)
Called when data is received from the socket.
Definition: Socket.cpp:99
void setDescriptor(int descriptor)
Set the descriptor for the socket.
Definition: Socket.cpp:32
int getDescriptor()
Get the descriptor for the socket.
Definition: Socket.cpp:48
virtual void onUnregister()
Called when the socket has finished unregistering for the epoll processing.
Definition: Socket.cpp:61
void shutdown(std::string text="unknown")
Definition: Socket.cpp:175