My Project
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
Server.h
Go to the documentation of this file.
1 #ifndef GCP_UTIL_SERVER_H
2 #define GCP_UTIL_SERVER_H
3 
11 #include <list>
12 #include <string>
13 #include <stdio.h>
14 
15 #include "gcp/util/common/FdSet.h"
16 #include "gcp/util/common/NetDat.h"
17 #include "gcp/util/common/NetHandler.h"
18 #include "gcp/util/common/Runnable.h"
19 #include "gcp/util/common/SignalTaskMsg.h"
20 #include "gcp/util/common/TimeOut.h"
21 
22 namespace gcp {
23  namespace util {
24 
25  class Port;
26  class SignalTask;
27  class TcpListener;
28 
29  class Server : public Runnable {
30 
31  public:
32 
34  NetHandler handler_;
35  Server* parent_;
36 
37  ServerConnection(int fd,
38  unsigned readBufSize, unsigned sendBufSize,
39  Server* parent);
40 
42  };
43 
47  Server(bool spawnThread, int listenPort, unsigned readBufSize=0,
48  unsigned sendBufSize=0);
49 
53  virtual ~Server();
54 
58  virtual void run();
59 
60  protected:
61 
62  unsigned readBufSize_;
63 
64  unsigned sendBufSize_;
65 
66  TimeOut timeOut_;
67 
68  // Method called when we time out in our select loop
69 
70  virtual void timeOutAction() {};
71 
72  // Method called when data have been completely read from a client
73 
74  virtual void readClientData(NetHandler& handler) {};
75 
76  // Method called immediately after a client has connected
77 
78  virtual void acceptClientAction() {};
79 
80  // Method to check any other fds that may be registered
81 
82  virtual void checkOtherFds() {};
83 
84  void sendClientData(NetDat& dat);
85 
86  void setReadBufSize(unsigned size);
87  void setSendBufSize(unsigned size);
88 
89  void setTimeOutSeconds(unsigned int seconds);
90 
91  protected:
92 
97 
98  private:
99 
100  unsigned int timeOutSeconds_;
101 
105  SignalTask* signalTask_;
106 
110  bool stop_;
111 
115  TcpListener* listener_;
116 
120  std::list<ServerConnection*> clients_;
121 
125  void initMembers(int listenPort, unsigned readBufSize, unsigned sendBufSize);
126 
131  void listen(unsigned port, unsigned nClients = 5);
132 
136  void sendShutdownMsg();
137 
141  void acceptConnection();
142 
143  private:
144 
148  void checkClients();
149 
153  void readFromClient(NetHandler* client);
154 
158  static SIGNALTASK_HANDLER_FN(shutDown);
159 
163  static RUN_FN(runFn);
164 
165  // Check clients for data to be read
166 
167  void checkClientsForReadableData();
168 
169  // Check clients for writability
170 
171  void checkClientsForWritability();
172 
173  static NET_READ_HANDLER(readHandler);
174  static NET_SEND_HANDLER(sendHandler);
175  static NET_ERROR_HANDLER(errHandler);
176 
177  }; // End class Server
178 
179  } // End namespace util
180 } // End namespace gcp
181 
182 
183 
184 #endif // End #ifndef GCP_UTIL_SERVER_H
ServerConnection(int fd, unsigned readBufSize, unsigned sendBufSize, Server *parent)
Definition: Server.cc:307
FdSet fdSet_
Definition: Server.h:96
Definition: SignalTask.h:53
Definition: NetHandler.h:16
Definition: FdSet.h:16
Definition: TimeOut.h:20
~ServerConnection()
Definition: Server.cc:325
Definition: Runnable.h:23
Definition: TcpListener.h:14
Definition: Server.h:29
Server(bool spawnThread, int listenPort, unsigned readBufSize=0, unsigned sendBufSize=0)
Definition: Server.cc:24
virtual ~Server()
Definition: Server.cc:75
void sendClientData(NetDat &dat)
Definition: Server.cc:237
virtual void run()
Definition: Server.cc:103