2002-08-02 23:57:46 +04: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 18:13:28 +04:00
|
|
|
#include "CTCPSocket.h"
|
2004-01-24 19:09:25 +03:00
|
|
|
#include "CNetworkAddress.h"
|
|
|
|
#include "CSocketMultiplexer.h"
|
|
|
|
#include "TSocketMultiplexerMethodJob.h"
|
2002-06-11 02:06:45 +04:00
|
|
|
#include "XSocket.h"
|
2001-10-06 18:13:28 +04:00
|
|
|
#include "CLock.h"
|
2004-01-24 19:09:25 +03:00
|
|
|
#include "CEventQueue.h"
|
2004-02-02 00:09:22 +03:00
|
|
|
#include "IEventJob.h"
|
2003-01-05 01:01:32 +03:00
|
|
|
#include "CArch.h"
|
|
|
|
#include "XArch.h"
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// CTCPSocket
|
|
|
|
//
|
|
|
|
|
2004-02-02 00:09:22 +03:00
|
|
|
CTCPSocket::CTCPSocket() :
|
|
|
|
m_mutex(),
|
|
|
|
m_flushed(&m_mutex, true),
|
|
|
|
m_eventFilter(NULL)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2003-01-05 01:01:32 +03:00
|
|
|
try {
|
|
|
|
m_socket = ARCH->newSocket(IArchNetwork::kINET, IArchNetwork::kSTREAM);
|
|
|
|
}
|
|
|
|
catch (XArchNetwork& e) {
|
|
|
|
throw XSocketCreate(e.what());
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
2004-02-02 00:09:22 +03:00
|
|
|
|
2001-10-06 18:13:28 +04:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2003-01-05 01:01:32 +03:00
|
|
|
CTCPSocket::CTCPSocket(CArchSocket socket) :
|
2004-02-02 00:09:22 +03:00
|
|
|
m_mutex(),
|
|
|
|
m_socket(socket),
|
|
|
|
m_flushed(&m_mutex, true),
|
|
|
|
m_eventFilter(NULL)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2003-01-05 01:01:32 +03:00
|
|
|
assert(m_socket != NULL);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// socket starts in connected state
|
2004-01-24 19:09:25 +03:00
|
|
|
init();
|
2004-02-02 00:09:22 +03:00
|
|
|
onConnected();
|
|
|
|
setJob(newJob());
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CTCPSocket::~CTCPSocket()
|
|
|
|
{
|
|
|
|
try {
|
2004-02-02 00:09:22 +03:00
|
|
|
close();
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
catch (...) {
|
2003-01-05 01:01:32 +03:00
|
|
|
// ignore
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CTCPSocket::bind(const CNetworkAddress& addr)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2003-01-05 01:01:32 +03: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 18:13:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CTCPSocket::close()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2004-02-02 00:09:22 +03:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
|
|
|
// clear buffers and enter disconnected state
|
|
|
|
if (m_connected) {
|
|
|
|
sendSocketEvent(getDisconnectedEvent());
|
|
|
|
}
|
|
|
|
onDisconnected();
|
|
|
|
|
|
|
|
// remove ourself from the multiplexer
|
|
|
|
setJob(NULL);
|
|
|
|
|
|
|
|
// close the socket
|
|
|
|
if (m_socket != NULL) {
|
|
|
|
CArchSocket socket = m_socket;
|
|
|
|
m_socket = NULL;
|
|
|
|
try {
|
|
|
|
ARCH->closeSocket(socket);
|
|
|
|
}
|
|
|
|
catch (XArchNetwork& e) {
|
|
|
|
// FIXME -- just discard this for now
|
|
|
|
//throw XSocketIOClose(e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
CTCPSocket::getEventTarget() const
|
|
|
|
{
|
|
|
|
return const_cast<void*>(reinterpret_cast<const void*>(this));
|
|
|
|
}
|
2002-06-03 02:57:50 +04:00
|
|
|
|
2004-02-02 00:09:22 +03:00
|
|
|
UInt32
|
|
|
|
CTCPSocket::read(void* buffer, UInt32 n)
|
|
|
|
{
|
|
|
|
// copy data directly from our input buffer
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
UInt32 size = m_inputBuffer.getSize();
|
|
|
|
if (n > size) {
|
|
|
|
n = size;
|
|
|
|
}
|
|
|
|
if (buffer != NULL) {
|
|
|
|
memcpy(buffer, m_inputBuffer.peek(n), n);
|
|
|
|
}
|
|
|
|
m_inputBuffer.pop(n);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2004-02-02 00:09:22 +03:00
|
|
|
// if no more data and we cannot read or write then send disconnected
|
|
|
|
if (n > 0 && !m_readable && !m_writable) {
|
|
|
|
sendSocketEvent(getDisconnectedEvent());
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CTCPSocket::write(const void* buffer, UInt32 n)
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
|
|
|
// must not have shutdown output
|
|
|
|
if (!m_writable) {
|
|
|
|
sendStreamEvent(getOutputErrorEvent());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore empty writes
|
|
|
|
if (n == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy data to the output buffer
|
|
|
|
bool wasEmpty = (m_outputBuffer.getSize() == 0);
|
|
|
|
m_outputBuffer.write(buffer, n);
|
|
|
|
|
|
|
|
// there's data to write
|
|
|
|
m_flushed = false;
|
|
|
|
|
|
|
|
// make sure we're waiting to write
|
|
|
|
if (wasEmpty) {
|
|
|
|
setJob(newJob());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CTCPSocket::flush()
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
while (m_flushed == false) {
|
|
|
|
m_flushed.wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CTCPSocket::shutdownInput()
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
|
|
|
// shutdown socket for reading
|
2004-01-24 19:09:25 +03:00
|
|
|
try {
|
2004-02-02 00:09:22 +03:00
|
|
|
ARCH->closeSocketForRead(m_socket);
|
2004-01-24 19:09:25 +03:00
|
|
|
}
|
2004-02-02 00:09:22 +03:00
|
|
|
catch (XArchNetwork&) {
|
2004-01-24 19:09:25 +03:00
|
|
|
// ignore
|
|
|
|
}
|
2004-02-02 00:09:22 +03:00
|
|
|
|
|
|
|
// shutdown buffer for reading
|
|
|
|
if (m_readable) {
|
|
|
|
sendStreamEvent(getInputShutdownEvent());
|
|
|
|
onInputShutdown();
|
|
|
|
setJob(newJob());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CTCPSocket::shutdownOutput()
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
|
|
|
// shutdown socket for writing
|
2004-01-24 19:09:25 +03:00
|
|
|
try {
|
2004-02-02 00:09:22 +03:00
|
|
|
ARCH->closeSocketForWrite(m_socket);
|
2004-01-24 19:09:25 +03:00
|
|
|
}
|
2004-02-02 00:09:22 +03:00
|
|
|
catch (XArchNetwork&) {
|
2004-01-24 19:09:25 +03:00
|
|
|
// ignore
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2004-02-02 00:09:22 +03:00
|
|
|
// shutdown buffer for writing
|
|
|
|
if (m_writable) {
|
|
|
|
sendStreamEvent(getOutputShutdownEvent());
|
|
|
|
onOutputShutdown();
|
|
|
|
setJob(newJob());
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-17 16:02:26 +04:00
|
|
|
void
|
2004-02-02 00:09:22 +03:00
|
|
|
CTCPSocket::setEventFilter(IEventJob* filter)
|
2002-06-17 16:02:26 +04:00
|
|
|
{
|
2004-02-02 00:09:22 +03:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
m_eventFilter = filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CTCPSocket::isReady() const
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
return (m_inputBuffer.getSize() > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
UInt32
|
|
|
|
CTCPSocket::getSize() const
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
return m_inputBuffer.getSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
IEventJob*
|
|
|
|
CTCPSocket::getEventFilter() const
|
|
|
|
{
|
|
|
|
CLock lock(&m_mutex);
|
|
|
|
return m_eventFilter;
|
2004-01-24 19:09:25 +03:00
|
|
|
}
|
2002-06-21 20:19:08 +04:00
|
|
|
|
2004-01-24 19:09:25 +03:00
|
|
|
void
|
|
|
|
CTCPSocket::connect(const CNetworkAddress& addr)
|
|
|
|
{
|
2004-02-02 00:09:22 +03:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
|
|
|
// fail on attempts to reconnect
|
|
|
|
if (m_socket == NULL || m_connected) {
|
|
|
|
sendSocketEvent(getConnectionFailedEvent());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-01-24 19:09:25 +03:00
|
|
|
try {
|
|
|
|
// FIXME -- don't throw if in progress, just return that info
|
|
|
|
ARCH->connectSocket(m_socket, addr.getAddress());
|
2004-02-02 00:09:22 +03:00
|
|
|
sendSocketEvent(getConnectedEvent());
|
|
|
|
onConnected();
|
|
|
|
setJob(newJob());
|
2004-01-24 19:09:25 +03:00
|
|
|
}
|
|
|
|
catch (XArchNetworkConnecting&) {
|
|
|
|
// connection is in progress
|
2004-02-02 00:09:22 +03:00
|
|
|
m_writable = true;
|
|
|
|
setJob(newJob());
|
2004-01-24 19:09:25 +03:00
|
|
|
}
|
|
|
|
catch (XArchNetwork& e) {
|
|
|
|
throw XSocketConnect(e.what());
|
|
|
|
}
|
2002-06-17 16:02:26 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CTCPSocket::init()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2004-02-02 00:09:22 +03:00
|
|
|
// default state
|
|
|
|
m_connected = false;
|
|
|
|
m_readable = false;
|
|
|
|
m_writable = false;
|
2004-01-24 19:09:25 +03:00
|
|
|
|
|
|
|
// make socket non-blocking
|
|
|
|
// FIXME -- check for error
|
|
|
|
ARCH->setBlockingOnSocket(m_socket, false);
|
2002-05-01 19:31:47 +04: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-05 01:01:32 +03: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 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2004-02-02 00:09:22 +03:00
|
|
|
void
|
|
|
|
CTCPSocket::setJob(ISocketMultiplexerJob* job)
|
2001-10-21 04:21:02 +04:00
|
|
|
{
|
2004-02-02 00:09:22 +03:00
|
|
|
// multiplexer will delete the old job
|
|
|
|
if (job == NULL) {
|
|
|
|
CSocketMultiplexer::getInstance()->removeSocket(this);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
CSocketMultiplexer::getInstance()->addSocket(this, job);
|
|
|
|
}
|
2002-05-30 20:11:59 +04:00
|
|
|
}
|
|
|
|
|
2004-01-24 19:09:25 +03:00
|
|
|
ISocketMultiplexerJob*
|
2004-02-02 00:09:22 +03:00
|
|
|
CTCPSocket::newJob()
|
2002-05-30 20:11:59 +04:00
|
|
|
{
|
2004-02-02 00:09:22 +03:00
|
|
|
// note -- must have m_mutex locked on entry
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2004-02-02 00:09:22 +03:00
|
|
|
if (m_socket == NULL || !(m_readable || m_writable)) {
|
|
|
|
return NULL;
|
2004-01-24 19:09:25 +03:00
|
|
|
}
|
2004-02-02 00:09:22 +03:00
|
|
|
else if (!m_connected) {
|
|
|
|
assert(!m_readable);
|
|
|
|
return new TSocketMultiplexerMethodJob<CTCPSocket>(
|
|
|
|
this, &CTCPSocket::serviceConnecting,
|
|
|
|
m_socket, m_readable, m_writable);
|
2004-01-24 19:09:25 +03:00
|
|
|
}
|
2004-02-02 00:09:22 +03:00
|
|
|
else {
|
|
|
|
return new TSocketMultiplexerMethodJob<CTCPSocket>(
|
|
|
|
this, &CTCPSocket::serviceConnected,
|
|
|
|
m_socket, m_readable,
|
|
|
|
m_writable && (m_outputBuffer.getSize() > 0));
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2004-02-02 00:09:22 +03:00
|
|
|
CTCPSocket::sendSocketEvent(CEvent::Type type)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2004-02-02 00:09:22 +03:00
|
|
|
EVENTQUEUE->addEvent(CEvent(type, this, NULL));
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2004-02-02 00:09:22 +03:00
|
|
|
CTCPSocket::sendStreamEvent(CEvent::Type type)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2004-02-02 00:09:22 +03:00
|
|
|
if (m_eventFilter != NULL) {
|
|
|
|
m_eventFilter->run(CEvent(type, this, NULL));
|
2003-01-05 01:01:32 +03:00
|
|
|
}
|
2004-02-02 00:09:22 +03:00
|
|
|
else {
|
|
|
|
EVENTQUEUE->addEvent(CEvent(type, this, NULL));
|
2003-01-05 01:01:32 +03:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
2004-01-24 19:09:25 +03:00
|
|
|
|
|
|
|
void
|
2004-02-02 00:09:22 +03:00
|
|
|
CTCPSocket::onConnected()
|
2004-01-24 19:09:25 +03:00
|
|
|
{
|
2004-02-02 00:09:22 +03:00
|
|
|
m_connected = true;
|
|
|
|
m_readable = true;
|
|
|
|
m_writable = true;
|
2004-01-24 19:09:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-02-02 00:09:22 +03:00
|
|
|
CTCPSocket::onInputShutdown()
|
2004-01-24 19:09:25 +03:00
|
|
|
{
|
2004-02-02 00:09:22 +03:00
|
|
|
m_inputBuffer.pop(m_inputBuffer.getSize());
|
|
|
|
m_readable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CTCPSocket::onOutputShutdown()
|
|
|
|
{
|
|
|
|
m_outputBuffer.pop(m_outputBuffer.getSize());
|
|
|
|
m_writable = false;
|
|
|
|
|
|
|
|
// we're now flushed
|
|
|
|
m_flushed = true;
|
|
|
|
m_flushed.broadcast();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CTCPSocket::onDisconnected()
|
|
|
|
{
|
|
|
|
// disconnected
|
|
|
|
onInputShutdown();
|
|
|
|
onOutputShutdown();
|
|
|
|
m_connected = false;
|
2004-01-24 19:09:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ISocketMultiplexerJob*
|
|
|
|
CTCPSocket::serviceConnecting(ISocketMultiplexerJob* job,
|
|
|
|
bool, bool write, bool error)
|
|
|
|
{
|
2004-02-02 00:09:22 +03:00
|
|
|
CLock lock(&m_mutex);
|
2004-01-24 19:09:25 +03:00
|
|
|
|
|
|
|
if (write && !error) {
|
|
|
|
try {
|
|
|
|
// connection may have failed or succeeded
|
|
|
|
ARCH->throwErrorOnSocket(m_socket);
|
|
|
|
}
|
|
|
|
catch (XArchNetwork&) {
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
2004-02-02 00:09:22 +03:00
|
|
|
sendSocketEvent(getConnectionFailedEvent());
|
|
|
|
onDisconnected();
|
|
|
|
return newJob();
|
2004-01-24 19:09:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (write) {
|
2004-02-02 00:09:22 +03:00
|
|
|
sendSocketEvent(getConnectedEvent());
|
|
|
|
onConnected();
|
|
|
|
return newJob();
|
2004-01-24 19:09:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return job;
|
|
|
|
}
|
|
|
|
|
|
|
|
ISocketMultiplexerJob*
|
|
|
|
CTCPSocket::serviceConnected(ISocketMultiplexerJob* job,
|
|
|
|
bool read, bool write, bool error)
|
|
|
|
{
|
2004-02-02 00:09:22 +03:00
|
|
|
CLock lock(&m_mutex);
|
|
|
|
|
2004-01-24 19:09:25 +03:00
|
|
|
if (error) {
|
2004-02-02 00:09:22 +03:00
|
|
|
sendSocketEvent(getDisconnectedEvent());
|
|
|
|
onDisconnected();
|
|
|
|
return newJob();
|
2004-01-24 19:09:25 +03:00
|
|
|
}
|
|
|
|
|
2004-02-02 00:09:22 +03:00
|
|
|
bool needNewJob = false;
|
2004-01-24 19:09:25 +03:00
|
|
|
|
2004-02-02 00:09:22 +03:00
|
|
|
if (write) {
|
2004-01-24 19:09:25 +03:00
|
|
|
try {
|
2004-02-02 00:09:22 +03:00
|
|
|
// write data
|
|
|
|
UInt32 n = m_outputBuffer.getSize();
|
|
|
|
const void* buffer = m_outputBuffer.peek(n);
|
|
|
|
n = (UInt32)ARCH->writeSocket(m_socket, buffer, n);
|
2004-01-24 19:09:25 +03:00
|
|
|
|
|
|
|
// discard written data
|
2004-02-02 00:09:22 +03:00
|
|
|
if (n > 0) {
|
|
|
|
m_outputBuffer.pop(n);
|
|
|
|
if (m_outputBuffer.getSize() == 0) {
|
|
|
|
sendStreamEvent(getOutputFlushedEvent());
|
|
|
|
m_flushed = true;
|
|
|
|
m_flushed.broadcast();
|
|
|
|
needNewJob = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (XArchNetworkShutdown&) {
|
|
|
|
// remote read end of stream hungup. our output side
|
|
|
|
// has therefore shutdown.
|
|
|
|
onOutputShutdown();
|
|
|
|
sendStreamEvent(getOutputShutdownEvent());
|
|
|
|
if (!m_readable && m_inputBuffer.getSize() == 0) {
|
|
|
|
sendSocketEvent(getDisconnectedEvent());
|
2004-01-24 19:09:25 +03:00
|
|
|
}
|
2004-02-02 00:09:22 +03:00
|
|
|
needNewJob = true;
|
2004-01-24 19:09:25 +03:00
|
|
|
}
|
|
|
|
catch (XArchNetworkDisconnected&) {
|
|
|
|
// stream hungup
|
2004-02-02 00:09:22 +03:00
|
|
|
onDisconnected();
|
|
|
|
sendSocketEvent(getDisconnectedEvent());
|
|
|
|
needNewJob = true;
|
|
|
|
}
|
|
|
|
catch (XArchNetwork&) {
|
|
|
|
// other write error
|
|
|
|
onDisconnected();
|
|
|
|
sendStreamEvent(getOutputErrorEvent());
|
|
|
|
sendSocketEvent(getDisconnectedEvent());
|
|
|
|
needNewJob = true;
|
2004-01-24 19:09:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-02 00:09:22 +03:00
|
|
|
if (read && m_readable) {
|
|
|
|
try {
|
|
|
|
UInt8 buffer[4096];
|
|
|
|
size_t n = ARCH->readSocket(m_socket, buffer, sizeof(buffer));
|
|
|
|
if (n > 0) {
|
|
|
|
bool wasEmpty = (m_inputBuffer.getSize() == 0);
|
|
|
|
|
|
|
|
// slurp up as much as possible
|
|
|
|
do {
|
|
|
|
m_inputBuffer.write(buffer, n);
|
2004-01-24 19:09:25 +03:00
|
|
|
n = ARCH->readSocket(m_socket, buffer, sizeof(buffer));
|
2004-02-02 00:09:22 +03:00
|
|
|
} while (n > 0);
|
|
|
|
|
|
|
|
// send input ready if input buffer was empty
|
|
|
|
if (wasEmpty) {
|
|
|
|
sendStreamEvent(getInputReadyEvent());
|
2004-01-24 19:09:25 +03:00
|
|
|
}
|
2004-02-02 00:09:22 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// remote write end of stream hungup. our input side
|
|
|
|
// has therefore shutdown but don't flush our buffer
|
|
|
|
// since there's still data to be read.
|
|
|
|
sendStreamEvent(getInputShutdownEvent());
|
|
|
|
if (!m_writable && m_inputBuffer.getSize() == 0) {
|
|
|
|
sendSocketEvent(getDisconnectedEvent());
|
2004-01-24 19:09:25 +03:00
|
|
|
}
|
2004-02-02 00:09:22 +03:00
|
|
|
m_readable = false;
|
|
|
|
needNewJob = true;
|
|
|
|
}
|
2004-01-24 19:09:25 +03:00
|
|
|
}
|
2004-02-02 00:09:22 +03:00
|
|
|
catch (XArchNetworkDisconnected&) {
|
2004-01-24 19:09:25 +03:00
|
|
|
// stream hungup
|
2004-02-02 00:09:22 +03:00
|
|
|
sendSocketEvent(getDisconnectedEvent());
|
|
|
|
onDisconnected();
|
|
|
|
needNewJob = true;
|
2004-01-24 19:09:25 +03:00
|
|
|
}
|
2004-02-02 00:09:22 +03:00
|
|
|
catch (XArchNetwork&) {
|
|
|
|
// ignore other read error
|
2004-01-24 19:09:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-02 00:09:22 +03:00
|
|
|
return needNewJob ? newJob() : job;
|
2004-01-24 19:09:25 +03:00
|
|
|
}
|