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"
|
|
|
|
#include "CBufferedInputStream.h"
|
|
|
|
#include "CBufferedOutputStream.h"
|
|
|
|
#include "CNetworkAddress.h"
|
2002-06-11 02:06:45 +04:00
|
|
|
#include "XIO.h"
|
|
|
|
#include "XSocket.h"
|
|
|
|
#include "CCondVar.h"
|
2001-10-06 18:13:28 +04:00
|
|
|
#include "CLock.h"
|
|
|
|
#include "CMutex.h"
|
|
|
|
#include "CThread.h"
|
|
|
|
#include "CStopwatch.h"
|
2002-06-11 02:06:45 +04:00
|
|
|
#include "TMethodJob.h"
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// CTCPSocket
|
|
|
|
//
|
|
|
|
|
2001-10-14 20:58:01 +04:00
|
|
|
CTCPSocket::CTCPSocket()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-11-19 03:33:36 +03:00
|
|
|
m_fd = CNetwork::socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
if (m_fd == CNetwork::Null) {
|
2001-10-06 18:13:28 +04:00
|
|
|
throw XSocketCreate();
|
|
|
|
}
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
CTCPSocket::CTCPSocket(CNetwork::Socket fd) :
|
|
|
|
m_fd(fd)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-11-19 03:33:36 +03:00
|
|
|
assert(m_fd != CNetwork::Null);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
init();
|
|
|
|
|
|
|
|
// socket starts in connected state
|
|
|
|
m_connected = kReadWrite;
|
|
|
|
|
|
|
|
// start handling socket
|
|
|
|
m_thread = new CThread(new TMethodJob<CTCPSocket>(
|
2001-10-21 04:21:02 +04:00
|
|
|
this, &CTCPSocket::ioThread));
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CTCPSocket::~CTCPSocket()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// ignore failures
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
delete m_input;
|
|
|
|
delete m_output;
|
2001-10-21 04:21:02 +04:00
|
|
|
delete m_mutex;
|
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
|
|
|
{
|
2001-11-19 03:33:36 +03:00
|
|
|
if (CNetwork::bind(m_fd, addr.getAddress(),
|
|
|
|
addr.getAddressLength()) == CNetwork::Error) {
|
|
|
|
if (errno == CNetwork::kEADDRINUSE) {
|
2001-10-06 18:13:28 +04:00
|
|
|
throw XSocketAddressInUse();
|
|
|
|
}
|
|
|
|
throw XSocketBind();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CTCPSocket::close()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2002-06-03 02:57:50 +04: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
|
|
|
|
{
|
|
|
|
CLock lock(m_mutex);
|
|
|
|
if (m_fd != CNetwork::Null) {
|
|
|
|
CNetwork::shutdown(m_fd, 2);
|
|
|
|
m_connected = kClosed;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
2002-06-03 02:57:50 +04:00
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2002-06-03 02:57:50 +04:00
|
|
|
// wait for thread
|
|
|
|
if (m_thread != NULL) {
|
2001-10-06 18:13:28 +04:00
|
|
|
m_thread->wait();
|
|
|
|
delete m_thread;
|
|
|
|
m_thread = NULL;
|
|
|
|
}
|
|
|
|
|
2002-06-03 02:57:50 +04:00
|
|
|
// close socket
|
2001-11-19 03:33:36 +03:00
|
|
|
if (m_fd != CNetwork::Null) {
|
|
|
|
if (CNetwork::close(m_fd) == CNetwork::Error) {
|
2001-10-06 18:13:28 +04:00
|
|
|
throw XIOClose();
|
|
|
|
}
|
2001-11-19 03:33:36 +03:00
|
|
|
m_fd = CNetwork::Null;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-17 16:02:26 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CTCPSocket::connect(const CNetworkAddress& addr)
|
2002-06-17 16:02:26 +04:00
|
|
|
{
|
2002-06-21 20:19:08 +04:00
|
|
|
// connect asynchronously so we can check for cancellation
|
|
|
|
CNetwork::setblocking(m_fd, false);
|
2002-06-17 16:02:26 +04:00
|
|
|
if (CNetwork::connect(m_fd, addr.getAddress(),
|
|
|
|
addr.getAddressLength()) == CNetwork::Error) {
|
2002-06-21 20:19:08 +04:00
|
|
|
// check for failure
|
|
|
|
if (CNetwork::getsockerror() != CNetwork::kECONNECTING) {
|
2002-07-24 17:01:18 +04:00
|
|
|
XSocketConnect e;
|
2002-06-21 20:19:08 +04:00
|
|
|
CNetwork::setblocking(m_fd, true);
|
2002-07-24 17:01:18 +04:00
|
|
|
throw e;
|
2002-06-21 20:19:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// wait for connection or failure
|
|
|
|
CNetwork::PollEntry pfds[1];
|
|
|
|
pfds[0].fd = m_fd;
|
|
|
|
pfds[0].events = CNetwork::kPOLLOUT;
|
|
|
|
for (;;) {
|
|
|
|
CThread::testCancel();
|
|
|
|
const int status = CNetwork::poll(pfds, 1, 10);
|
|
|
|
if (status > 0) {
|
|
|
|
if ((pfds[0].revents & (CNetwork::kPOLLERR |
|
|
|
|
CNetwork::kPOLLNVAL)) != 0) {
|
|
|
|
// connection failed
|
2002-07-24 17:01:18 +04:00
|
|
|
int error = 0;
|
|
|
|
CNetwork::AddressLength size = sizeof(error);
|
2002-06-21 20:19:08 +04:00
|
|
|
CNetwork::setblocking(m_fd, true);
|
2002-07-24 17:01:18 +04:00
|
|
|
CNetwork::getsockopt(m_fd, SOL_SOCKET, SO_ERROR,
|
|
|
|
reinterpret_cast<char*>(&error), &size);
|
|
|
|
throw XSocketConnect(error);
|
2002-06-21 20:19:08 +04:00
|
|
|
}
|
|
|
|
if ((pfds[0].revents & CNetwork::kPOLLOUT) != 0) {
|
|
|
|
int error;
|
|
|
|
CNetwork::AddressLength size = sizeof(error);
|
|
|
|
if (CNetwork::getsockopt(m_fd, SOL_SOCKET, SO_ERROR,
|
|
|
|
reinterpret_cast<char*>(&error),
|
|
|
|
&size) == CNetwork::Error ||
|
|
|
|
error != 0) {
|
|
|
|
// connection failed
|
|
|
|
CNetwork::setblocking(m_fd, true);
|
2002-07-24 17:01:18 +04:00
|
|
|
throw XSocketConnect(error);
|
2002-06-21 20:19:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// connected!
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-06-17 16:02:26 +04:00
|
|
|
}
|
|
|
|
|
2002-06-21 20:19:08 +04:00
|
|
|
// back to blocking
|
|
|
|
CNetwork::setblocking(m_fd, true);
|
|
|
|
|
2002-06-17 16:02:26 +04:00
|
|
|
// start servicing the socket
|
|
|
|
m_connected = kReadWrite;
|
|
|
|
m_thread = new CThread(new TMethodJob<CTCPSocket>(
|
|
|
|
this, &CTCPSocket::ioThread));
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
IInputStream*
|
|
|
|
CTCPSocket::getInputStream()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
return m_input;
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
IOutputStream*
|
|
|
|
CTCPSocket::getOutputStream()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
return m_output;
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CTCPSocket::init()
|
2001-10-06 18:13:28 +04: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 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.
|
|
|
|
CNetwork::TCPNoDelayType flag = 1;
|
|
|
|
CNetwork::setsockopt(m_fd, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CTCPSocket::ioThread(void*)
|
2001-10-21 04:21:02 +04:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
ioService();
|
2002-05-30 20:11:59 +04:00
|
|
|
ioCleanup();
|
2001-10-21 04:21:02 +04:00
|
|
|
}
|
|
|
|
catch (...) {
|
2002-05-30 20:11:59 +04:00
|
|
|
ioCleanup();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CTCPSocket::ioCleanup()
|
2002-05-30 20:11:59 +04:00
|
|
|
{
|
|
|
|
try {
|
2001-10-21 04:21:02 +04:00
|
|
|
m_input->close();
|
2002-05-30 20:11:59 +04:00
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
try {
|
2001-10-21 04:21:02 +04:00
|
|
|
m_output->close();
|
2002-05-30 20:11:59 +04:00
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// ignore
|
2001-10-21 04:21:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CTCPSocket::ioService()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-11-19 03:33:36 +03:00
|
|
|
assert(m_fd != CNetwork::Null);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// now service the connection
|
2001-11-19 03:33:36 +03:00
|
|
|
CNetwork::PollEntry pfds[1];
|
2001-10-06 18:13:28 +04:00
|
|
|
pfds[0].fd = m_fd;
|
|
|
|
for (;;) {
|
|
|
|
{
|
|
|
|
// choose events to poll for
|
|
|
|
CLock lock(m_mutex);
|
|
|
|
pfds[0].events = 0;
|
2002-05-30 20:11:59 +04:00
|
|
|
if (m_connected == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
if ((m_connected & kRead) != 0) {
|
|
|
|
// still open for reading
|
2001-11-19 03:33:36 +03:00
|
|
|
pfds[0].events |= CNetwork::kPOLLIN;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
if ((m_connected & kWrite) != 0 && m_output->getSize() > 0) {
|
|
|
|
// data queued for writing
|
2001-11-19 03:33:36 +03:00
|
|
|
pfds[0].events |= CNetwork::kPOLLOUT;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for status
|
2002-06-03 02:57:50 +04:00
|
|
|
const int status = CNetwork::poll(pfds, 1, 10);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// transfer data and handle errors
|
|
|
|
if (status == 1) {
|
2001-11-19 03:33:36 +03:00
|
|
|
if ((pfds[0].revents & (CNetwork::kPOLLERR |
|
|
|
|
CNetwork::kPOLLNVAL)) != 0) {
|
2001-10-06 18:13:28 +04:00
|
|
|
// stream is no good anymore so bail
|
|
|
|
m_input->hangup();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read some data
|
2001-11-19 03:33:36 +03:00
|
|
|
if (pfds[0].revents & CNetwork::kPOLLIN) {
|
2001-10-06 18:13:28 +04:00
|
|
|
UInt8 buffer[4096];
|
2001-11-19 03:33:36 +03:00
|
|
|
ssize_t n = CNetwork::read(m_fd, buffer, sizeof(buffer));
|
2001-10-06 18:13:28 +04:00
|
|
|
if (n > 0) {
|
|
|
|
CLock lock(m_mutex);
|
|
|
|
m_input->write(buffer, n);
|
|
|
|
}
|
|
|
|
else if (n == 0) {
|
|
|
|
// stream hungup
|
|
|
|
m_input->hangup();
|
2002-05-30 20:11:59 +04:00
|
|
|
m_connected &= ~kRead;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// write some data
|
2001-11-19 03:33:36 +03:00
|
|
|
if (pfds[0].revents & CNetwork::kPOLLOUT) {
|
2001-10-06 18:13:28 +04:00
|
|
|
CLock lock(m_mutex);
|
|
|
|
|
|
|
|
// get amount of data to write
|
|
|
|
UInt32 n = m_output->getSize();
|
|
|
|
|
|
|
|
// write data
|
|
|
|
const void* buffer = m_output->peek(n);
|
2001-11-19 03:33:36 +03:00
|
|
|
n = (UInt32)CNetwork::write(m_fd, buffer, n);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
// discard written data
|
|
|
|
if (n > 0) {
|
|
|
|
m_output->pop(n);
|
|
|
|
}
|
2001-11-19 03:33:36 +03:00
|
|
|
else if (n == (UInt32)-1 && CNetwork::getsockerror() == EPIPE) {
|
2001-10-21 04:21:02 +04:00
|
|
|
return;
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CTCPSocket::closeInput(void*)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
// note -- m_mutex should already be locked
|
2001-11-19 03:33:36 +03:00
|
|
|
CNetwork::shutdown(m_fd, 0);
|
2001-10-06 18:13:28 +04:00
|
|
|
m_connected &= ~kRead;
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CTCPSocket::closeOutput(void*)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
// note -- m_mutex should already be locked
|
2001-11-19 03:33:36 +03:00
|
|
|
CNetwork::shutdown(m_fd, 1);
|
2001-10-06 18:13:28 +04:00
|
|
|
m_connected &= ~kWrite;
|
|
|
|
}
|