/*
* casocklib - An asynchronous communication library for C++
* ---------------------------------------------------------
* Copyright (C) 2010 Leandro Costa
*
* This file is part of casocklib.
*
* casocklib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* casocklib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with casocklib. If not, see <http://www.gnu.org/licenses/>.
*/
/*!
* \file casock/proactor/asio/base/SocketChannel.h
* \brief [brief description]
* \author Leandro Costa
* \date 2010
*
* $LastChangedDate$
* $LastChangedBy$
* $Revision$
*/
#ifndef __CASOCKLIB__CASOCK_PROACTOR_ASIO_BASE__SOCKET_CHANNEL_H_
#define __CASOCKLIB__CASOCK_PROACTOR_ASIO_BASE__SOCKET_CHANNEL_H_
#include <asio.hpp>
#include "casock/base/CASClosedConnectionException.h"
namespace casock {
namespace proactor {
namespace asio {
namespace base {
class AsyncProcessor;
class SocketChannel
{
protected:
SocketChannel (casock::proactor::asio::base::AsyncProcessor& rAsyncProcessor);
virtual ~SocketChannel ()
{
close ();
}
private:
/*!
* asynchronous read:
*
* ReadHandler is a handler in the same format expected by ::asio::async_read,
* i.e., ::boost::bind (&Class::method, object, param1, param2, ...),
* ex.: ::boost::bind (&MySocketChannel::my_handle, this, ::asio::placeholders::error)
*/
template<typename ReadHandler>
void asyncRead (char* buffer, const size_t& size, ReadHandler handler);
/*!
* asynchronous read some:
*
* ReadHandler is a handler in the same format expected by ::asio::ip::tcp::socket::async_read_some
* i.e., ::boost::bind (&Class::method, object, param1, param2, ...),
* ex.: ::boost::bind (&MySocketChannel::my_handle, this, ::asio::placeholders::error, ::asio::placeholders::bytes_transferred)
*/
template<typename ReadHandler>
void asyncReadSome (char* buffer, const size_t& size, ReadHandler handler);
/*!
* asynchronous write:
*
* WriteHandler is a handler in the same format expected by ::asio::async_write,
* i.e., ::boost::bind (&Class::method, object, param1, param2, ...),
* ex.: ::boost::bind (&MySocketChannel::my_handle, this, ::asio::placeholders::error)
*/
template<typename WriteHandler>
void asyncWrite (const char* buffer, const size_t& size, WriteHandler handler);
public:
/*!
* read data of specific type
*/
template<typename _Type, typename ReadHandler>
void read (_Type& buffer, ReadHandler handler);
/*!
* read raw data of specific size
*/
template<typename ReadHandler>
void read (char* buffer, const size_t& size, ReadHandler handler);
/*!
* read some raw data into a maximum size buffer
*/
template<typename ReadHandler>
void readsome (char* buffer, const size_t& size, ReadHandler handler);
/*!
* write data of specific type
*/
template<typename _Type, typename WriteHandler>
void write (const _Type& buffer, WriteHandler handler);
/*!
* write raw data of specific size
*/
template<typename WriteHandler>
void write (const char* buffer, const size_t& size, WriteHandler handler);
protected:
virtual void close () { m_socket.close (); }
protected:
::asio::ip::tcp::socket& socket () { return m_socket; }
public:
const bool isConnected () const { return m_socket.is_open (); }
protected:
::asio::ip::tcp::socket m_socket;
};
/*
* template definitions
*/
template<typename ReadHandler>
void SocketChannel::asyncRead (char* buffer, const size_t& size, ReadHandler handler)
{
if (m_socket.is_open ())
{
::asio::async_read (
m_socket, ::asio::buffer (buffer, size),
::asio::transfer_at_least (size),
handler
);
}
else
{
throw (casock::base::CASClosedConnectionException ("socket is not open"));
}
}
template<typename ReadHandler>
void SocketChannel::asyncReadSome (char* buffer, const size_t& size, ReadHandler handler)
{
if (m_socket.is_open ())
{
m_socket.async_read_some (
::asio::buffer (buffer, size),
handler
);
}
else
{
throw (casock::base::CASClosedConnectionException ("socket is not open"));
}
}
template<typename WriteHandler>
void SocketChannel::asyncWrite (const char* buffer, const size_t& size, WriteHandler handler)
{
if (m_socket.is_open ())
{
::asio::async_write (
m_socket,
::asio::buffer (buffer, size),
handler
);
}
else
{
throw (casock::base::CASClosedConnectionException ("socket is not open"));
}
}
template<typename _Type, typename ReadHandler>
void SocketChannel::read (_Type& buffer, ReadHandler handler)
{
asyncRead (reinterpret_cast<char*> (&buffer), sizeof (_Type), handler);
}
template<typename ReadHandler>
void SocketChannel::read (char* buffer, const size_t& size, ReadHandler handler)
{
asyncRead (buffer, size, handler);
}
template<typename ReadHandler>
void SocketChannel::readsome (char* buffer, const size_t& size, ReadHandler handler)
{
asyncReadSome (buffer, size, handler);
}
template<typename _Type, typename WriteHandler>
void SocketChannel::write (const _Type& buffer, WriteHandler handler)
{
asyncWrite (reinterpret_cast<const char*> (&buffer), sizeof (_Type), handler);
}
template<typename WriteHandler>
void SocketChannel::write (const char* buffer, const size_t& size, WriteHandler handler)
{
asyncWrite (buffer, size, handler);
}
}
}
}
}
#endif // __CASOCKLIB__CASOCK_PROACTOR_ASIO_BASE__SOCKET_CHANNEL_H_
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
casock c++ RPC 框架 (907个子文件)
all-wcprops 3KB
all-wcprops 3KB
all-wcprops 3KB
all-wcprops 2KB
all-wcprops 2KB
all-wcprops 2KB
all-wcprops 2KB
all-wcprops 2KB
all-wcprops 1KB
all-wcprops 1KB
all-wcprops 1KB
all-wcprops 1KB
all-wcprops 1KB
all-wcprops 841B
all-wcprops 824B
all-wcprops 797B
all-wcprops 721B
all-wcprops 698B
all-wcprops 490B
all-wcprops 486B
all-wcprops 480B
all-wcprops 465B
all-wcprops 438B
all-wcprops 434B
all-wcprops 420B
all-wcprops 416B
all-wcprops 355B
all-wcprops 345B
all-wcprops 335B
all-wcprops 329B
all-wcprops 324B
all-wcprops 317B
all-wcprops 313B
all-wcprops 210B
all-wcprops 210B
all-wcprops 208B
all-wcprops 204B
all-wcprops 204B
all-wcprops 202B
all-wcprops 202B
all-wcprops 202B
all-wcprops 199B
all-wcprops 198B
all-wcprops 193B
all-wcprops 192B
all-wcprops 192B
all-wcprops 190B
all-wcprops 189B
all-wcprops 184B
all-wcprops 184B
all-wcprops 184B
all-wcprops 180B
all-wcprops 172B
all-wcprops 170B
Makefile.am 2KB
Makefile.am 1KB
Makefile.am 767B
Makefile.am 765B
Makefile.am 609B
Makefile.am 598B
Makefile.am 561B
Makefile.am 526B
Makefile.am 519B
Makefile.am 485B
Makefile.am 482B
Makefile.am 451B
Makefile.am 437B
Makefile.am 424B
Makefile.am 383B
Makefile.am 365B
Makefile.am 301B
Makefile.am 297B
Makefile.am 277B
Makefile.am 263B
Makefile.am 246B
Makefile.am 242B
Makefile.am 232B
Makefile.am 218B
Makefile.am 202B
Makefile.am 160B
Makefile.am 124B
Makefile.am 111B
Makefile.am 111B
Makefile.am 99B
Makefile.am 34B
Makefile.am 33B
Makefile.am 32B
Makefile.am 30B
Makefile.am 30B
Makefile.am 29B
Makefile.am 29B
Makefile.am 29B
Makefile.am 29B
Makefile.am 28B
Makefile.am 26B
Makefile.am 24B
Makefile.am 19B
Makefile.am 19B
Makefile.am 19B
Makefile.am 17B
共 907 条
- 1
- 2
- 3
- 4
- 5
- 6
- 10
资源评论
- hjl19807162015-06-15没有使用,不过一样谢谢
手提羊刀
- 粉丝: 1
- 资源: 25
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功