poco/Foundation/include/Poco/AsyncIOChannel.h
Guenter Obiltschnig e4d9b3b40f fixed line endings
2008-02-07 16:08:15 +00:00

155 lines
5.6 KiB
C++

//
// AsyncIOChannel.h
//
// $Id: //poco/svn/Foundation/include/Poco/AsyncIOChannel.h#2 $
//
// Library: Foundation
// Package: AsyncIO
// Module: AsyncIOChannel
//
// Definition of the AsyncIOChannel class.
//
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#ifndef Foundation_AsyncIOChannel_INCLUDED
#define Foundation_AsyncIOChannel_INCLUDED
#include "Poco/Foundation.h"
#include "Poco/ActiveDispatcher.h"
#include "Poco/ActiveMethod.h"
#include "Poco/AsyncIOCommand.h"
namespace Poco {
class Foundation_API AsyncIOChannel: public ActiveDispatcher
/// AsyncIOChannel supports asynchronous I/O operations on
/// streams or other input/output facilities.
///
/// This implementation of asynchronous I/O is based on
/// blocking I/O operations that are executed in a separate thread.
/// Some operating systems may provide special asynchronous
/// I/O operations at the operating system level. However, these
/// are not used by the current implementation.
///
/// I/O operations (in the form of AsyncIOCommand subclass instances)
/// are queued for execution on an AsyncIOChannel. The AsyncIOChannel's
/// I/O thread executes all queued commands in FIFO order.
///
/// Since AsyncIOChannel is based on ActiveDispatcher, the I/O thread is
/// taken from the default thread pool. It should be ensured that the
/// capacity of the default thread pool is sufficient before using
/// asynchronous I/O. For every AsyncIOChannel instance, one thread
/// from the default thread pool is needed.
///
/// Subclasses of AsyncIOChannel implement asynchronous input/output
/// for streams and sockets.
///
/// The enqueue() active member function is used to enqueue AsyncIOCommand
/// instances for execution.
///
/// The cancel() member function (inherited from ActiveDispatcher) can be
/// used to cancel all pending requests.
///
/// Both the AsyncIOCommand class, and the AsyncIOChannel class offer events
/// that notify an interested party about the successful or unsuccessful
/// completion of a command.
///
/// There are also two ways to wait for the completion of a command and
/// obtain its result. The first one is by using the return value of
/// enqueue(), which is ActiveResult<int>. See the ActiveResult class
/// template for more information. The second one is by using the functions
/// of AsyncIOCommand, such as wait(), succeeded() and result().
///
/// AsyncIOChannel supports the operations write, read and seek directly. However,
/// subclasses of AsyncIOChannel can define additional operations.
{
public:
ActiveMethod<int, AsyncIOCommand::Ptr, AsyncIOChannel, ActiveStarter<ActiveDispatcher> > enqueue;
/// Actual signature:
/// int enqueueImpl(const AsyncIOCommand::Ptr& pCommand);
///
/// Enqueue the given command for eventual execution.
/// Returns the number of bytes read or written if the operation
/// was successful.
BasicEvent<AsyncIOEvent> commandCompleted;
/// Fired when a command has successfully completed.
BasicEvent<AsyncIOEvent> commandFailed;
/// Fired when a command has failed.
virtual int write(const void* buffer, int length);
/// Write length bytes from buffer.
///
/// Must be overridden by subclasses. The default implementation
/// throws a NotImplementedException.
///
/// Returns the number of bytes written.
virtual int read(void* buffer, int length);
/// Read up to length bytes into buffer.
///
/// Must be overridden by subclasses. The default implementation
/// throws a NotImplementedException.
///
/// Returns the number of bytes read.
virtual int seek(std::streamoff off, std::ios::seekdir dir);
/// Sets the current read/write position.
///
/// Must be overridden by subclasses. The default implementation
/// throws a NotImplementedException.
///
/// Always returns 0.
protected:
AsyncIOChannel();
/// Creates the AsyncIOChannel.
~AsyncIOChannel();
/// Destroys the AsyncIOChannel.
int enqueueImpl(const AsyncIOCommand::Ptr& pCommand);
/// Execute the given command by calling
/// pCommand->execute(this);
/// and return the result.
private:
AsyncIOChannel(const AsyncIOChannel&);
AsyncIOChannel& operator = (const AsyncIOChannel&);
};
} // namespace Poco
#endif // Foundation_AsyncIOChannel_INCLUDED