2007-02-25 23:16:26 +00:00
|
|
|
/***************************************************************************
|
|
|
|
|
* Copyright (C) 2007 by Lothar May *
|
|
|
|
|
* *
|
|
|
|
|
* 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. *
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <net/serverthread.h>
|
2007-03-13 23:27:17 +00:00
|
|
|
#include <net/servercontext.h>
|
|
|
|
|
#include <net/connectdata.h>
|
|
|
|
|
#include <net/serverrecvthread.h>
|
2007-02-25 23:16:26 +00:00
|
|
|
#include <net/socket_helper.h>
|
2007-03-13 23:27:17 +00:00
|
|
|
#include <net/serverexception.h>
|
|
|
|
|
#include <net/socket_msg.h>
|
|
|
|
|
|
|
|
|
|
#define ACCEPT_TIMEOUT_MSEC 50
|
|
|
|
|
#define NET_SERVER_LISTEN_BACKLOG 5
|
2007-02-25 23:16:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
ServerThread::ServerThread()
|
|
|
|
|
{
|
2007-03-13 23:27:17 +00:00
|
|
|
m_context.reset(new ServerContext);
|
2007-02-25 23:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ServerThread::~ServerThread()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-03-13 23:27:17 +00:00
|
|
|
ServerThread::Init(unsigned serverPort, bool ipv6, const std::string &pwd)
|
2007-02-25 23:16:26 +00:00
|
|
|
{
|
|
|
|
|
if (IsRunning())
|
|
|
|
|
return; // TODO: throw exception
|
2007-03-13 23:27:17 +00:00
|
|
|
|
|
|
|
|
ServerContext &context = GetContext();
|
|
|
|
|
|
|
|
|
|
context.SetAddrFamily(ipv6 ? AF_INET6 : AF_INET);
|
|
|
|
|
context.SetServerPort(serverPort);
|
|
|
|
|
context.SetPassword(pwd);
|
2007-02-25 23:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ServerThread::Main()
|
|
|
|
|
{
|
2007-03-13 23:27:17 +00:00
|
|
|
m_recvThread.reset(new ServerRecvThread);
|
|
|
|
|
try
|
2007-02-25 23:16:26 +00:00
|
|
|
{
|
2007-03-13 23:27:17 +00:00
|
|
|
Listen();
|
|
|
|
|
GetRecvThread().Run();
|
2007-02-25 23:16:26 +00:00
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
while (!ShouldTerminate())
|
|
|
|
|
{
|
|
|
|
|
// The main server thread is simple. It only accepts connections.
|
|
|
|
|
AcceptLoop();
|
|
|
|
|
}
|
|
|
|
|
} catch (const NetException &)
|
|
|
|
|
{
|
|
|
|
|
// TODO: callback.
|
2007-02-25 23:16:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-13 23:27:17 +00:00
|
|
|
void
|
|
|
|
|
ServerThread::Listen()
|
|
|
|
|
{
|
|
|
|
|
ServerContext &context = GetContext();
|
|
|
|
|
|
|
|
|
|
// if (context.GetServerPort() < 1024)
|
|
|
|
|
// throw ServerException(ERR_SOCK_INVALID_PORT, 0);
|
|
|
|
|
|
|
|
|
|
context.SetSocket(socket(context.GetAddrFamily(), SOCK_STREAM, 0));
|
|
|
|
|
if (!IS_VALID_SOCKET(context.GetSocket()))
|
|
|
|
|
throw ServerException(ERR_SOCK_CREATION_FAILED, SOCKET_ERRNO());
|
|
|
|
|
|
|
|
|
|
unsigned long mode = 1;
|
|
|
|
|
if (IOCTLSOCKET(context.GetSocket(), FIONBIO, &mode) == SOCKET_ERROR)
|
|
|
|
|
throw ServerException(ERR_SOCK_CREATION_FAILED, SOCKET_ERRNO());
|
|
|
|
|
|
|
|
|
|
context.GetServerSockaddr()->ss_family = context.GetAddrFamily();
|
|
|
|
|
|
|
|
|
|
if (!socket_string_to_addr(
|
|
|
|
|
"0.0.0.0",
|
|
|
|
|
context.GetAddrFamily(),
|
|
|
|
|
(struct sockaddr *)context.GetServerSockaddr(),
|
|
|
|
|
context.GetServerSockaddrSize()))
|
|
|
|
|
{
|
|
|
|
|
throw ServerException(ERR_SOCK_SET_ADDR_FAILED, 0);
|
|
|
|
|
}
|
|
|
|
|
if (!socket_set_port(
|
|
|
|
|
context.GetServerPort(),
|
|
|
|
|
context.GetAddrFamily(),
|
|
|
|
|
(struct sockaddr *)context.GetServerSockaddr(),
|
|
|
|
|
context.GetServerSockaddrSize()))
|
|
|
|
|
{
|
|
|
|
|
throw ServerException(ERR_SOCK_SET_PORT_FAILED, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!IS_VALID_BIND(bind(
|
|
|
|
|
context.GetSocket(),
|
|
|
|
|
(const struct sockaddr *)context.GetServerSockaddr(),
|
|
|
|
|
context.GetServerSockaddrSize())))
|
|
|
|
|
{
|
|
|
|
|
throw ServerException(ERR_SOCK_BIND_FAILED, SOCKET_ERRNO());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!IS_VALID_LISTEN(listen(context.GetSocket(), NET_SERVER_LISTEN_BACKLOG)))
|
|
|
|
|
{
|
|
|
|
|
throw ServerException(ERR_SOCK_LISTEN_FAILED, SOCKET_ERRNO());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ServerThread::AcceptLoop()
|
|
|
|
|
{
|
|
|
|
|
ServerContext &context = GetContext();
|
|
|
|
|
|
|
|
|
|
fd_set readSet;
|
|
|
|
|
struct timeval timeout;
|
|
|
|
|
|
|
|
|
|
FD_ZERO(&readSet);
|
|
|
|
|
FD_SET(context.GetSocket(), &readSet);
|
|
|
|
|
|
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
|
timeout.tv_usec = ACCEPT_TIMEOUT_MSEC * 1000;
|
|
|
|
|
int selectResult = select(context.GetSocket() + 1, &readSet, NULL, NULL, &timeout);
|
|
|
|
|
if (!IS_VALID_SELECT(selectResult))
|
|
|
|
|
{
|
|
|
|
|
throw ServerException(ERR_SOCK_SELECT_FAILED, SOCKET_ERRNO());
|
|
|
|
|
}
|
|
|
|
|
if (selectResult > 0) // accept is possible
|
|
|
|
|
{
|
|
|
|
|
boost::shared_ptr<ConnectData> tmpData(new ConnectData);
|
|
|
|
|
socklen_t addrSize = sizeof(*tmpData->GetSockaddr());
|
|
|
|
|
tmpData->SetSocket(accept(context.GetSocket(), (struct sockaddr *)tmpData->GetSockaddr(), &addrSize));
|
|
|
|
|
|
|
|
|
|
if (!IS_VALID_SOCKET(tmpData->GetSocket()))
|
|
|
|
|
{
|
|
|
|
|
throw ServerException(ERR_SOCK_ACCEPT_FAILED, SOCKET_ERRNO());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GetRecvThread().AddConnection(tmpData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ServerContext &
|
|
|
|
|
ServerThread::GetContext() const
|
|
|
|
|
{
|
|
|
|
|
assert(m_context.get());
|
|
|
|
|
return *m_context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ServerContext &
|
|
|
|
|
ServerThread::GetContext()
|
|
|
|
|
{
|
|
|
|
|
assert(m_context.get());
|
|
|
|
|
return *m_context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ServerRecvThread &
|
|
|
|
|
ServerThread::GetRecvThread()
|
|
|
|
|
{
|
|
|
|
|
assert(m_recvThread.get());
|
|
|
|
|
return *m_recvThread;
|
|
|
|
|
}
|
|
|
|
|
|