2002-08-02 19:57:46 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
* Copyright (C) 2002 Chris Schoeneman
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2001-10-06 14:13:28 +00:00
|
|
|
#include "CTCPListenSocket.h"
|
|
|
|
#include "CNetworkAddress.h"
|
2004-01-24 16:09:25 +00:00
|
|
|
#include "CSocketMultiplexer.h"
|
|
|
|
#include "CTCPSocket.h"
|
|
|
|
#include "TSocketMultiplexerMethodJob.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "XSocket.h"
|
2004-01-24 16:09:25 +00:00
|
|
|
#include "XIO.h"
|
|
|
|
#include "CEvent.h"
|
|
|
|
#include "CEventQueue.h"
|
|
|
|
#include "CLock.h"
|
|
|
|
#include "CMutex.h"
|
2003-01-04 22:01:32 +00:00
|
|
|
#include "CArch.h"
|
|
|
|
#include "XArch.h"
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// CTCPListenSocket
|
|
|
|
//
|
|
|
|
|
2004-01-24 16:09:25 +00:00
|
|
|
CTCPListenSocket::CTCPListenSocket() :
|
|
|
|
m_target(NULL)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2004-01-24 16:09:25 +00:00
|
|
|
m_mutex = new CMutex;
|
2003-01-04 22:01:32 +00:00
|
|
|
try {
|
|
|
|
m_socket = ARCH->newSocket(IArchNetwork::kINET, IArchNetwork::kSTREAM);
|
|
|
|
}
|
|
|
|
catch (XArchNetwork& e) {
|
|
|
|
throw XSocketCreate(e.what());
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CTCPListenSocket::~CTCPListenSocket()
|
|
|
|
{
|
|
|
|
try {
|
2004-01-24 16:09:25 +00:00
|
|
|
if (m_socket != NULL) {
|
|
|
|
CSocketMultiplexer::getInstance()->removeSocket(this);
|
|
|
|
ARCH->closeSocket(m_socket);
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// ignore
|
|
|
|
}
|
2004-01-24 16:09:25 +00:00
|
|
|
delete m_mutex;
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
CTCPListenSocket::bind(const CNetworkAddress& addr)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
try {
|
2004-01-24 16:09:25 +00:00
|
|
|
CLock lock(m_mutex);
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->bindSocket(m_socket, addr.getAddress());
|
|
|
|
ARCH->listenOnSocket(m_socket);
|
2004-01-24 16:09:25 +00:00
|
|
|
CSocketMultiplexer::getInstance()->addSocket(this,
|
|
|
|
new TSocketMultiplexerMethodJob<CTCPListenSocket>(
|
|
|
|
this, &CTCPListenSocket::serviceListening,
|
|
|
|
m_socket, true, false));
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
2003-01-04 22:01:32 +00:00
|
|
|
catch (XArchNetworkAddressInUse& e) {
|
|
|
|
throw XSocketAddressInUse(e.what());
|
|
|
|
}
|
|
|
|
catch (XArchNetwork& e) {
|
|
|
|
throw XSocketBind(e.what());
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-17 12:02:26 +00:00
|
|
|
IDataSocket*
|
2002-06-10 22:06:45 +00:00
|
|
|
CTCPListenSocket::accept()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2004-01-24 16:09:25 +00:00
|
|
|
try {
|
|
|
|
CSocketMultiplexer::getInstance()->addSocket(this,
|
|
|
|
new TSocketMultiplexerMethodJob<CTCPListenSocket>(
|
|
|
|
this, &CTCPListenSocket::serviceListening,
|
|
|
|
m_socket, true, false));
|
|
|
|
return new CTCPSocket(ARCH->acceptSocket(m_socket, NULL));
|
|
|
|
}
|
|
|
|
catch (XArchNetwork&) {
|
|
|
|
return NULL;
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CTCPListenSocket::close()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2004-01-24 16:09:25 +00:00
|
|
|
CLock lock(m_mutex);
|
2003-01-04 22:01:32 +00:00
|
|
|
if (m_socket == NULL) {
|
2001-10-06 14:13:28 +00:00
|
|
|
throw XIOClosed();
|
|
|
|
}
|
2003-01-04 22:01:32 +00:00
|
|
|
try {
|
2004-01-24 16:09:25 +00:00
|
|
|
CSocketMultiplexer::getInstance()->removeSocket(this);
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->closeSocket(m_socket);
|
|
|
|
m_socket = NULL;
|
|
|
|
}
|
|
|
|
catch (XArchNetwork& e) {
|
|
|
|
throw XSocketIOClose(e.what());
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
2004-01-24 16:09:25 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
CTCPListenSocket::setEventTarget(void* target)
|
|
|
|
{
|
|
|
|
CLock lock(m_mutex);
|
|
|
|
m_target = target;
|
|
|
|
}
|
|
|
|
|
|
|
|
ISocketMultiplexerJob*
|
|
|
|
CTCPListenSocket::serviceListening(ISocketMultiplexerJob* job,
|
|
|
|
bool read, bool, bool error)
|
|
|
|
{
|
|
|
|
if (error) {
|
|
|
|
close();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (read) {
|
|
|
|
CEventQueue::getInstance()->addEvent(
|
|
|
|
CEvent(getConnectingEvent(), m_target, NULL));
|
|
|
|
// stop polling on this socket until the client accepts
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return job;
|
|
|
|
}
|