moving 1.4 to trunk

This commit is contained in:
Nick Bolton
2012-06-10 16:50:54 +00:00
parent cdeb3a7824
commit 488241850c
1291 changed files with 425650 additions and 12 deletions

48
src/lib/io/CMakeLists.txt Normal file
View File

@@ -0,0 +1,48 @@
# synergy -- mouse and keyboard sharing utility
# Copyright (C) 2009 Chris Schoeneman, Nick Bolton, Sorin Sbarnea
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set(inc
CStreamBuffer.h
CStreamFilter.h
IStream.h
IStreamFilterFactory.h
XIO.h
)
set(src
CStreamBuffer.cpp
CStreamFilter.cpp
IStream.cpp
XIO.cpp
)
if (WIN32)
list(APPEND src ${inc})
endif()
set(inc
../arch
../base
../common
)
if (UNIX)
list(APPEND inc
../../..
)
endif()
include_directories(${inc})
add_library(io STATIC ${src})

View File

@@ -0,0 +1,145 @@
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2002 Chris Schoeneman, Nick Bolton, Sorin Sbarnea
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "CStreamBuffer.h"
//
// CStreamBuffer
//
const UInt32 CStreamBuffer::kChunkSize = 4096;
CStreamBuffer::CStreamBuffer() :
m_size(0),
m_headUsed(0)
{
// do nothing
}
CStreamBuffer::~CStreamBuffer()
{
// do nothing
}
const void*
CStreamBuffer::peek(UInt32 n)
{
assert(n <= m_size);
// if requesting no data then return NULL so we don't try to access
// an empty list.
if (n == 0) {
return NULL;
}
// reserve space in first chunk
ChunkList::iterator head = m_chunks.begin();
head->reserve(n + m_headUsed);
// consolidate chunks into the first chunk until it has n bytes
ChunkList::iterator scan = head;
++scan;
while (head->size() - m_headUsed < n && scan != m_chunks.end()) {
head->insert(head->end(), scan->begin(), scan->end());
scan = m_chunks.erase(scan);
}
return reinterpret_cast<const void*>(&(head->begin()[m_headUsed]));
}
void
CStreamBuffer::pop(UInt32 n)
{
// discard all chunks if n is greater than or equal to m_size
if (n >= m_size) {
m_size = 0;
m_headUsed = 0;
m_chunks.clear();
return;
}
// update size
m_size -= n;
// discard chunks until more than n bytes would've been discarded
ChunkList::iterator scan = m_chunks.begin();
assert(scan != m_chunks.end());
while (scan->size() - m_headUsed <= n) {
n -= (UInt32)scan->size() - m_headUsed;
m_headUsed = 0;
scan = m_chunks.erase(scan);
assert(scan != m_chunks.end());
}
// remove left over bytes from the head chunk
if (n > 0) {
m_headUsed += n;
}
}
void
CStreamBuffer::write(const void* vdata, UInt32 n)
{
assert(vdata != NULL);
// ignore if no data, otherwise update size
if (n == 0) {
return;
}
m_size += n;
// cast data to bytes
const UInt8* data = reinterpret_cast<const UInt8*>(vdata);
// point to last chunk if it has space, otherwise append an empty chunk
ChunkList::iterator scan = m_chunks.end();
if (scan != m_chunks.begin()) {
--scan;
if (scan->size() >= kChunkSize) {
++scan;
}
}
if (scan == m_chunks.end()) {
scan = m_chunks.insert(scan, Chunk());
}
// append data in chunks
while (n > 0) {
// choose number of bytes for next chunk
assert(scan->size() <= kChunkSize);
UInt32 count = kChunkSize - (UInt32)scan->size();
if (count > n)
count = n;
// transfer data
scan->insert(scan->end(), data, data + count);
n -= count;
data += count;
// append another empty chunk if we're not done yet
if (n > 0) {
++scan;
scan = m_chunks.insert(scan, Chunk());
}
}
}
UInt32
CStreamBuffer::getSize() const
{
return m_size;
}

View File

