MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SocketServer.hpp
1 /*
2  Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #ifndef SOCKET_SERVER_HPP
19 #define SOCKET_SERVER_HPP
20 
21 #include <NdbTCP.h>
22 #include <NdbMutex.h>
23 #include <NdbThread.h>
24 #include <Vector.hpp>
25 
26 extern "C" void* sessionThread_C(void*);
27 extern "C" void* socketServerThread_C(void*);
28 
32 class SocketServer {
33 public:
37  class Session {
38  public:
39  virtual ~Session() {}
40  virtual void runSession(){}
41  virtual void stopSession(){ m_stop = true; }
42  protected:
43  friend class SocketServer;
44  friend void* sessionThread_C(void*);
45  Session(NDB_SOCKET_TYPE sock) :
46  m_stop(false),
47  m_socket(sock),
48  m_refCount(0),
49  m_thread_stopped(false)
50  {
51  DBUG_ENTER("SocketServer::Session");
52  DBUG_PRINT("enter",("NDB_SOCKET: " MY_SOCKET_FORMAT,
53  MY_SOCKET_FORMAT_VALUE(m_socket)));
54  DBUG_VOID_RETURN;
55  }
56  bool m_stop; // Has the session been ordered to stop?
57  NDB_SOCKET_TYPE m_socket;
58  unsigned m_refCount;
59  private:
60  bool m_thread_stopped; // Has the session thread stopped?
61  };
62 
66  class Service {
67  public:
68  Service() {}
69  virtual ~Service(){}
70 
76  virtual Session * newSession(NDB_SOCKET_TYPE theSock) = 0;
77  virtual void stopSessions(){}
78  };
79 
83  SocketServer(unsigned maxSessions = ~(unsigned)0);
84  ~SocketServer();
85 
91  static bool tryBind(unsigned short port, const char * intface = 0);
92 
98  bool setup(Service *, unsigned short *port, const char * pinterface = 0);
99 
103  struct NdbThread* startServer();
104  void stopServer();
105 
119  bool stopSessions(bool wait = false, unsigned wait_timeout = 0);
120 
121  void foreachSession(void (*f)(Session*, void*), void *data);
122  void checkSessions();
123 
124 private:
125  struct SessionInstance {
126  Service * m_service;
127  Session * m_session;
128  NdbThread * m_thread;
129  };
130  struct ServiceInstance {
131  Service * m_service;
132  NDB_SOCKET_TYPE m_socket;
133  };
134  NdbLockable m_session_mutex;
135  Vector<SessionInstance> m_sessions;
136  MutexVector<ServiceInstance> m_services;
137  ndb_socket_poller m_services_poller;
138  unsigned m_maxSessions;
139 
140  bool doAccept();
141  void checkSessionsImpl();
142  void startSession(SessionInstance &);
143 
148  bool m_stopThread;
149  struct NdbThread * m_thread;
150  NdbLockable m_threadLock;
151  void doRun();
152  friend void* socketServerThread_C(void*);
153  friend void* sessionThread_C(void*);
154 };
155 
156 #endif