mirror of
https://github.com/pocoproject/poco.git
synced 2024-12-13 18:45:10 +01:00
0c65fa6295
- ActiveMethod::operator () default argument value (convenient for arguments of Poco::Void type)
243 lines
6.4 KiB
C++
243 lines
6.4 KiB
C++
//
|
|
// IOChannel.h
|
|
//
|
|
// $Id: //poco/svn/Foundation/include/Poco/IOChannel.h#2 $
|
|
//
|
|
// Library: Foundation
|
|
// Package: IO
|
|
// Module: IOChannel
|
|
//
|
|
// Definition of the IOChannel 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_IOChannel_INCLUDED
|
|
#define Foundation_IOChannel_INCLUDED
|
|
|
|
|
|
#include "Poco/Foundation.h"
|
|
#include "Poco/IOChannelConfig.h"
|
|
#include "Poco/RefCountedObject.h"
|
|
#include "Poco/AutoPtr.h"
|
|
|
|
|
|
namespace Poco {
|
|
|
|
|
|
class Foundation_API IOChannel: public RefCountedObject
|
|
/// IOChannel supports I/O operations on streams or other input/output facilities.
|
|
///
|
|
/// IOChannel supports blocking (default) and non-blocking read and write operations.
|
|
{
|
|
public:
|
|
typedef AutoPtr<IOChannelConfig> ConfigPtr;
|
|
|
|
IOChannel(const std::string& name = "");
|
|
/// Creates the IOChannel.
|
|
|
|
IOChannel(IOChannelConfig* pConfig, const std::string& name = "");
|
|
/// Creates the IOChannel with specified configuration.
|
|
|
|
virtual ~IOChannel();
|
|
/// Destroys the IOChannel.
|
|
|
|
virtual void open() = 0;
|
|
/// Opens the channel.
|
|
|
|
virtual void close() = 0;
|
|
/// Closes the channel.
|
|
|
|
std::string& read(std::string& buffer, int length = 0);
|
|
/// Read up to length bytes into the supplied buffer.
|
|
/// If length is 0, the channel implementation must provide
|
|
/// support for the termination of transmission.
|
|
/// Also, in case when length is 0 and the channel implementation
|
|
/// provides the buffer length and termination character
|
|
/// information, it also allocates the buffer. The buffer,
|
|
/// however, is always freed in this function, regardless of where
|
|
/// it was allocated. See readData(char*&) for more information.
|
|
|
|
const std::string& read(int length);
|
|
/// Read up to length bytes and return the reference to internal buffer.
|
|
|
|
int read(char* pReadBuf, int length);
|
|
/// Reads a string of characters from the channel.
|
|
|
|
int write(char c);
|
|
/// Writes a character to the channel.
|
|
|
|
int write(const char* buffer, int length);
|
|
/// Write length bytes from buffer.
|
|
/// Returns the number of bytes written.
|
|
|
|
int write(const std::string& data);
|
|
/// Writes a string of characters to the channel.
|
|
|
|
char read();
|
|
/// Reads one character from the channel.
|
|
|
|
const std::string& name() const;
|
|
/// Returns the channel name.
|
|
|
|
virtual void setTimeout(int timeoutMS);
|
|
/// Sets timeout in milliseconds.
|
|
|
|
virtual void setBlocking();
|
|
/// Sets blocking operation.
|
|
|
|
virtual void setNonblocking(int timeoutMS);
|
|
/// Sets non-blocking operation by calling setTimeout().
|
|
|
|
int getTimeout() const;
|
|
/// Returns timeout in milliseconds.
|
|
|
|
bool isBlocking() const;
|
|
/// Returns true if operation is blocking, false otherwise.
|
|
|
|
protected:
|
|
IOChannelConfig& config();
|
|
/// Returns the reference to the configuration for this cahannel.
|
|
|
|
virtual void init();
|
|
/// (Re)initializes the internal channel configuration.
|
|
/// Does nothing in this implementation.
|
|
|
|
virtual int readData(char* pReadBuf, int length) = 0;
|
|
/// Reads length bytes and puts them in the buffer.
|
|
/// Must be implemented by the inheriting class.
|
|
|
|
virtual int readData(char*& pReadBuf) = 0;
|
|
/// Reads an unspecified amount of bytes and places them in the buffer.
|
|
/// Properly behaved implementation of this function reads bytes and places
|
|
/// them in the buffer. Buffer is allocated by the implementation as
|
|
/// needed. Implementation must provide a way to terminate the communication
|
|
/// session.
|
|
/// Important: this function is called from read(std::string, int length) when
|
|
/// the length is zero. The buffer allocated in the implementation of this function
|
|
/// is always freed in the caller function.
|
|
|
|
virtual int writeData(const char* buffer, int length) = 0;
|
|
/// Writes length bytes from buffer to the target.
|
|
/// Must be implemented by the inheriting class.
|
|
|
|
private:
|
|
IOChannel(const IOChannel&);
|
|
IOChannel& operator = (const IOChannel&);
|
|
|
|
std::string _name;
|
|
std::string _buffer;
|
|
ConfigPtr _pConfig;
|
|
};
|
|
|
|
|
|
//
|
|
// inlines
|
|
//
|
|
|
|
inline IOChannelConfig& IOChannel::config()
|
|
{
|
|
return *_pConfig;
|
|
}
|
|
|
|
|
|
inline void IOChannel::init()
|
|
{
|
|
}
|
|
|
|
|
|
inline const std::string& IOChannel::name() const
|
|
{
|
|
return _name;
|
|
}
|
|
|
|
|
|
inline const std::string& IOChannel::read(int length)
|
|
{
|
|
return read(_buffer, length);
|
|
}
|
|
|
|
|
|
inline int IOChannel::read(char* pReadBuf, int length)
|
|
{
|
|
return readData(pReadBuf, length);
|
|
}
|
|
|
|
|
|
inline int IOChannel::write(char c)
|
|
{
|
|
return writeData(&c, 1);
|
|
}
|
|
|
|
|
|
inline int IOChannel::write(const std::string& data)
|
|
{
|
|
return writeData(data.data(), static_cast<int>(data.size()));
|
|
}
|
|
|
|
|
|
inline int IOChannel::write(const char* buffer, int length)
|
|
{
|
|
return writeData(buffer, length);
|
|
}
|
|
|
|
|
|
inline void IOChannel::setTimeout(int timeoutMS)
|
|
{
|
|
_pConfig->setTimeout(timeoutMS);
|
|
}
|
|
|
|
|
|
inline void IOChannel::setBlocking()
|
|
{
|
|
_pConfig->setBlocking();
|
|
}
|
|
|
|
|
|
inline void IOChannel::setNonblocking(int timeoutMS)
|
|
{
|
|
setTimeout(timeoutMS);
|
|
}
|
|
|
|
|
|
inline int IOChannel::getTimeout() const
|
|
{
|
|
return _pConfig->getTimeout();
|
|
}
|
|
|
|
|
|
inline bool IOChannel::isBlocking() const
|
|
{
|
|
return _pConfig->isBlocking();
|
|
}
|
|
|
|
|
|
} // namespace Poco
|
|
|
|
|
|
#endif // Foundation_IOChannel_INCLUDED
|