@@ -0,0 +1,81 @@
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2002 Chris Schoeneman, Nick Bolton, Sorin Sbarnea
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CSTREAMBUFFER_H
#define CSTREAMBUFFER_H
#include "BasicTypes.h"
#include "stdlist.h"
#include "stdvector.h"
//! FIFO of bytes
/*!
This class maintains a FIFO (first-in, last-out) buffer of bytes.
*/
class CStreamBuffer {
public:
CStreamBuffer();
~CStreamBuffer();
//! @name manipulators
//@{
//! Read data without removing from buffer
/*!
Return a pointer to memory with the next \c n bytes in the buffer
(which must be <= getSize()). The caller must not modify the returned
memory nor delete it.
*/
const void* peek(UInt32 n);
//! Discard data
/*!
Discards the next \c n bytes. If \c n >= getSize() then the buffer
is cleared.
*/
void pop(UInt32 n);
//! Write data to buffer
/*!
Appends \c n bytes from \c data to the buffer.
*/
void write(const void* data, UInt32 n);
//@}
//! @name accessors
//@{
//! Get size of buffer
/*!
Returns the number of bytes in the buffer.
*/
UInt32 getSize() const;
//@}
private:
static const UInt32 kChunkSize;
typedef std::vector<UInt8> Chunk;
typedef std::list<Chunk> ChunkList;
ChunkList m_chunks;
UInt32 m_size;
UInt32 m_headUsed;
};
#endif

View File

@@ -0,0 +1,116 @@
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2004 Chris Schoeneman, Nick Bolton, Sorin Sbarnea
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "CStreamFilter.h"
#include "IEventQueue.h"
#include "TMethodEventJob.h"
//
// CStreamFilter
//
CStreamFilter::CStreamFilter(IStream* stream, bool adoptStream) :
m_stream(stream),
m_adopted(adoptStream)
{
// replace handlers for m_stream
EVENTQUEUE->removeHandlers(m_stream->getEventTarget());
EVENTQUEUE->adoptHandler(CEvent::kUnknown, m_stream->getEventTarget(),
new TMethodEventJob<CStreamFilter>(this,
&CStreamFilter::handleUpstreamEvent));
}
CStreamFilter::~CStreamFilter()
{
EVENTQUEUE->removeHandler(CEvent::kUnknown, m_stream->getEventTarget());
if (m_adopted) {
delete m_stream;
}
}
void
CStreamFilter::close()
{
getStream()->close();
}
UInt32
CStreamFilter::read(void* buffer, UInt32 n)
{
return getStream()->read(buffer, n);
}
void
CStreamFilter::write(const void* buffer, UInt32 n)
{
getStream()->write(buffer, n);
}
void
CStreamFilter::flush()
{
getStream()->flush();
}
void
CStreamFilter::shutdownInput()
{
getStream()->shutdownInput();
}
void
CStreamFilter::shutdownOutput()
{
getStream()->shutdownOutput();
}
void*
CStreamFilter::getEventTarget() const
{
return const_cast<void*>(reinterpret_cast<const void*>(this));
}
bool
CStreamFilter::isReady() const
{
return getStream()->isReady();
}
UInt32
CStreamFilter::getSize() const
{
return getStream()->getSize();
}
IStream*
CStreamFilter::getStream() const
{
return m_stream;
}
void
CStreamFilter::filterEvent(const CEvent& event)
{
EVENTQUEUE->dispatchEvent(CEvent(event.getType(),
getEventTarget(), event.getData()));
}
void
CStreamFilter::handleUpstreamEvent(const CEvent& event, void*)
{
filterEvent(event);
}

View File

@@ -0,0 +1,73 @@
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2004 Chris Schoeneman, Nick Bolton, Sorin Sbarnea
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CSTREAMFILTER_H
#define CSTREAMFILTER_H
#include "IStream.h"
//! A stream filter
/*!
This class wraps a stream. Subclasses provide indirect access
to the wrapped stream, typically performing some filtering.
*/
class CStreamFilter : public IStream {
public:
/*!
Create a wrapper around \c stream. Iff \c adoptStream is true then
this object takes ownership of the stream and will delete it in the
d'tor.
*/
CStreamFilter(IStream* stream, bool adoptStream = true);
~CStreamFilter();
// IStream overrides
// These all just forward to the underlying stream except getEventTarget.
// Override as necessary. getEventTarget returns a pointer to this.
virtual void close();
virtual UInt32 read(void* buffer, UInt32 n);
virtual void write(const void* buffer, UInt32 n);
virtual void flush();
virtual void shutdownInput();
virtual void shutdownOutput();
virtual void* getEventTarget() const;
virtual bool isReady() const;
virtual UInt32 getSize() const;
protected:
//! Get the stream
/*!
Returns the stream passed to the c'tor.
*/
IStream* getStream() const;
//! Handle events from source stream
/*!
Does the event filtering. The default simply dispatches an event
identical except using this object as the event target.
*/
virtual void filterEvent(const CEvent&);
private:
void handleUpstreamEvent(const CEvent&, void*);
private:
IStream* m_stream;
bool m_adopted;
};
#endif

