mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-26 10:32:56 +01:00
added AsyncIO
This commit is contained in:
79
Foundation/src/AsyncIOChannel.cpp
Normal file
79
Foundation/src/AsyncIOChannel.cpp
Normal file
@@ -0,0 +1,79 @@
|
||||
//
|
||||
// AsyncIOChannel.cpp
|
||||
//
|
||||
// $Id: //poco/Main/Foundation/src/AsyncIOChannel.cpp#1 $
|
||||
//
|
||||
// Library: Foundation
|
||||
// Package: AsyncIO
|
||||
// Module: AsyncIOChannel
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/AsyncIOChannel.h"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
|
||||
|
||||
AsyncIOChannel::AsyncIOChannel():
|
||||
enqueue(this, &AsyncIOChannel::enqueueImpl)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
AsyncIOChannel::~AsyncIOChannel()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
int AsyncIOChannel::enqueueImpl(const AsyncIOCommand::Ptr& pCommand)
|
||||
{
|
||||
AsyncIOCommand::Ptr ptr(pCommand);
|
||||
return ptr->execute(*this);
|
||||
}
|
||||
|
||||
|
||||
int AsyncIOChannel::write(const void* buffer, int length)
|
||||
{
|
||||
throw NotImplementedException("write()");
|
||||
}
|
||||
|
||||
|
||||
int AsyncIOChannel::read(void* buffer, int length)
|
||||
{
|
||||
throw NotImplementedException("read()");
|
||||
}
|
||||
|
||||
|
||||
int AsyncIOChannel::seek(std::streamoff off, std::ios::seekdir dir)
|
||||
{
|
||||
throw NotImplementedException("seek()");
|
||||
}
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
236
Foundation/src/AsyncIOCommand.cpp
Normal file
236
Foundation/src/AsyncIOCommand.cpp
Normal file
@@ -0,0 +1,236 @@
|
||||
//
|
||||
// AsyncIOCommand.cpp
|
||||
//
|
||||
// $Id: //poco/Main/Foundation/src/AsyncIOCommand.cpp#1 $
|
||||
//
|
||||
// Library: Foundation
|
||||
// Package: AsyncIO
|
||||
// Module: AsyncIOCommand
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/AsyncIOCommand.h"
|
||||
#include "Poco/AsyncIOChannel.h"
|
||||
#include "Poco/AsyncIOEvent.h"
|
||||
#include <cstring>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
|
||||
|
||||
//
|
||||
// AsyncIOCommand
|
||||
//
|
||||
|
||||
|
||||
AsyncIOCommand::AsyncIOCommand():
|
||||
_state(CMD_PENDING),
|
||||
_result(0),
|
||||
_pException(0)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
AsyncIOCommand::~AsyncIOCommand()
|
||||
{
|
||||
delete _pException;
|
||||
}
|
||||
|
||||
|
||||
void AsyncIOCommand::wait()
|
||||
{
|
||||
_completed.wait();
|
||||
}
|
||||
|
||||
|
||||
void AsyncIOCommand::wait(long milliseconds)
|
||||
{
|
||||
_completed.wait(milliseconds);
|
||||
}
|
||||
|
||||
|
||||
bool AsyncIOCommand::tryWait(long milliseconds)
|
||||
{
|
||||
return _completed.tryWait(milliseconds);
|
||||
}
|
||||
|
||||
|
||||
int AsyncIOCommand::execute(AsyncIOChannel& channel)
|
||||
{
|
||||
delete _pException;
|
||||
_pException = 0;
|
||||
|
||||
_state = CMD_IN_PROGRESS;
|
||||
try
|
||||
{
|
||||
_result = executeImpl(channel);
|
||||
_state = CMD_COMPLETED;
|
||||
_completed.set();
|
||||
AsyncIOEvent completedEvent(this, &channel, AsyncIOEvent::EV_COMMAND_COMPLETED);
|
||||
commandCompleted(this, completedEvent);
|
||||
channel.commandCompleted(this, completedEvent);
|
||||
return _result;
|
||||
}
|
||||
catch (Exception& exc)
|
||||
{
|
||||
_pException = exc.clone();
|
||||
_state = CMD_FAILED;
|
||||
_completed.set();
|
||||
AsyncIOEvent failedEvent(this, &channel, AsyncIOEvent::EV_COMMAND_FAILED);
|
||||
commandFailed(this, failedEvent);
|
||||
channel.commandFailed(this, failedEvent);
|
||||
throw;
|
||||
}
|
||||
catch (std::exception& exc)
|
||||
{
|
||||
_pException = new Exception(exc.what());
|
||||
_state = CMD_FAILED;
|
||||
_completed.set();
|
||||
AsyncIOEvent failedEvent(this, &channel, AsyncIOEvent::EV_COMMAND_FAILED);
|
||||
commandFailed(this, failedEvent);
|
||||
channel.commandFailed(this, failedEvent);
|
||||
throw;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
_pException = new Exception("Unknown exception");
|
||||
_state = CMD_FAILED;
|
||||
_completed.set();
|
||||
AsyncIOEvent failedEvent(this, &channel, AsyncIOEvent::EV_COMMAND_FAILED);
|
||||
commandFailed(this, failedEvent);
|
||||
channel.commandFailed(this, failedEvent);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// AsyncWriteCommand
|
||||
//
|
||||
|
||||
|
||||
AsyncWriteCommand::AsyncWriteCommand(const void* buffer, int length):
|
||||
_buffer(buffer),
|
||||
_length(length)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
AsyncWriteCommand::~AsyncWriteCommand()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
int AsyncWriteCommand::executeImpl(AsyncIOChannel& channel)
|
||||
{
|
||||
return channel.write(_buffer, _length);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// AsyncBufferedWriteCommand
|
||||
//
|
||||
|
||||
|
||||
AsyncBufferedWriteCommand::AsyncBufferedWriteCommand(const void* buf, int length):
|
||||
AsyncWriteCommand(new char[length], length)
|
||||
{
|
||||
std::memcpy(const_cast<void*>(buffer()), buf, length);
|
||||
}
|
||||
|
||||
|
||||
AsyncBufferedWriteCommand::~AsyncBufferedWriteCommand()
|
||||
{
|
||||
delete [] reinterpret_cast<const char*>(buffer());
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// AsyncReadCommand
|
||||
//
|
||||
|
||||
|
||||
AsyncReadCommand::AsyncReadCommand(void* buffer, int length):
|
||||
_buffer(buffer),
|
||||
_length(length)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
AsyncReadCommand::~AsyncReadCommand()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
int AsyncReadCommand::executeImpl(AsyncIOChannel& channel)
|
||||
{
|
||||
return channel.read(_buffer, _length);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// AsyncBufferedReadCommand
|
||||
//
|
||||
|
||||
|
||||
AsyncBufferedReadCommand::AsyncBufferedReadCommand(int length):
|
||||
AsyncReadCommand(new char[length], length)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
AsyncBufferedReadCommand::~AsyncBufferedReadCommand()
|
||||
{
|
||||
delete [] reinterpret_cast<char*>(buffer());
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// AsyncSeekCommand
|
||||
//
|
||||
|
||||
|
||||
AsyncSeekCommand::AsyncSeekCommand(std::streamoff off, std::ios::seekdir dir):
|
||||
_off(off),
|
||||
_dir(dir)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
AsyncSeekCommand::~AsyncSeekCommand()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
int AsyncSeekCommand::executeImpl(AsyncIOChannel& channel)
|
||||
{
|
||||
return channel.seek(_off, _dir);
|
||||
}
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
88
Foundation/src/AsyncIOEvent.cpp
Normal file
88
Foundation/src/AsyncIOEvent.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
//
|
||||
// AsyncIOEvent.cpp
|
||||
//
|
||||
// $Id: //poco/Main/Foundation/src/AsyncIOEvent.cpp#1 $
|
||||
//
|
||||
// Library: Foundation
|
||||
// Package: AsyncIO
|
||||
// Module: AsyncIOEvent
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/AsyncIOEvent.h"
|
||||
#include "Poco/AsyncIOCommand.h"
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
|
||||
|
||||
AsyncIOEvent::AsyncIOEvent(AsyncIOCommand* pCommand, AsyncIOChannel* pChannel, EventKind what):
|
||||
_pCommand(pCommand),
|
||||
_pChannel(pChannel),
|
||||
_what(what)
|
||||
{
|
||||
poco_check_ptr (pCommand);
|
||||
poco_check_ptr (pChannel);
|
||||
|
||||
_pCommand->duplicate();
|
||||
}
|
||||
|
||||
|
||||
AsyncIOEvent::AsyncIOEvent(const AsyncIOEvent& event):
|
||||
_pCommand(event._pCommand),
|
||||
_pChannel(event._pChannel),
|
||||
_what(event._what)
|
||||
{
|
||||
_pCommand->duplicate();
|
||||
}
|
||||
|
||||
|
||||
AsyncIOEvent::~AsyncIOEvent()
|
||||
{
|
||||
_pCommand->release();
|
||||
}
|
||||
|
||||
|
||||
AsyncIOEvent& AsyncIOEvent::operator = (const AsyncIOEvent& event)
|
||||
{
|
||||
AsyncIOEvent tmp(event);
|
||||
swap(tmp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
void AsyncIOEvent::swap(AsyncIOEvent& event)
|
||||
{
|
||||
std::swap(_pCommand, event._pCommand);
|
||||
std::swap(_pChannel, event._pChannel);
|
||||
std::swap(_what, event._what);
|
||||
}
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
100
Foundation/src/AsyncStreamChannel.cpp
Normal file
100
Foundation/src/AsyncStreamChannel.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
//
|
||||
// AsyncStreamChannel.cpp
|
||||
//
|
||||
// $Id: //poco/Main/Foundation/src/AsyncStreamChannel.cpp#1 $
|
||||
//
|
||||
// Library: Foundation
|
||||
// Package: AsyncIO
|
||||
// Module: AsyncStreamChannel
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/AsyncStreamChannel.h"
|
||||
#include "Poco/Exception.h"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
|
||||
|
||||
AsyncStreamChannel::AsyncStreamChannel(std::istream& istr):
|
||||
_pIstr(&istr),
|
||||
_pOstr(0)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
AsyncStreamChannel::AsyncStreamChannel(std::ostream& ostr):
|
||||
_pIstr(0),
|
||||
_pOstr(&ostr)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
AsyncStreamChannel::AsyncStreamChannel(std::iostream& iostr):
|
||||
_pIstr(&iostr),
|
||||
_pOstr(&iostr)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
AsyncStreamChannel::~AsyncStreamChannel()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
int AsyncStreamChannel::write(const void* buffer, int length)
|
||||
{
|
||||
if (_pOstr)
|
||||
{
|
||||
_pOstr->write(reinterpret_cast<const char*>(buffer), length);
|
||||
return length;
|
||||
}
|
||||
else throw IOException("Cannot write to an input stream");
|
||||
}
|
||||
|
||||
|
||||
int AsyncStreamChannel::read(void* buffer, int length)
|
||||
{
|
||||
if (_pIstr)
|
||||
return static_cast<int>(_pIstr->read(reinterpret_cast<char*>(buffer), length).gcount());
|
||||
else
|
||||
throw IOException("Cannot read from an output stream");
|
||||
}
|
||||
|
||||
|
||||
int AsyncStreamChannel::seek(std::streamoff off, std::ios::seekdir dir)
|
||||
{
|
||||
if (_pIstr)
|
||||
_pIstr->seekg(off, dir);
|
||||
if (_pOstr)
|
||||
_pOstr->seekp(off, dir);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
Binary file not shown.
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// pocomsg.mc[.h]
|
||||
//
|
||||
// $Id: //poco/Main/Foundation/src/pocomsg.mc#7 $
|
||||
// $Id: //poco/Main/Foundation/src/pocomsg.h#26 $
|
||||
//
|
||||
// The Poco message source/header file.
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user