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"
|
2004-01-24 16:09:25 +00:00
|
|
|
#include "CNetworkAddress.h"
|
|
|
|
#include "CSocketMultiplexer.h"
|
|
|
|
#include "TSocketMultiplexerMethodJob.h"
|
2001-10-06 14:13:28 +00:00
|
|
|
#include "CBufferedInputStream.h"
|
|
|
|
#include "CBufferedOutputStream.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "XSocket.h"
|
2004-01-24 16:09:25 +00:00
|
|
|
#include "XIO.h"
|
2001-10-06 14:13:28 +00:00
|
|
|
#include "CLock.h"
|
|
|
|
#include "CMutex.h"
|
2004-01-24 16:09:25 +00:00
|
|
|
#include "CEventQueue.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
|
|
|
|
|
|
|
// socket starts in connected state
|
2004-01-24 16:09:25 +00:00
|
|
|
init();
|
|
|
|
setState(kReadWrite, true);
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CTCPSocket::~CTCPSocket()
|
|
|
|
{
|
|
|
|
try {
|
2004-01-24 16:09:25 +00:00
|
|
|
if (m_socket != NULL) {
|
|
|
|
close();
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
// flush buffers
|
2004-01-24 16:09:25 +00:00
|
|
|
m_output->flush();
|
2002-06-02 22:57:50 +00:00
|
|
|
|
2004-01-24 16:09:25 +00:00
|
|
|
// now closed
|
|
|
|
setState(kClosed, true);
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2004-01-24 16:09:25 +00:00
|
|
|
// close buffers
|
|
|
|
try {
|
|
|
|
m_input->close();
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
m_output->close();
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// ignore
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-02 22:57:50 +00:00
|
|
|
// close socket
|
2004-01-24 16:09:25 +00:00
|
|
|
CLock lock(m_mutex);
|
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
|
2004-01-24 16:09:25 +00:00
|
|
|
CTCPSocket::setEventTarget(void* target)
|
2002-06-17 12:02:26 +00:00
|
|
|
{
|
2004-01-24 16:09:25 +00:00
|
|
|
CLock lock(m_mutex);
|
|
|
|
m_target = target;
|
|
|
|
}
|
2002-06-21 16:19:08 +00:00
|
|
|
|
2004-01-24 16:09:25 +00:00
|
|
|
void
|
|
|
|
CTCPSocket::connect(const CNetworkAddress& addr)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
// FIXME -- don't throw if in progress, just return that info
|
|
|
|
ARCH->connectSocket(m_socket, addr.getAddress());
|
|
|
|
setState(kReadWrite, true);
|
|
|
|
}
|
|
|
|
catch (XArchNetworkConnecting&) {
|
|
|
|
// connection is in progress
|
|
|
|
setState(kConnecting, true);
|
|
|
|
}
|
|
|
|
catch (XArchNetwork& e) {
|
|
|
|
throw XSocketConnect(e.what());
|
|
|
|
}
|
2002-06-17 12:02:26 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2004-01-24 16:09:25 +00:00
|
|
|
m_mutex = new CMutex;
|
|
|
|
m_input = new CBufferedInputStream(m_mutex,
|
|
|
|
new TMethodJob<CTCPSocket>(
|
|
|
|
this, &CTCPSocket::emptyInput),
|
2001-10-06 14:13:28 +00:00
|
|
|
new TMethodJob<CTCPSocket>(
|
|
|
|
this, &CTCPSocket::closeInput));
|
2004-01-24 16:09:25 +00:00
|
|
|
m_output = new CBufferedOutputStream(m_mutex,
|
|
|
|
new TMethodJob<CTCPSocket>(
|
|
|
|
this, &CTCPSocket::fillOutput),
|
2001-10-06 14:13:28 +00:00
|
|
|
new TMethodJob<CTCPSocket>(
|
|
|
|
this, &CTCPSocket::closeOutput));
|
2004-01-24 16:09:25 +00:00
|
|
|
m_state = kUnconnected;
|
|
|
|
m_target = NULL;
|
|
|
|
m_job = NULL;
|
|
|
|
|
|
|
|
// make socket non-blocking
|
|
|
|
// FIXME -- check for error
|
|
|
|
ARCH->setBlockingOnSocket(m_socket, false);
|
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
|
|
|
}
|
|
|
|
|
2004-01-24 16:09:25 +00:00
|
|
|
ISocketMultiplexerJob*
|
|
|
|
CTCPSocket::newMultiplexerJob(JobFunc func, bool readable, bool writable)
|
2001-10-21 00:21:02 +00:00
|
|
|
{
|
2004-01-24 16:09:25 +00:00
|
|
|
return new TSocketMultiplexerMethodJob<CTCPSocket>(
|
|
|
|
this, func, m_socket, readable, writable);
|
2002-05-30 16:11:59 +00:00
|
|
|
}
|
|
|
|
|
2004-01-24 16:09:25 +00:00
|
|
|
ISocketMultiplexerJob*
|
|
|
|
CTCPSocket::setState(State state, bool setJob)
|
2002-05-30 16:11:59 +00:00
|
|
|
{
|
2004-01-24 16:09:25 +00:00
|
|
|
if (m_state == state || m_state == kClosed) {
|
|
|
|
return m_job;
|
2002-05-30 16:11:59 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2004-01-24 16:09:25 +00:00
|
|
|
State oldState = m_state;
|
|
|
|
m_state = state;
|
|
|
|
|
|
|
|
bool read = (m_input->getSize() > 0);
|
|
|
|
bool write = (m_output->getSize() > 0);
|
|
|
|
CEvent::Type eventType = 0;
|
|
|
|
m_job = NULL;
|
|
|
|
switch (m_state) {
|
|
|
|
case kUnconnected:
|
|
|
|
assert(0 && "cannot re-enter unconnected state");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kConnecting:
|
|
|
|
m_job = newMultiplexerJob(&CTCPSocket::serviceConnecting, false, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kReadWrite:
|
|
|
|
if (oldState == kConnecting) {
|
|
|
|
eventType = IDataSocket::getConnectedEvent();
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
2004-01-24 16:09:25 +00:00
|
|
|
m_job = newMultiplexerJob(&CTCPSocket::serviceConnected, true, write);
|
|
|
|
break;
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2004-01-24 16:09:25 +00:00
|
|
|
case kReadOnly:
|
|
|
|
if (!write) {
|
|
|
|
eventType = IDataSocket::getShutdownOutputEvent();
|
|
|
|
}
|
|
|
|
if (oldState == kWriteOnly) {
|
|
|
|
goto shutdown;
|
|
|
|
}
|
|
|
|
m_job = newMultiplexerJob(&CTCPSocket::serviceConnected, true, write);
|
|
|
|
break;
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2004-01-24 16:09:25 +00:00
|
|
|
case kWriteOnly:
|
|
|
|
if (!read) {
|
|
|
|
m_input->hangup();
|
|
|
|
eventType = IDataSocket::getShutdownInputEvent();
|
|
|
|
}
|
|
|
|
if (oldState == kReadOnly) {
|
|
|
|
goto shutdown;
|
|
|
|
}
|
|
|
|
m_job = newMultiplexerJob(&CTCPSocket::serviceConnected, false, write);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kShutdown:
|
|
|
|
shutdown:
|
|
|
|
if (!read && !write) {
|
|
|
|
eventType = ISocket::getDisconnectedEvent();
|
|
|
|
m_state = kClosed;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_state = kShutdown;
|
|
|
|
}
|
|
|
|
break;
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2004-01-24 16:09:25 +00:00
|
|
|
case kClosed:
|
|
|
|
m_input->hangup();
|
|
|
|
if (oldState == kConnecting) {
|
|
|
|
eventType = IDataSocket::getConnectionFailedEvent();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
eventType = ISocket::getDisconnectedEvent();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2004-01-24 16:09:25 +00:00
|
|
|
// notify
|
|
|
|
if (eventType != 0) {
|
|
|
|
sendEvent(eventType);
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2004-01-24 16:09:25 +00:00
|
|
|
// cut over to new job. multiplexer will delete the old job.
|
|
|
|
if (setJob) {
|
|
|
|
if (m_job == NULL) {
|
|
|
|
CSocketMultiplexer::getInstance()->removeSocket(this);
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
2004-01-24 16:09:25 +00:00
|
|
|
else {
|
|
|
|
CSocketMultiplexer::getInstance()->addSocket(this, m_job);
|
2003-01-04 22:01:32 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
2004-01-24 16:09:25 +00:00
|
|
|
return m_job;
|
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);
|
2004-01-24 16:09:25 +00:00
|
|
|
setState(kWriteOnly, true);
|
2003-01-04 22:01:32 +00:00
|
|
|
}
|
|
|
|
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 {
|
2004-01-24 16:09:25 +00:00
|
|
|
// ARCH->closeSocketForWrite(m_socket);
|
|
|
|
setState(kReadOnly, true);
|
2003-01-04 22:01:32 +00:00
|
|
|
}
|
|
|
|
catch (XArchNetwork&) {
|
|
|
|
// ignore
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
2004-01-24 16:09:25 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
CTCPSocket::emptyInput(void*)
|
|
|
|
{
|
|
|
|
// note -- m_mutex should already be locked
|
|
|
|
bool write = (m_output->getSize() > 0);
|
|
|
|
if (m_state == kWriteOnly && !write) {
|
|
|
|
m_state = kShutdown;
|
|
|
|
}
|
|
|
|
if (m_state == kWriteOnly) {
|
|
|
|
m_job = newMultiplexerJob(&CTCPSocket::serviceConnected, false, write);
|
|
|
|
CSocketMultiplexer::getInstance()->addSocket(this, m_job);
|
|
|
|
m_input->hangup();
|
|
|
|
sendEvent(IDataSocket::getShutdownInputEvent());
|
|
|
|
}
|
|
|
|
else if (m_state == kShutdown) {
|
|
|
|
m_job = NULL;
|
|
|
|
CSocketMultiplexer::getInstance()->removeSocket(this);
|
|
|
|
if (!write) {
|
|
|
|
sendEvent(ISocket::getDisconnectedEvent());
|
|
|
|
m_state = kClosed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CTCPSocket::fillOutput(void*)
|
|
|
|
{
|
|
|
|
// note -- m_mutex should already be locked
|
|
|
|
if (m_state == kReadWrite) {
|
|
|
|
m_job = newMultiplexerJob(&CTCPSocket::serviceConnected, true, true);
|
|
|
|
CSocketMultiplexer::getInstance()->addSocket(this, m_job);
|
|
|
|
}
|
|
|
|
else if (m_state == kWriteOnly) {
|
|
|
|
m_job = newMultiplexerJob(&CTCPSocket::serviceConnected, false, true);
|
|
|
|
CSocketMultiplexer::getInstance()->addSocket(this, m_job);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ISocketMultiplexerJob*
|
|
|
|
CTCPSocket::serviceConnecting(ISocketMultiplexerJob* job,
|
|
|
|
bool, bool write, bool error)
|
|
|
|
{
|
|
|
|
CLock lock(m_mutex);
|
|
|
|
|
|
|
|
if (write && !error) {
|
|
|
|
try {
|
|
|
|
// connection may have failed or succeeded
|
|
|
|
ARCH->throwErrorOnSocket(m_socket);
|
|
|
|
}
|
|
|
|
catch (XArchNetwork&) {
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
return setState(kClosed, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write) {
|
|
|
|
return setState(kReadWrite, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return job;
|
|
|
|
}
|
|
|
|
|
|
|
|
ISocketMultiplexerJob*
|
|
|
|
CTCPSocket::serviceConnected(ISocketMultiplexerJob* job,
|
|
|
|
bool read, bool write, bool error)
|
|
|
|
{
|
|
|
|
CLock lock(m_mutex);
|
|
|
|
if (error) {
|
|
|
|
return setState(kClosed, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write) {
|
|
|
|
// get amount of data to write
|
|
|
|
UInt32 n = m_output->getSize();
|
|
|
|
|
|
|
|
// write data
|
|
|
|
try {
|
|
|
|
const void* buffer = m_output->peek(n);
|
|
|
|
size_t n2 = ARCH->writeSocket(m_socket, buffer, n);
|
|
|
|
|
|
|
|
// discard written data
|
|
|
|
if (n2 > 0) {
|
|
|
|
m_output->pop(n2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (XArchNetworkDisconnected&) {
|
|
|
|
// stream hungup
|
|
|
|
return setState(kReadOnly, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (read) {
|
|
|
|
UInt8 buffer[4096];
|
|
|
|
size_t n = ARCH->readSocket(m_socket, buffer, sizeof(buffer));
|
|
|
|
if (n > 0) {
|
|
|
|
// slurp up as much as possible
|
|
|
|
do {
|
|
|
|
m_input->write(buffer, n);
|
|
|
|
try {
|
|
|
|
n = ARCH->readSocket(m_socket, buffer, sizeof(buffer));
|
|
|
|
}
|
|
|
|
catch (XArchNetworkWouldBlock&) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (n > 0);
|
|
|
|
|
|
|
|
// notify
|
|
|
|
sendEvent(IDataSocket::getInputEvent());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// stream hungup
|
|
|
|
return setState(kWriteOnly, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write && m_output->getSize() == 0) {
|
|
|
|
if (m_state == kReadOnly) {
|
|
|
|
ARCH->closeSocketForWrite(m_socket);
|
|
|
|
sendEvent(IDataSocket::getShutdownOutputEvent());
|
|
|
|
m_job = newMultiplexerJob(&CTCPSocket::serviceConnected,
|
|
|
|
true, false);
|
|
|
|
job = m_job;
|
|
|
|
}
|
|
|
|
else if (m_state == kReadWrite || m_state == kReadOnly) {
|
|
|
|
m_job = newMultiplexerJob(&CTCPSocket::serviceConnected,
|
|
|
|
true, false);
|
|
|
|
job = m_job;
|
|
|
|
}
|
|
|
|
else if (m_state == kWriteOnly) {
|
|
|
|
m_job = NULL;
|
|
|
|
job = m_job;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return job;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CTCPSocket::sendEvent(CEvent::Type type)
|
|
|
|
{
|
|
|
|
CEventQueue::getInstance()->addEvent(CEvent(type, m_target, NULL));
|
|
|
|
}
|