2009-02-27 11:54:59 +00:00
|
|
|
/*
|
2011-01-15 04:01:31 +00:00
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
* Copyright (C) 2002 Chris Schoeneman, Nick Bolton, Sorin Sbarnea
|
2009-02-27 11:54:59 +00:00
|
|
|
*
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* found in the file COPYING that should have accompanied this file.
|
|
|
|
*
|
|
|
|
* This package 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.
|
2010-06-20 17:38:51 +00:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-02-27 11:54:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "CTCPListenSocket.h"
|
|
|
|
#include "CNetworkAddress.h"
|
|
|
|
#include "CSocketMultiplexer.h"
|
|
|
|
#include "CTCPSocket.h"
|
|
|
|
#include "TSocketMultiplexerMethodJob.h"
|
|
|
|
#include "XSocket.h"
|
|
|
|
#include "XIO.h"
|
|
|
|
#include "CLock.h"
|
|
|
|
#include "CMutex.h"
|
|
|
|
#include "IEventQueue.h"
|
|
|
|
#include "CArch.h"
|
|
|
|
#include "XArch.h"
|
|
|
|
|
|
|
|
//
|
|
|
|
// CTCPListenSocket
|
|
|
|
//
|
|
|
|
|
|
|
|
CTCPListenSocket::CTCPListenSocket()
|
|
|
|
{
|
|
|
|
m_mutex = new CMutex;
|
|
|
|
try {
|
|
|
|
m_socket = ARCH->newSocket(IArchNetwork::kINET, IArchNetwork::kSTREAM);
|
|
|
|
}
|
|
|
|
catch (XArchNetwork& e) {
|
|
|
|
throw XSocketCreate(e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CTCPListenSocket::~CTCPListenSocket()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (m_socket != NULL) {
|
|
|
|
CSocketMultiplexer::getInstance()->removeSocket(this);
|
|
|
|
ARCH->closeSocket(m_socket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
delete m_mutex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CTCPListenSocket::bind(const CNetworkAddress& addr)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
CLock lock(m_mutex);
|
|
|
|
ARCH->setReuseAddrOnSocket(m_socket, true);
|
|
|
|
ARCH->bindSocket(m_socket, addr.getAddress());
|
|
|
|
ARCH->listenOnSocket(m_socket);
|
|
|
|
CSocketMultiplexer::getInstance()->addSocket(this,
|
|
|
|
new TSocketMultiplexerMethodJob<CTCPListenSocket>(
|
|
|
|
this, &CTCPListenSocket::serviceListening,
|
|
|
|
m_socket, true, false));
|
|
|
|
}
|
|
|
|
catch (XArchNetworkAddressInUse& e) {
|
|
|
|
throw XSocketAddressInUse(e.what());
|
|
|
|
}
|
|
|
|
catch (XArchNetwork& e) {
|
|
|
|
throw XSocketBind(e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CTCPListenSocket::close()
|
|
|
|
{
|
|
|
|
CLock lock(m_mutex);
|
|
|
|
if (m_socket == NULL) {
|
|
|
|
throw XIOClosed();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
CSocketMultiplexer::getInstance()->removeSocket(this);
|
|
|
|
ARCH->closeSocket(m_socket);
|
|
|
|
m_socket = NULL;
|
|
|
|
}
|
|
|
|
catch (XArchNetwork& e) {
|
|
|
|
throw XSocketIOClose(e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
CTCPListenSocket::getEventTarget() const
|
|
|
|
{
|
|
|
|
return const_cast<void*>(reinterpret_cast<const void*>(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
IDataSocket*
|
|
|
|
CTCPListenSocket::accept()
|
|
|
|
{
|
2010-05-31 21:30:29 +00:00
|
|
|
IDataSocket* socket = NULL;
|
2009-02-27 11:54:59 +00:00
|
|
|
try {
|
2010-05-31 21:30:29 +00:00
|
|
|
socket = new CTCPSocket(ARCH->acceptSocket(m_socket, NULL));
|
2009-02-27 11:54:59 +00:00
|
|
|
if (socket != NULL) {
|
|
|
|
CSocketMultiplexer::getInstance()->addSocket(this,
|
|
|
|
new TSocketMultiplexerMethodJob<CTCPListenSocket>(
|
|
|
|
this, &CTCPListenSocket::serviceListening,
|
|
|
|
m_socket, true, false));
|
|
|
|
}
|
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
catch (XArchNetwork&) {
|
2010-05-31 21:30:29 +00:00
|
|
|
if (socket != NULL) {
|
|
|
|
delete socket;
|
|
|
|
}
|
2009-02-27 11:54:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-31 21:30:29 +00:00
|
|
|
catch (std::exception &ex) {
|
|
|
|
if (socket != NULL) {
|
|
|
|
delete socket;
|
|
|
|
}
|
|
|
|
throw ex;
|
|
|
|
}
|
2009-02-27 11:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ISocketMultiplexerJob*
|
|
|
|
CTCPListenSocket::serviceListening(ISocketMultiplexerJob* job,
|
|
|
|
bool read, bool, bool error)
|
|
|
|
{
|
|
|
|
if (error) {
|
|
|
|
close();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (read) {
|
|
|
|
EVENTQUEUE->addEvent(CEvent(getConnectingEvent(), this, NULL));
|
|
|
|
// stop polling on this socket until the client accepts
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return job;
|
|
|
|
}
|