2008-02-07 17:08:15 +01:00
|
|
|
//
|
|
|
|
// AsyncIOCommand.h
|
|
|
|
//
|
|
|
|
// $Id: //poco/svn/Foundation/include/Poco/AsyncIOCommand.h#2 $
|
|
|
|
//
|
|
|
|
// Library: Foundation
|
|
|
|
// Package: AsyncIO
|
|
|
|
// Module: AsyncIOCommand
|
|
|
|
//
|
|
|
|
// Definition of the AsyncIOCommand class and subclasses.
|
|
|
|
//
|
|
|
|
// 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_AsyncIOCommand_INCLUDED
|
|
|
|
#define Foundation_AsyncIOCommand_INCLUDED
|
|
|
|
|
|
|
|
|
|
|
|
#include "Poco/Foundation.h"
|
|
|
|
#include "Poco/RefCountedObject.h"
|
|
|
|
#include "Poco/AutoPtr.h"
|
|
|
|
#include "Poco/Event.h"
|
|
|
|
#include "Poco/BasicEvent.h"
|
|
|
|
#include "Poco/AsyncIOEvent.h"
|
|
|
|
#include "Poco/Exception.h"
|
|
|
|
#include <ios>
|
|
|
|
|
|
|
|
|
|
|
|
namespace Poco {
|
|
|
|
|
|
|
|
|
|
|
|
class AsyncIOChannel;
|
|
|
|
|
|
|
|
|
|
|
|
class Foundation_API AsyncIOCommand: public RefCountedObject
|
|
|
|
/// AsyncIOCommand is the base class for all asynchronous input/output
|
|
|
|
/// commands used with AsyncIOChannel.
|
|
|
|
///
|
|
|
|
/// See class AsyncIOChannel for more information about asynchronous input/output.
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef AutoPtr<AsyncIOCommand> Ptr;
|
|
|
|
|
|
|
|
enum State
|
|
|
|
{
|
|
|
|
CMD_PENDING, /// Command is queued for execution.
|
|
|
|
CMD_IN_PROGRESS, /// Command is currently being executed.
|
|
|
|
CMD_COMPLETED, /// Command has completed successfully.
|
|
|
|
CMD_FAILED /// Command has failed.
|
|
|
|
};
|
|
|
|
|
|
|
|
BasicEvent<AsyncIOEvent> commandCompleted;
|
|
|
|
/// Fired when the command has successfully completed.
|
|
|
|
|
|
|
|
BasicEvent<AsyncIOEvent> commandFailed;
|
|
|
|
/// Fired when the command has failed.
|
|
|
|
|
|
|
|
State state() const;
|
|
|
|
/// Returns the current state of the command.
|
|
|
|
|
|
|
|
void wait();
|
|
|
|
/// Waits for the completion of the command.
|
|
|
|
|
|
|
|
void wait(long milliseconds);
|
|
|
|
/// Waits at most the given timeout for the
|
|
|
|
/// completion of the command. Throws a
|
|
|
|
/// TimeoutException if the command does
|
|
|
|
/// not complete within the given timeout.
|
|
|
|
|
|
|
|
bool tryWait(long milliseconds);
|
|
|
|
/// Waits at most the given timeout for the
|
|
|
|
/// completion of the command.
|
|
|
|
///
|
|
|
|
/// Returns true if the command completes within
|
|
|
|
/// the given timeout, otherwise false.
|
|
|
|
|
|
|
|
bool succeeded() const;
|
|
|
|
/// Returns true iff the command completed successfully.
|
|
|
|
|
|
|
|
bool failed() const;
|
|
|
|
/// Returns true iff the command has completed with an error.
|
|
|
|
|
|
|
|
const Exception* exception() const;
|
|
|
|
/// If the command failed with an exception, returns a
|
|
|
|
/// clone of the exception. Otherwise, returns a null pointer.
|
|
|
|
|
|
|
|
int execute(AsyncIOChannel& channel);
|
|
|
|
/// Executes the command on the given AsyncIOChannel.
|
|
|
|
///
|
|
|
|
/// Sets the state to CMD_IN_PROGRESS, calls
|
|
|
|
/// executeImpl(), and, according to its result,
|
|
|
|
/// sets the state to CMD_COMPLETED or CMD_FAILED
|
|
|
|
/// and signals the event.
|
|
|
|
///
|
|
|
|
/// Returns the number of bytes processed by the
|
|
|
|
/// command.
|
|
|
|
|
|
|
|
int result() const;
|
|
|
|
/// Returns the result of the command.
|
|
|
|
|
|
|
|
protected:
|
|
|
|
AsyncIOCommand();
|
|
|
|
/// Creates the AsyncIOCommand.
|
|
|
|
|
|
|
|
~AsyncIOCommand();
|
|
|
|
/// Destroys the AsyncIOCommand.
|
|
|
|
|
|
|
|
virtual int executeImpl(AsyncIOChannel& channel) = 0;
|
|
|
|
/// Executes the command on the given AsyncIOChannel.
|
|
|
|
/// Must be overridded by subclasses.
|
|
|
|
///
|
|
|
|
/// Returns the number of bytes processed by the
|
|
|
|
/// command.
|
|
|
|
|
|
|
|
private:
|
|
|
|
State _state;
|
|
|
|
Event _completed;
|
|
|
|
int _result;
|
|
|
|
Exception* _pException;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Foundation_API AsyncWriteCommand: public AsyncIOCommand
|
|
|
|
/// An asynchronous write command.
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AsyncWriteCommand(const void* buffer, int length);
|
|
|
|
/// Create an AsyncWriteCommand for writing length bytes
|
|
|
|
/// from the given buffer. The given buffer must be
|
|
|
|
/// valid until the command completes.
|
|
|
|
|
|
|
|
const void* buffer() const;
|
|
|
|
/// Returns the buffer's address.
|
|
|
|
|
|
|
|
int length() const;
|
|
|
|
/// Returns the buffer's size.
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int executeImpl(AsyncIOChannel& channel);
|
|
|
|
~AsyncWriteCommand();
|
|
|
|
|
|
|
|
private:
|
|
|
|
AsyncWriteCommand();
|
|
|
|
|
|
|
|
const void* _buffer;
|
|
|
|
int _length;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Foundation_API AsyncBufferedWriteCommand: public AsyncWriteCommand
|
|
|
|
/// An asynchronous write command. The difference to
|
|
|
|
/// AsyncWriteCommand is that AsyncBufferedWriteCommand
|
|
|
|
/// copies the data in the buffer into an internal buffer, thus
|
|
|
|
/// the given buffer can be deleted as soon as the constructor
|
|
|
|
/// returns.
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AsyncBufferedWriteCommand(const void* buffer, int length);
|
|
|
|
/// Create an AsyncBufferedWriteCommand for writing length bytes
|
|
|
|
/// from the given buffer and copies the data from the
|
|
|
|
/// given buffer into an internal buffer.
|
|
|
|
///
|
|
|
|
/// The internal buffer can be accessed via the buffer()
|
|
|
|
/// member function inherited from AsyncWriteCommand.
|
|
|
|
|
|
|
|
protected:
|
|
|
|
~AsyncBufferedWriteCommand();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Foundation_API AsyncReadCommand: public AsyncIOCommand
|
|
|
|
/// An asynchronous read command.
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AsyncReadCommand(void* buffer, int length);
|
|
|
|
/// Create an AsyncReadCommand for reading up to length
|
|
|
|
/// bytes into the given buffer.
|
|
|
|
|
|
|
|
void* buffer() const;
|
|
|
|
/// Returns the buffer's address.
|
|
|
|
|
|
|
|
int length() const;
|
|
|
|
/// Returns the buffer's size.
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int executeImpl(AsyncIOChannel& channel);
|
|
|
|
~AsyncReadCommand();
|
|
|
|
|
|
|
|
private:
|
|
|
|
AsyncReadCommand();
|
|
|
|
|
|
|
|
void* _buffer;
|
|
|
|
int _length;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Foundation_API AsyncBufferedReadCommand: public AsyncReadCommand
|
|
|
|
/// An asynchronous read command. In contrast to
|
|
|
|
/// AsyncReadCommand, which requires an externally supplied
|
|
|
|
/// buffer that must be valid until the command completes,
|
|
|
|
/// AsyncBufferedReadCommand maintains an internal buffer.
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AsyncBufferedReadCommand(int length);
|
|
|
|
/// Create an AsyncReadCommand for reading up to length
|
|
|
|
/// bytes into an internal buffer.
|
|
|
|
///
|
|
|
|
/// The buffer can be accessed via the buffer() member
|
|
|
|
/// function inherited from AsyncReadCommand.
|
|
|
|
|
|
|
|
protected:
|
|
|
|
~AsyncBufferedReadCommand();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Foundation_API AsyncSeekCommand: public AsyncIOCommand
|
|
|
|
/// An asynchronous seek command.
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AsyncSeekCommand(std::streamoff off, std::ios::seekdir dir = std::ios::beg);
|
|
|
|
/// Creates an AsyncSeekCommand for setting the current read/write position.
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int executeImpl(AsyncIOChannel& channel);
|
|
|
|
~AsyncSeekCommand();
|
|
|
|
|
|
|
|
private:
|
|
|
|
AsyncSeekCommand();
|
|
|
|
|
|
|
|
std::streamoff _off;
|
|
|
|
std::ios::seekdir _dir;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// inlines
|
|
|
|
//
|
|
|
|
inline AsyncIOCommand::State AsyncIOCommand::state() const
|
|
|
|
{
|
|
|
|
return _state;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline int AsyncIOCommand::result() const
|
|
|
|
{
|
|
|
|
return _result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline const Exception* AsyncIOCommand::exception() const
|
|
|
|
{
|
|
|
|
return _pException;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool AsyncIOCommand::succeeded() const
|
|
|
|
{
|
|
|
|
return _state == CMD_COMPLETED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool AsyncIOCommand::failed() const
|
|
|
|
{
|
|
|
|
return _state == CMD_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline const void* AsyncWriteCommand::buffer() const
|
|
|
|
{
|
|
|
|
return _buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline int AsyncWriteCommand::length() const
|
|
|
|
{
|
|
|
|
return _length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void* AsyncReadCommand::buffer() const
|
|
|
|
{
|
|
|
|
return _buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline int AsyncReadCommand::length() const
|
|
|
|
{
|
|
|
|
return _length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Poco
|
|
|
|
|
|
|
|
|
|
|
|
#endif // Foundation_AsyncIOCommand_INCLUDED
|