barrier/lib/net/CTCPSocket.cpp

467 lines
9.4 KiB
C++
Raw Normal View History

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 "CNetworkAddress.h"
#include "CSocketMultiplexer.h"
#include "TSocketMultiplexerMethodJob.h"
2001-10-06 14:13:28 +00:00
#include "CBufferedInputStream.h"
#include "CBufferedOutputStream.h"
#include "XSocket.h"
#include "XIO.h"
2001-10-06 14:13:28 +00:00
#include "CLock.h"
#include "CMutex.h"
#include "CEventQueue.h"
#include "TMethodJob.h"
#include "CArch.h"
#include "XArch.h"
2001-10-06 14:13:28 +00:00
//
// CTCPSocket
//
CTCPSocket::CTCPSocket()
2001-10-06 14:13:28 +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();
}
CTCPSocket::CTCPSocket(CArchSocket socket) :
m_socket(socket)
2001-10-06 14:13:28 +00:00
{
assert(m_socket != NULL);
2001-10-06 14:13:28 +00:00
// socket starts in connected state
init();
setState(kReadWrite, true);
2001-10-06 14:13:28 +00:00
}
CTCPSocket::~CTCPSocket()
{
try {
if (m_socket != NULL) {
close();
}
2001-10-06 14:13:28 +00:00
}
catch (...) {
// 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
}
void
2002-06-17 13:31:21 +00:00
CTCPSocket::bind(const CNetworkAddress& addr)
2001-10-06 14:13:28 +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
}
}
void
CTCPSocket::close()
2001-10-06 14:13:28 +00:00
{
// flush buffers
m_output->flush();
// now closed
setState(kClosed, true);
2001-10-06 14:13:28 +00:00
// close buffers
try {
m_input->close();
}
catch (...) {
// ignore
}
try {
m_output->close();
}
catch (...) {
// ignore
2001-10-06 14:13:28 +00:00
}
// close socket
CLock lock(m_mutex);
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
}
}
}
void
CTCPSocket::setEventTarget(void* target)
{
CLock lock(m_mutex);
m_target = target;
}
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());
}
}
IInputStream*
CTCPSocket::getInputStream()
2001-10-06 14:13:28 +00:00
{
return m_input;
}
IOutputStream*
CTCPSocket::getOutputStream()
2001-10-06 14:13:28 +00:00
{
return m_output;
}
void
CTCPSocket::init()
2001-10-06 14:13:28 +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));
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));
m_state = kUnconnected;
m_target = NULL;
m_job = NULL;
// make socket non-blocking
// FIXME -- check for error
ARCH->setBlockingOnSocket(m_socket, false);
// 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.
// 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
}
ISocketMultiplexerJob*
CTCPSocket::newMultiplexerJob(JobFunc func, bool readable, bool writable)
2001-10-21 00:21:02 +00:00
{
return new TSocketMultiplexerMethodJob<CTCPSocket>(
this, func, m_socket, readable, writable);
2002-05-30 16:11:59 +00:00
}
ISocketMultiplexerJob*
CTCPSocket::setState(State state, bool setJob)
2002-05-30 16:11:59 +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
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
}
m_job = newMultiplexerJob(&CTCPSocket::serviceConnected, true, write);
break;
2001-10-06 14:13:28 +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
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
case kClosed:
m_input->hangup();
if (oldState == kConnecting) {
eventType = IDataSocket::getConnectionFailedEvent();
}
else {
eventType = ISocket::getDisconnectedEvent();
}
break;
}
2001-10-06 14:13:28 +00:00
// notify
if (eventType != 0) {
sendEvent(eventType);
}
2001-10-06 14:13:28 +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
}
else {
CSocketMultiplexer::getInstance()->addSocket(this, m_job);
}
2001-10-06 14:13:28 +00:00
}
return m_job;
2001-10-06 14:13:28 +00:00
}
void
CTCPSocket::closeInput(void*)
2001-10-06 14:13:28 +00:00
{
// note -- m_mutex should already be locked
try {
ARCH->closeSocketForRead(m_socket);
setState(kWriteOnly, true);
}
catch (XArchNetwork&) {
// ignore
}
2001-10-06 14:13:28 +00:00
}
void
CTCPSocket::closeOutput(void*)
2001-10-06 14:13:28 +00:00
{
// note -- m_mutex should already be locked
try {
// ARCH->closeSocketForWrite(m_socket);
setState(kReadOnly, true);
}
catch (XArchNetwork&) {
// ignore
}
2001-10-06 14:13:28 +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));
}