SocketServer.cpp 7.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (C) 2003 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


18
#include <ndb_global.h>
19
#include <my_pthread.h>
20

tomas@poseidon.bredbandsbolaget.se's avatar
tomas@poseidon.bredbandsbolaget.se committed
21
#include <SocketServer.hpp>
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

#include <NdbTCP.h>
#include <NdbOut.hpp>
#include <NdbThread.h>
#include <NdbSleep.h>

#define DEBUG(x) ndbout << x << endl;

SocketServer::SocketServer(int maxSessions) :
  m_sessions(10),
  m_services(5)
{
  m_thread = 0;
  m_stopThread = false;
  m_maxSessions = maxSessions;
}

SocketServer::~SocketServer() {
40 41
  unsigned i;
  for(i = 0; i<m_sessions.size(); i++){
42 43
    delete m_sessions[i].m_session;
  }
44
  for(i = 0; i<m_services.size(); i++){
45 46 47 48 49
    delete m_services[i].m_service;
  }
}

bool
50
SocketServer::tryBind(unsigned short port, const char * intface) {
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  struct sockaddr_in servaddr;
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(port);
  
  if(intface != 0){
    if(Ndb_getInAddr(&servaddr.sin_addr, intface))
      return false;
  }
  
  const NDB_SOCKET_TYPE sock  = socket(AF_INET, SOCK_STREAM, 0);
  if (sock == NDB_INVALID_SOCKET) {
    return false;
  }
  
  const int on = 1;
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, 
		 (const char*)&on, sizeof(on)) == -1) {
    NDB_CLOSE_SOCKET(sock);
    return false;
  }
  
  if (bind(sock, (struct sockaddr*) &servaddr, sizeof(servaddr)) == -1) {
    NDB_CLOSE_SOCKET(sock);
    return false;
  }

  NDB_CLOSE_SOCKET(sock);
  return true;
}

bool
SocketServer::setup(SocketServer::Service * service, 
		    unsigned short port, 
		    const char * intface){
87 88
  DBUG_ENTER("SocketServer::setup");
  DBUG_PRINT("enter",("interface=%s, port=%d", intface, port));
89 90 91 92 93 94 95 96
  struct sockaddr_in servaddr;
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(port);
  
  if(intface != 0){
    if(Ndb_getInAddr(&servaddr.sin_addr, intface))
97
      DBUG_RETURN(false);
98 99 100 101
  }
  
  const NDB_SOCKET_TYPE sock  = socket(AF_INET, SOCK_STREAM, 0);
  if (sock == NDB_INVALID_SOCKET) {
102 103 104
    DBUG_PRINT("error",("socket() - %d - %s",
			errno, strerror(errno)));
    DBUG_RETURN(false);
105 106 107 108 109
  }
  
  const int on = 1;
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, 
		 (const char*)&on, sizeof(on)) == -1) {
110 111
    DBUG_PRINT("error",("getsockopt() - %d - %s",
			errno, strerror(errno)));
112
    NDB_CLOSE_SOCKET(sock);
113
    DBUG_RETURN(false);
114 115 116
  }
  
  if (bind(sock, (struct sockaddr*) &servaddr, sizeof(servaddr)) == -1) {
117 118
    DBUG_PRINT("error",("bind() - %d - %s",
			errno, strerror(errno)));
119
    NDB_CLOSE_SOCKET(sock);
120
    DBUG_RETURN(false);
121 122 123
  }
  
  if (listen(sock, m_maxSessions) == -1){
124 125
    DBUG_PRINT("error",("listen() - %d - %s",
			errno, strerror(errno)));
126
    NDB_CLOSE_SOCKET(sock);
127
    DBUG_RETURN(false);
128 129 130 131 132 133
  }
  
  ServiceInstance i;
  i.m_socket = sock;
  i.m_service = service;
  m_services.push_back(i);
134
  DBUG_RETURN(true);
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
}

void
SocketServer::doAccept(){
  fd_set readSet, exceptionSet;
  FD_ZERO(&readSet);
  FD_ZERO(&exceptionSet);
  
  m_services.lock();
  int maxSock = 0;
  for (unsigned i = 0; i < m_services.size(); i++){
    const NDB_SOCKET_TYPE s = m_services[i].m_socket;
    FD_SET(s, &readSet);
    FD_SET(s, &exceptionSet);
    maxSock = (maxSock > s ? maxSock : s);
  }
  struct timeval timeout;
  timeout.tv_sec  = 1;
  timeout.tv_usec = 0;
  
  if(select(maxSock + 1, &readSet, 0, &exceptionSet, &timeout) > 0){
    for (unsigned i = 0; i < m_services.size(); i++){
      ServiceInstance & si = m_services[i];
      
      if(FD_ISSET(si.m_socket, &readSet)){
	NDB_SOCKET_TYPE childSock = accept(si.m_socket, 0, 0);
	if(childSock == NDB_INVALID_SOCKET){
	  continue;
	}
	
	SessionInstance s;
	s.m_service = si.m_service;
	s.m_session = si.m_service->newSession(childSock);
	if(s.m_session != 0){
	  m_sessions.push_back(s);
	  startSession(m_sessions.back());
	}
	
	continue;
      }      
      
      if(FD_ISSET(si.m_socket, &exceptionSet)){
	DEBUG("socket in the exceptionSet");
	continue;
      }
    }
  }
  m_services.unlock();
}

