SocketServer.cpp 8.13 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

unknown's avatar
unknown 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() {
unknown's avatar
unknown committed
40 41
  unsigned i;
  for(i = 0; i<m_sessions.size(); i++){
42 43
    delete m_sessions[i].m_session;
  }
unknown's avatar
unknown committed
44
  for(i = 0; i<m_services.size(); i++){
45 46 47 48 49
    delete m_services[i].m_service;
  }
}

bool
unknown's avatar
unknown committed
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
  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;
  }
  
unknown's avatar
unknown committed
67 68
  DBUG_PRINT("info",("NDB_SOCKET: %d", sock));

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  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, 
87
		    unsigned short * port,
88
		    const char * intface){
89
  DBUG_ENTER("SocketServer::setup");
90
  DBUG_PRINT("enter",("interface=%s, port=%u", intface, *port));
91 92 93 94
  struct sockaddr_in servaddr;
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
95
  servaddr.sin_port = htons(*port);
96 97 98
  
  if(intface != 0){
    if(Ndb_getInAddr(&servaddr.sin_addr, intface))
99
      DBUG_RETURN(false);
100 101 102 103
  }
  
  const NDB_SOCKET_TYPE sock  = socket(AF_INET, SOCK_STREAM, 0);
  if (sock == NDB_INVALID_SOCKET) {
104 105 106
    DBUG_PRINT("error",("socket() - %d - %s",
			errno, strerror(errno)));
    DBUG_RETURN(false);
107 108
  }
  
unknown's avatar
unknown committed
109 110
  DBUG_PRINT("info",("NDB_SOCKET: %d", sock));
 
111 112 113
  const int on = 1;
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, 
		 (const char*)&on, sizeof(on)) == -1) {
114 115
    DBUG_PRINT("error",("getsockopt() - %d - %s",
			errno, strerror(errno)));
116
    NDB_CLOSE_SOCKET(sock);
117
    DBUG_RETURN(false);
118 119 120
  }
  
  if (bind(sock, (struct sockaddr*) &servaddr, sizeof(servaddr)) == -1) {
121 122
    DBUG_PRINT("error",("bind() - %d - %s",
			errno, strerror(errno)));
123
    NDB_CLOSE_SOCKET(sock);
124
    DBUG_RETURN(false);
125
  }
126 127 128 129 130

  /* Get the port we bound to */
  SOCKET_SIZE_TYPE sock_len = sizeof(servaddr);
  if(getsockname(sock,(struct sockaddr*)&servaddr,&sock_len)<0) {
    ndbout_c("An error occurred while trying to find out what"
131
	     " port we bound to. Error: %s",strerror(errno));
132 133 134 135
    NDB_CLOSE_SOCKET(sock);
    DBUG_RETURN(false);
  }

136
  DBUG_PRINT("info",("bound to %u",ntohs(servaddr.sin_port)));
137
  if (listen(sock, m_maxSessions) == -1){
138 139
    DBUG_PRINT("error",("listen() - %d - %s",
			errno, strerror(errno)));
140
    NDB_CLOSE_SOCKET(sock);
141
    DBUG_RETURN(false);
142 143 144 145 146 147
  }
  
  ServiceInstance i;
  i.m_socket = sock;
  i.m_service = service;
  m_services.push_back(i);
148 149 150

  *port = ntohs(servaddr.sin_port);

151
  DBUG_RETURN(true);
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 190 191 192 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 261 262 263 264 265 266 267 268 269 270 271 272 273
}

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;
  ss->doRun();
  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;
}

274 275 276 277 278 279 280 281 282
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();
}

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
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){
unknown's avatar
unknown committed
301 302
  int i;
  for(i = m_sessions.size() - 1; i>=0; i--)
303 304 305 306
  {
    m_sessions[i].m_session->stopSession();
    m_sessions[i].m_session->m_stop = true; // to make sure
  }
unknown's avatar
unknown committed
307
  for(i = m_services.size() - 1; i>=0; i--)
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
    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;

  if(!transfer(si->m_socket)){
    si->m_stopped = true;
    return 0;
  }
  
330 331 332 333 334 335 336 337 338 339 340 341
  /**
   * may have m_stopped set if we're transforming a mgm
   * connection into a transporter connection.
   */
  if(!si->m_stopped)
  {
    if(!si->m_stop){
      si->m_stopped = false;
      si->runSession();
    } else {
      NDB_CLOSE_SOCKET(si->m_socket);
    }
342 343 344 345 346
  }
  
  si->m_stopped = true;
  return 0;
}
347 348 349

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