64
src/lib/io/IStream.cpp Normal file
View File

@@ -0,0 +1,64 @@
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2004 Chris Schoeneman, Nick Bolton, Sorin Sbarnea
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "IStream.h"
#include "CEventQueue.h"
//
// IStream
//
CEvent::Type IStream::s_inputReadyEvent = CEvent::kUnknown;
CEvent::Type IStream::s_outputFlushedEvent = CEvent::kUnknown;
CEvent::Type IStream::s_outputErrorEvent = CEvent::kUnknown;
CEvent::Type IStream::s_inputShutdownEvent = CEvent::kUnknown;
CEvent::Type IStream::s_outputShutdownEvent = CEvent::kUnknown;
CEvent::Type
IStream::getInputReadyEvent()
{
return m_eventQueue.registerTypeOnce(s_inputReadyEvent,
"IStream::inputReady");
}
CEvent::Type
IStream::getOutputFlushedEvent()
{
return m_eventQueue.registerTypeOnce(s_outputFlushedEvent,
"IStream::outputFlushed");
}
CEvent::Type
IStream::getOutputErrorEvent()
{
return m_eventQueue.registerTypeOnce(s_outputErrorEvent,
"IStream::outputError");
}
CEvent::Type
IStream::getInputShutdownEvent()
{
return m_eventQueue.registerTypeOnce(s_inputShutdownEvent,
"IStream::inputShutdown");
}
CEvent::Type
IStream::getOutputShutdownEvent()
{
return m_eventQueue.registerTypeOnce(s_outputShutdownEvent,
"IStream::outputShutdown");
}

168
src/lib/io/IStream.h Normal file
View File

@@ -0,0 +1,168 @@
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2004 Chris Schoeneman, Nick Bolton, Sorin Sbarnea
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ISTREAM_H
#define ISTREAM_H
#include "IInterface.h"
#include "CEvent.h"
#include "IEventQueue.h"
class IEventQueue;
//! Bidirectional stream interface
/*!
Defines the interface for all streams.
*/
class IStream : public IInterface {
public:
IStream() : m_eventQueue(*EVENTQUEUE) { }
IStream(IEventQueue& eventQueue) : m_eventQueue(eventQueue) { }
//! @name manipulators
//@{
//! Close the stream
/*!
Closes the stream. Pending input data and buffered output data
are discarded. Use \c flush() before \c close() to send buffered
output data. Attempts to \c read() after a close return 0,
attempts to \c write() generate output error events, and attempts
to \c flush() return immediately.
*/
virtual void close() = 0;
//! Read from stream
/*!
Read up to \p n bytes into \p buffer, returning the number read
(zero if no data is available or input is shutdown). \p buffer
may be NULL in which case the data is discarded.
*/
virtual UInt32 read(void* buffer, UInt32 n) = 0;
//! Write to stream
/*!
Write \c n bytes from \c buffer to the stream. If this can't
complete immediately it will block. Data may be buffered in
order to return more quickly. A output error event is generated
when writing fails.
*/
virtual void write(const void* buffer, UInt32 n) = 0;
//! Flush the stream
/*!
Waits until all buffered data has been written to the stream.
*/
virtual void flush() = 0;
//! Shutdown input
/*!
Shutdown the input side of the stream. Any pending input data is
discarded and further reads immediately return 0.
*/
virtual void shutdownInput() = 0;
//! Shutdown output
/*!
Shutdown the output side of the stream. Any buffered output data
is discarded and further writes generate output error events. Use
\c flush() before \c shutdownOutput() to send buffered output data.
*/
virtual void shutdownOutput() = 0;
//@}
//! @name accessors
//@{
//! Get event target
/*!
Returns the event target for events generated by this stream. It
should be the source stream in a chain of stream filters.
*/
virtual void* getEventTarget() const = 0;
//! Test if \c read() will succeed
/*!
Returns true iff an immediate \c read() will return data. This
may or may not be the same as \c getSize() > 0, depending on the
stream type.
*/
virtual bool isReady() const = 0;
//! Get bytes available to read
/*!
Returns a conservative estimate of the available bytes to read
(i.e. a number not greater than the actual number of bytes).
Some streams may not be able to determine this and will always
return zero.
*/
virtual UInt32 getSize() const = 0;
//! Get input ready event type
/*!
Returns the input ready event type. A stream sends this event
when \c read() will return with data.
*/
virtual CEvent::Type getInputReadyEvent();
//! Get output flushed event type
/*!
Returns the output flushed event type. A stream sends this event
when the output buffer has been flushed. If there have been no
writes since the event was posted, calling \c shutdownOutput() or
\c close() will not discard any data and \c flush() will return
immediately.
*/
virtual CEvent::Type getOutputFlushedEvent();
//! Get output error event type
/*!
Returns the output error event type. A stream sends this event
when a write has failed.
*/
virtual CEvent::Type getOutputErrorEvent();
//! Get input shutdown event type
/*!
Returns the input shutdown event type. This is sent when the
input side of the stream has shutdown. When the input has
shutdown, no more data will ever be available to read.
*/
virtual CEvent::Type getInputShutdownEvent();
//! Get output shutdown event type
/*!
Returns the output shutdown event type. This is sent when the
output side of the stream has shutdown. When the output has
shutdown, no more data can ever be written to the stream. Any
attempt to do so will generate a output error event.
*/
virtual CEvent::Type getOutputShutdownEvent();
//@}
private:
IEventQueue& m_eventQueue;
static CEvent::Type s_inputReadyEvent;
static CEvent::Type s_outputFlushedEvent;
static CEvent::Type s_outputErrorEvent;
static CEvent::Type s_inputShutdownEvent;
static CEvent::Type s_outputShutdownEvent;
};
#endif