extern "C"
void* 
socketServerThread_C(void* _ss){
  SocketServer * ss = (SocketServer *)_ss;
  
190
  my_thread_init();
191
  ss->doRun();
192
  my_thread_end();  
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
  NdbThread_Exit(0);
  return 0;
}

void
SocketServer::startServer(){
  m_threadLock.lock();
  if(m_thread == 0 && m_stopThread == false){
    m_thread = NdbThread_Create(socketServerThread_C,
				(void**)this,
				32768,
				"NdbSockServ",
				NDB_THREAD_PRIO_LOW);
  }
  m_threadLock.unlock();
}

void
SocketServer::stopServer(){
  m_threadLock.lock();
  if(m_thread != 0){
    m_stopThread = true;
    
    void * res;
    NdbThread_WaitFor(m_thread, &res);
    NdbThread_Destroy(&m_thread);
    m_thread = 0;
  }
  m_threadLock.unlock();
}

void
SocketServer::doRun(){

  while(!m_stopThread){
    checkSessions();
    if(m_sessions.size() < m_maxSessions){
      doAccept();
    } else {
      NdbSleep_MilliSleep(200);
    }
  }
}

void
SocketServer::startSession(SessionInstance & si){
  si.m_thread = NdbThread_Create(sessionThread_C,
				 (void**)si.m_session,
				 32768,
				 "NdbSock_Session",
				 NDB_THREAD_PRIO_LOW);
}

static
bool 
transfer(NDB_SOCKET_TYPE sock){
#if defined NDB_OSE || defined NDB_SOFTOSE
  const PROCESS p = current_process();
  const size_t ps = sizeof(PROCESS);
  int res = setsockopt(sock, SOL_SOCKET, SO_OSEOWNER, &p, ps);
  if(res != 0){
    ndbout << "Failed to transfer ownership of socket" << endl;
    return false;
  }
#endif
  return true;
}

261 262 263 264 265 266 267 268 269
void
SocketServer::foreachSession(void (*func)(SocketServer::Session*, void *), void *data)
{
  for(int i = m_sessions.size() - 1; i >= 0; i--){
    (*func)(m_sessions[i].m_session, data);
  }
  checkSessions();
}

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
void
SocketServer::checkSessions(){
  for(int i = m_sessions.size() - 1; i >= 0; i--){
    if(m_sessions[i].m_session->m_stopped){
      if(m_sessions[i].m_thread != 0){
	void* ret;
	NdbThread_WaitFor(m_sessions[i].m_thread, &ret);
	NdbThread_Destroy(&m_sessions[i].m_thread);
      } 
      m_sessions[i].m_session->stopSession();
      delete m_sessions[i].m_session;
      m_sessions.erase(i);
    }
  }
}

void
SocketServer::stopSessions(bool wait){
288 289
  int i;
  for(i = m_sessions.size() - 1; i>=0; i--)
290 291 292 293
  {
    m_sessions[i].m_session->stopSession();
    m_sessions[i].m_session->m_stop = true; // to make sure
  }
294
  for(i = m_services.size() - 1; i>=0; i--)
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    m_services[i].m_service->stopSessions();
  
  if(wait){
    while(m_sessions.size() > 0){
      checkSessions();
      NdbSleep_MilliSleep(100);
    }
  }
}

/***** Session code ******/

extern "C"
void* 
sessionThread_C(void* _sc){
  SocketServer::Session * si = (SocketServer::Session *)_sc;

312
  my_thread_init();
313 314
  if(!transfer(si->m_socket)){
    si->m_stopped = true;
315
    my_thread_end();
316 317 318 319 320 321 322 323 324 325 326 327
    NdbThread_Exit(0);
    return 0;
  }
  
  if(!si->m_stop){
    si->m_stopped = false;
    si->runSession();
  } else {
    NDB_CLOSE_SOCKET(si->m_socket);
  }
  
  si->m_stopped = true;
328
  my_thread_end();
329 330 331
  NdbThread_Exit(0);
  return 0;
}
332 333 334

template class MutexVector<SocketServer::ServiceInstance>;
template class MutexVector<SocketServer::SessionInstance>;