//
// Copyright (C) 2008 - All Rights Reserved
//
// File: ServicePostman
// Version: 1.0
// Date: 2008-4-17
//
// Purpose:
//
// --------------------------------------------------------------
//
#ifndef ServicePostman_H
#define ServicePostman_H
//
#include <ACE/Asynch_IO.h>
#include <ACE/Atomic_Op.h>
//
class CServicePostman : public ACE_Service_Handler
{
enum {
SleepSeconds = 0,
SleepMicroseconds = 4000,
};
public:
CServicePostman(size_t header_length = 256);
int connect(const ACE_TCHAR *host, u_short port);
int write(const ACE_Message_Block &message_block);
void open(ACE_HANDLE handle, ACE_Message_Block &message_block);
void close();
void handle_read_stream(const ACE_Asynch_Read_Stream::Result &result);
void handle_write_stream(const ACE_Asynch_Write_Stream::Result &result);
protected:
virtual size_t handle_recv(const ACE_Message_Block &message_block) = 0;
~CServicePostman();
int initiate_read_stream(size_t length);
int initiate_write_stream(const ACE_Message_Block &message_block);
void wait();
private:
bool flg_close_;
size_t header_length_;
ACE_Asynch_Read_Stream reader_;
ACE_Asynch_Write_Stream writer_;
ACE_Atomic_Op<ACE_Thread_Mutex, unsigned long> io_count_;
};
//
#endif
/
#include "ServicePostman.h"
//
#include <ACE/OS_NS_unistd.h>
#include <ACE/Message_Block.h>
#include <ACE/Global_Macros.h>
#include <ACE/SOCK_Connector.h>
#include <ACE/OS_NS_sys_socket.h>
//
CServicePostman::CServicePostman(size_t header_length)
: io_count_(0),
flg_close_(false),
header_length_(header_length)
{
ACE_TRACE(ACE_TEXT("CServicePostman(size_t)"));
}
CServicePostman::~CServicePostman()
{
ACE_TRACE(ACE_TEXT("~CServicePostman()"));
if (this->handle() != ACE_INVALID_HANDLE) {
ACE_OS::closesocket(this->handle());
this->handle(ACE_INVALID_HANDLE);
}
wait();
}
int CServicePostman::connect(const ACE_TCHAR *host, u_short port)
{
ACE_TRACE(ACE_TEXT("connect(const ACE_TCHAR *, u_short)"));
ACE_INET_Addr address;
ACE_Time_Value time_out;
ACE_SOCK_Stream stream_sock;
ACE_Message_Block message_block;
ACE_SOCK_Connector connector;
int ret = -1;
if (handle() != ACE_INVALID_HANDLE) {
goto Exit;
}
if (address.set(port, host) == -1) {
goto Exit;
}
time_out.set(1, 0);
if (connector.connect(stream_sock, address, &time_out) == -1) {
goto Exit;
}
open(stream_sock.get_handle(), message_block);
ret = 0;
Exit:
return ret;
}
int CServicePostman::write(const ACE_Message_Block &message_block)
{
ACE_TRACE(ACE_TEXT("write(const ACE_Message_Block &)"));
int ret = -1;
if (handle() == ACE_INVALID_HANDLE) {
goto Exit;
}
ret = initiate_write_stream(message_block);
Exit:
return ret;
}
void CServicePostman::open(ACE_HANDLE handle, ACE_Message_Block &message_block)
{
ACE_TRACE(ACE_TEXT("open(ACE_HANDLE, ACE_Message_Block &)"));
this->handle(handle);
bool succeeded = false;
if (reader_.open(*this, handle(), 0, proactor()) != 0) {
goto Exit;
}
else if (writer_.open(*this, handle(), 0, proactor()) != 0) {
goto Exit;
}
else if (initiate_read_stream(0) == -1) {
goto Exit;
}
succeeded = true;
Exit:
if (!succeeded) {
close();
}
}
void CServicePostman::close()
{
ACE_TRACE(ACE_TEXT("close()"));
flg_close_ = true;
delete this;
}
void CServicePostman::handle_read_stream(const ACE_Asynch_Read_Stream::Result &result)
{
ACE_TRACE(ACE_TEXT("handle_read_stream(const ACE_Asynch_Read_Stream::Result &)"));
ACE_Message_Block &message_block = result.message_block();
if (!result.success() || result.bytes_transferred() == 0) {
goto Exit;
}
initiate_read_stream(handle_recv(message_block));
Exit:
message_block.release();
--io_count_;
if (!flg_close_ && io_count_ == 0) {
close();
}
}
void CServicePostman::handle_write_stream(const ACE_Asynch_Write_Stream::Result &result)
{
ACE_TRACE(ACE_TEXT("handle_write_stream(const ACE_Asynch_Write_Stream::Result &)"));
result.message_block().release();
--io_count_;
}
int CServicePostman::initiate_read_stream(size_t length)
{
ACE_TRACE(ACE_TEXT("initiate_read_stream(size_t)"));
ACE_Message_Block *message_block = 0;
ACE_NEW_NORETURN(message_block, ACE_Message_Block());
int ret = -1;
length = (length == 0) ? header_length_ : length;
if (!message_block || message_block->init(length) == -1) {
goto Exit;
}
ACE_OS::memset(message_block->base(), 0, message_block->size());
if (reader_.read(*message_block, message_block->size()) == -1) {
goto Exit;
}
++io_count_;
ret = 0;
Exit:
return ret;
}
int CServicePostman::initiate_write_stream(const ACE_Message_Block &message_block)
{
ACE_TRACE(ACE_TEXT("initiate_write_stream(const ACE_Message_Block &)"));
ACE_Message_Block *message_block = _message_block.duplicate();
int ret = -1;
if (!message_block || writer_.write(*message_block, message_block->length()) == -1) {
goto Exit;
}
++io_count_;
ret = 0;
Exit:
if (ret == -1) {
if (message_block) {
message_block->release();
message_block = 0;
}
}
return ret;
}
void CServicePostman::wait()
{
ACE_TRACE(ACE_TEXT("wait()"));
ACE_Time_Value time_sleep(SleepSeconds, SleepMicroseconds);
while (io_count_ > 0) {
ACE_OS::sleep(time_sleep);
}
}