View File

@@ -0,0 +1,39 @@
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2002 Chris Schoeneman, Nick Bolton, Sorin Sbarnea
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ISTREAMFILTERFACTORY_H
#define ISTREAMFILTERFACTORY_H
#include "IInterface.h"
class IStream;
//! Stream filter factory interface
/*!
This interface provides factory methods to create stream filters.
*/
class IStreamFilterFactory : public IInterface {
public:
//! Create filter
/*!
Create and return a stream filter on \p stream. The caller must
delete the returned object.
*/
virtual IStream* create(IStream* stream, bool adoptStream) = 0;
};
#endif

50
src/lib/io/XIO.cpp Normal file
View File

@@ -0,0 +1,50 @@
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2002 Chris Schoeneman, Nick Bolton, Sorin Sbarnea
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "XIO.h"
//
// XIOClosed
//
CString
XIOClosed::getWhat() const throw()
{
return format("XIOClosed", "already closed");
}
//
// XIOEndOfStream
//
CString
XIOEndOfStream::getWhat() const throw()
{
return format("XIOEndOfStream", "reached end of stream");
}
//
// XIOWouldBlock
//
CString
XIOWouldBlock::getWhat() const throw()
{
return format("XIOWouldBlock", "stream operation would block");
}

51
src/lib/io/XIO.h Normal file
View File

@@ -0,0 +1,51 @@
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2002 Chris Schoeneman, Nick Bolton, Sorin Sbarnea
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef XIO_H
#define XIO_H
#include "XBase.h"
//! Generic I/O exception
XBASE_SUBCLASS(XIO, XBase);
//! I/O closing exception
/*!
Thrown if a stream cannot be closed.
*/
XBASE_SUBCLASS(XIOClose, XIO);
//! I/O already closed exception
/*!
Thrown when attempting to close or perform I/O on an already closed.
stream.
*/
XBASE_SUBCLASS_WHAT(XIOClosed, XIO);
//! I/O end of stream exception
/*!
Thrown when attempting to read beyond the end of a stream.
*/
XBASE_SUBCLASS_WHAT(XIOEndOfStream, XIO);
//! I/O would block exception
/*!
Thrown if an operation on a stream would block.
*/
XBASE_SUBCLASS_WHAT(XIOWouldBlock, XIO);
#endif