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 "CTCPSocket.h"
|
|
|
|
#include "CNetworkAddress.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "XIO.h"
|
|
|
|
#include "XSocket.h"
|
2001-10-06 14:13:28 +00:00
|
|
|
#include "CThread.h"
|
2003-01-04 22:01:32 +00:00
|
|
|
#include "CArch.h"
|
|
|
|
#include "XArch.h"
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// CTCPListenSocket
|
|
|
|
//
|
|
|
|
|
|
|
|
CTCPListenSocket::CTCPListenSocket()
|
|
|
|
{
|
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 {
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->closeSocket(m_socket);
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
ARCH->bindSocket(m_socket, addr.getAddress());
|
|
|
|
ARCH->listenOnSocket(m_socket);
|
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
|
|
|
{
|
2002-06-21 16:19:08 +00:00
|
|
|
// accept asynchronously so we can check for cancellation
|
2003-01-04 22:01:32 +00:00
|
|
|
IArchNetwork::CPollEntry pfds[1];
|
|
|
|
pfds[0].m_socket = m_socket;
|
|
|
|
pfds[0].m_events = IArchNetwork::kPOLLIN;
|
2001-10-06 14:13:28 +00:00
|
|
|
for (;;) {
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->testCancelThread();
|
|
|
|
try {
|
|
|
|
const int status = ARCH->pollSocket(pfds, 1, 0.01);
|
|
|
|
if (status > 0 &&
|
|
|
|
(pfds[0].m_revents & IArchNetwork::kPOLLIN) != 0) {
|
|
|
|
return new CTCPSocket(ARCH->acceptSocket(m_socket, NULL));
|
2001-11-19 00:33:36 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
2003-01-04 22:01:32 +00:00
|
|
|
catch (XArchNetwork&) {
|
|
|
|
// ignore and retry
|
|
|
|
}
|
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
|
|
|
{
|
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 {
|
|
|
|
ARCH->closeSocket(m_socket);
|
|
|
|
m_socket = NULL;
|
|
|
|
}
|
|
|
|
catch (XArchNetwork& e) {
|
|
|
|
throw XSocketIOClose(e.what());
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
}
|