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 "CTCPSocket.h"
|
|
|
|
#include "CBufferedInputStream.h"
|
|
|
|
#include "CBufferedOutputStream.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 "CLock.h"
|
|
|
|
#include "CMutex.h"
|
|
|
|
#include "CThread.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "TMethodJob.h"
|
2003-01-04 22:01:32 +00:00
|
|
|
#include "CArch.h"
|
|
|
|
#include "XArch.h"
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// CTCPSocket
|
|
|
|
//
|
|
|
|
|
2001-10-14 16:58:01 +00:00
|
|
|
CTCPSocket::CTCPSocket()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
CTCPSocket::CTCPSocket(CArchSocket socket) :
|
|
|
|
m_socket(socket)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
assert(m_socket != NULL);
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
init();
|
|
|
|
|
|
|
|
// socket starts in connected state
|
|
|
|
m_connected = kReadWrite;
|
|
|
|
|
|
|
|
// start handling socket
|
|
|
|
m_thread = new CThread(new TMethodJob<CTCPSocket>(
|
2001-10-21 00:21:02 +00:00
|
|
|
this, &CTCPSocket::ioThread));
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CTCPSocket::~CTCPSocket()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
catch (...) {
|
2003-01-04 22:01:32 +00:00
|
|
|
// ignore
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
delete m_input;
|
|
|
|
delete m_output;
|
2001-10-21 00:21:02 +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
|
|
|
CTCPSocket::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());
|
|
|
|
}
|
|
|
|
catch (XArchNetworkAddressInUse& e) {
|
|
|
|
throw XSocketAddressInUse(e.what());
|
|
|
|
}
|
|
|
|
catch (XArchNetwork& e) {
|
|
|
|
throw XSocketBind(e.what());
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CTCPSocket::close()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2002-06-02 22:57:50 +00:00
|
|
|
// see if buffers should be flushed
|
|
|
|
bool doFlush = false;
|
|
|
|
{
|
|
|
|
CLock lock(m_mutex);
|
|
|
|
doFlush = (m_thread != NULL && (m_connected & kWrite) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// flush buffers
|
|
|
|
if (doFlush) {
|
|
|
|
m_output->flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
// cause ioThread to exit
|
2003-01-04 22:01:32 +00:00
|
|
|
if (m_socket != NULL) {
|
2002-06-02 22:57:50 +00:00
|
|
|
CLock lock(m_mutex);
|
2003-01-04 22:01:32 +00:00
|
|
|
try {
|
|
|
|
ARCH->closeSocketForRead(m_socket);
|
|
|
|
}
|
|
|
|
catch (XArchNetwork&) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
ARCH->closeSocketForWrite(m_socket);
|
|
|
|
}
|
|
|
|
catch (XArchNetwork&) {
|
|
|
|
// ignore
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
2003-01-04 22:01:32 +00:00
|
|
|
m_connected = kClosed;
|
2002-06-02 22:57:50 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2002-06-02 22:57:50 +00:00
|
|
|
// wait for thread
|
|
|
|
if (m_thread != NULL) {
|
2001-10-06 14:13:28 +00:00
|
|
|
m_thread->wait();
|
|
|
|
delete m_thread;
|
|
|
|
m_thread = NULL;
|
|
|
|
}
|
|
|
|
|
2002-06-02 22:57:50 +00:00
|
|
|
// close socket
|
2003-01-04 22:01:32 +00:00
|
|
|
if (m_socket != NULL) {
|
|
|
|
try {
|
|
|
|
ARCH->closeSocket(m_socket);
|
|
|
|
m_socket = NULL;
|
|
|
|
}
|
|
|
|
catch (XArchNetwork& e) {
|
|
|
|
throw XSocketIOClose(e.what());
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-17 12:02:26 +00:00
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
CTCPSocket::connect(const CNetworkAddress& addr)
|
2002-06-17 12:02:26 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
do {
|
|
|
|
// connect asynchronously so we can check for cancellation.
|
|
|
|
// we can't wrap setting and resetting the blocking flag in
|
|
|
|
// the c'tor/d'tor of a class (to make resetting automatic)
|
|
|
|
// because setBlockingOnSocket() can throw and it might be
|
|
|
|
// called while unwinding the stack due to a throw.
|
|
|
|
try {
|
|
|
|
ARCH->setBlockingOnSocket(m_socket, false);
|
|
|
|
ARCH->connectSocket(m_socket, addr.getAddress());
|
|
|
|
ARCH->setBlockingOnSocket(m_socket, true);
|
|
|
|
|
|
|
|
// connected
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
catch (XArchNetworkConnecting&) {
|
|
|
|
// connection is in progress
|
|
|
|
ARCH->setBlockingOnSocket(m_socket, true);
|
|
|
|
}
|
|
|
|
catch (XArchNetwork& e) {
|
|
|
|
ARCH->setBlockingOnSocket(m_socket, true);
|
|
|
|
throw XSocketConnect(e.what());
|
2002-06-21 16:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// wait for connection or failure
|
2003-01-04 22:01:32 +00:00
|
|
|
IArchNetwork::CPollEntry pfds[1];
|
|
|
|
pfds[0].m_socket = m_socket;
|
|
|
|
pfds[0].m_events = IArchNetwork::kPOLLOUT;
|
2002-06-21 16:19:08 +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) {
|
|
|
|
if ((pfds[0].m_revents & (IArchNetwork::kPOLLERR |
|
|
|
|
IArchNetwork::kPOLLNVAL)) != 0) {
|
2002-06-21 16:19:08 +00:00
|
|
|
// connection failed
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->throwErrorOnSocket(m_socket);
|
2002-06-21 16:19:08 +00:00
|
|
|
}
|
2003-01-04 22:01:32 +00:00
|
|
|
if ((pfds[0].m_revents & IArchNetwork::kPOLLOUT) != 0) {
|
|
|
|
// connection may have failed or succeeded
|
|
|
|
ARCH->throwErrorOnSocket(m_socket);
|
2002-06-21 16:19:08 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
// connected!
|
|
|
|
break;
|
|
|
|
}
|
2002-06-21 16:19:08 +00:00
|
|
|
}
|
|
|
|
}
|
2003-01-04 22:01:32 +00:00
|
|
|
catch (XArchNetwork& e) {
|
|
|
|
throw XSocketConnect(e.what());
|
|
|
|
}
|
2002-06-21 16:19:08 +00:00
|
|
|
}
|
2003-01-04 22:01:32 +00:00
|
|
|
} while (false);
|
2002-06-21 16:19:08 +00:00
|
|
|
|
2002-06-17 12:02:26 +00:00
|
|
|
// start servicing the socket
|
|
|
|
m_connected = kReadWrite;
|
|
|
|
m_thread = new CThread(new TMethodJob<CTCPSocket>(
|
|
|
|
this, &CTCPSocket::ioThread));
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
IInputStream*
|
|
|
|
CTCPSocket::getInputStream()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
return m_input;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
IOutputStream*
|
|
|
|
CTCPSocket::getOutputStream()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
return m_output;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CTCPSocket::init()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
m_mutex = new CMutex;
|
|
|
|
m_thread = NULL;
|
|
|
|
m_connected = kClosed;
|
|
|
|
m_input = new CBufferedInputStream(m_mutex,
|
|
|
|
new TMethodJob<CTCPSocket>(
|
|
|
|
this, &CTCPSocket::closeInput));
|
|
|
|
m_output = new CBufferedOutputStream(m_mutex,
|
|
|
|
new TMethodJob<CTCPSocket>(
|
|
|
|
this, &CTCPSocket::closeOutput));
|
2002-05-01 15:31:47 +00:00
|
|
|
|
|
|
|
// turn off Nagle algorithm. we send lots of very short messages
|
|
|
|
// that should be sent without (much) delay. for example, the
|
|
|
|
// mouse motion messages are much less useful if they're delayed.
|
2003-01-04 22:01:32 +00:00
|
|
|
// FIXME -- the client should do this
|
|
|
|
try {
|
|
|
|
ARCH->setNoDelayOnSocket(m_socket, true);
|
|
|
|
}
|
|
|
|
catch (XArchNetwork& e) {
|
|
|
|
try {
|
|
|
|
ARCH->closeSocket(m_socket);
|
|
|
|
m_socket = NULL;
|
|
|
|
}
|
|
|
|
catch (XArchNetwork&) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
throw XSocketCreate(e.what());
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CTCPSocket::ioThread(void*)
|
2001-10-21 00:21:02 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
ioService();
|
2002-05-30 16:11:59 +00:00
|
|
|
ioCleanup();
|
2001-10-21 00:21:02 +00:00
|
|
|
}
|
|
|
|
catch (...) {
|
2002-05-30 16:11:59 +00:00
|
|
|
ioCleanup();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CTCPSocket::ioCleanup()
|
2002-05-30 16:11:59 +00:00
|
|
|
{
|
|
|
|
try {
|
2001-10-21 00:21:02 +00:00
|
|
|
m_input->close();
|
2002-05-30 16:11:59 +00:00
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
try {
|
2001-10-21 00:21:02 +00:00
|
|
|
m_output->close();
|
2002-05-30 16:11:59 +00:00
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// ignore
|
2001-10-21 00:21:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CTCPSocket::ioService()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
assert(m_socket != NULL);
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
// now service the connection
|
2003-01-04 22:01:32 +00:00
|
|
|
IArchNetwork::CPollEntry pfds[1];
|
|
|
|
pfds[0].m_socket = m_socket;
|
2001-10-06 14:13:28 +00:00
|
|
|
for (;;) {
|
|
|
|
{
|
|
|
|
// choose events to poll for
|
|
|
|
CLock lock(m_mutex);
|
2003-01-04 22:01:32 +00:00
|
|
|
pfds[0].m_events = 0;
|
2002-05-30 16:11:59 +00:00
|
|
|
if (m_connected == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
if ((m_connected & kRead) != 0) {
|
|
|
|
// still open for reading
|
2003-01-04 22:01:32 +00:00
|
|
|
pfds[0].m_events |= IArchNetwork::kPOLLIN;
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
if ((m_connected & kWrite) != 0 && m_output->getSize() > 0) {
|
|
|
|
// data queued for writing
|
2003-01-04 22:01:32 +00:00
|
|
|
pfds[0].m_events |= IArchNetwork::kPOLLOUT;
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
try {
|
|
|
|
// check for status
|
|
|
|
const int status = ARCH->pollSocket(pfds, 1, 0.01);
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
// transfer data and handle errors
|
|
|
|
if (status == 1) {
|
|
|
|
if ((pfds[0].m_revents & (IArchNetwork::kPOLLERR |
|
|
|
|
IArchNetwork::kPOLLNVAL)) != 0) {
|
|
|
|
// stream is no good anymore so bail
|
2001-10-06 14:13:28 +00:00
|
|
|
CLock lock(m_mutex);
|
|
|
|
m_input->hangup();
|
2003-01-04 22:01:32 +00:00
|
|
|
return;
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
2003-01-04 22:01:32 +00:00
|
|
|
|
|
|
|
// read some data
|
|
|
|
if (pfds[0].m_revents & IArchNetwork::kPOLLIN) {
|
|
|
|
UInt8 buffer[4096];
|
|
|
|
size_t n = ARCH->readSocket(m_socket,
|
|
|
|
buffer, sizeof(buffer));
|
|
|
|
CLock lock(m_mutex);
|
|
|
|
if (n > 0) {
|
|
|
|
m_input->write(buffer, n);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// stream hungup
|
|
|
|
m_input->hangup();
|
|
|
|
m_connected &= ~kRead;
|
2002-10-30 22:16:30 +00:00
|
|
|
}
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
// write some data
|
|
|
|
if (pfds[0].m_revents & IArchNetwork::kPOLLOUT) {
|
|
|
|
CLock lock(m_mutex);
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
// get amount of data to write
|
|
|
|
UInt32 n = m_output->getSize();
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
// write data
|
|
|
|
const void* buffer = m_output->peek(n);
|
|
|
|
size_t n2 = ARCH->writeSocket(m_socket, buffer, n);
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
// discard written data
|
|
|
|
if (n2 > 0) {
|
|
|
|
m_output->pop(n2);
|
2002-10-30 22:16:30 +00:00
|
|
|
}
|
2001-10-21 00:21:02 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
2003-01-04 22:01:32 +00:00
|
|
|
catch (XArchNetwork&) {
|
|
|
|
// socket has failed
|
|
|
|
return;
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CTCPSocket::closeInput(void*)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
// note -- m_mutex should already be locked
|
2003-01-04 22:01:32 +00:00
|
|
|
try {
|
|
|
|
ARCH->closeSocketForRead(m_socket);
|
|
|
|
m_connected &= ~kRead;
|
|
|
|
}
|
|
|
|
catch (XArchNetwork&) {
|
|
|
|
// ignore
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CTCPSocket::closeOutput(void*)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
// note -- m_mutex should already be locked
|
2003-01-04 22:01:32 +00:00
|
|
|
try {
|
|
|
|
ARCH->closeSocketForWrite(m_socket);
|
|
|
|
m_connected &= ~kWrite;
|
|
|
|
}
|
|
|
|
catch (XArchNetwork&) {
|
|
|
|
// ignore
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|