CARMA C++
Server.h
1 #ifndef SZA_UTIL_SERVER_H
2 #define SZA_UTIL_SERVER_H
3 
11 #include <list>
12 #include <string>
13 #include <stdio.h>
14 
15 #include "carma/szautil/FdSet.h"
16 #include "carma/szautil/NetDat.h"
19 #include "carma/szautil/Runnable.h"
21 #include "carma/szautil/TimeOut.h"
22 
23 namespace sza {
24  namespace util {
25 
26  class Port;
27  class SignalTask;
28  class TcpListener;
29 
30  class Server : public Runnable {
31 
32  public:
33 
34  class ServerData {
35  public:
36  ServerData();
37  virtual ~ServerData();
38  };
39 
40  class ServerConnection {
41  public:
42 
43  // A queue of messages to be send to this client
44 
45  PipeQueue::MsgQueue msgQueue_;
46 
47  // A Handler for sending/receiving messages to/from this client
48 
49  NetHandler handler_;
50 
51  // A temporary buffer for storing data received from this
52  // client
53 
54  std::vector<unsigned char> bytes_;
55 
56  // A handle to our parent
57 
58  Server* parent_;
59 
60  // Client-specific data
61 
62  ServerData* data_;
63 
64  // True when this connection has been initialized, whatever
65  // that means for a particular type of server
66 
67  bool initialized_;
68 
69  //------------------------------------------------------------
70  // Methods of this class
71  //------------------------------------------------------------
72 
73  ServerConnection(int fd,
74  unsigned readBufSize, unsigned sendBufSize,
75  Server* parent);
76 
77  ~ServerConnection();
78 
79  void setSendBufferSize(unsigned size);
80  void setReadBufferSize(unsigned size);
81 
82  void packClientData(NetDat& dat);
83  void packClientData(unsigned char* buffer, unsigned datSize);
84  void stageClientData(unsigned char* buffer, unsigned datSize);
85  void checkMsgQueue();
86  };
87 
91  Server(bool spawnThread, int listenPort, unsigned readBufSize=0,
92  unsigned sendBufSize=0);
93 
97  virtual ~Server();
98 
102  virtual void run();
103 
104  protected:
105 
106  unsigned readBufSize_;
107 
108  unsigned sendBufSize_;
109 
110  TimeOut timeOut_;
111 
112  virtual void serviceSelect();
113 
114  // Method called when we time out in our select loop
115 
116  virtual void timeOutAction() {};
117 
118  // Method called when data have been completely read from a client
119 
120  virtual void readClientData(ServerConnection* conn) {};
121 
122  // Method called immediately after a client has connected
123 
124  virtual void acceptClientAction(ServerConnection* conn) {};
125  void sendClientData(NetDat& dat, ServerConnection* client);
126 
127  void setReadBufSize(unsigned size);
128  void setSendBufSize(unsigned size);
129 
130  void setTimeOutSeconds(unsigned int seconds);
131 
132  protected:
133 
137  FdSet fdSet_;
138 
139  protected:
140 
141  unsigned int timeOutSeconds_;
142 
146  SignalTask* signalTask_;
147 
151  bool stop_;
152 
156  TcpListener* listener_;
157 
161  std::list<ServerConnection*> clients_;
162 
166  void initMembers(int listenPort, unsigned readBufSize, unsigned sendBufSize);
167 
172  void listen(unsigned port, unsigned nClients = 5);
173 
177  void sendShutdownMsg();
178 
182  void acceptConnection();
183 
184  private:
185 
189  void checkClients();
190 
194  void readFromClient(NetHandler* client);
195 
199  static SIGNALTASK_HANDLER_FN(shutDown);
200 
204  static RUN_FN(runFn);
205 
206  // Check clients for data to be read
207 
208  void checkClientsForReadableData();
209 
210  // Check clients for writability
211 
212  void checkClientsForWritability();
213 
214  static NET_READ_HANDLER(readHandler);
215  static NET_SEND_HANDLER(sendHandler);
216  static NET_ERROR_HANDLER(errHandler);
217 
218  }; // End class Server
219 
220  } // End namespace util
221 } // End namespace sza
222 
223 
224 
225 #endif // End #ifndef SZA_UTIL_SERVER_H
Tagged: Wed Jul 6 13:41:09 PDT 2005.
Tagged: Fri Nov 14 12:39:36 UTC 2003.
Tagged: Sun Apr 4 22:36:40 UTC 2004.
Tagged: Tue May 2 16:31:46 PDT 2006.
Started: Thu Feb 26 22:08:23 UTC 2004.
Tagged: Fri Nov 14 12:39:34 UTC 2003.