一个自己用于处理基本网络操作的头文件。希望大家能多提自己的想法和意见。谢谢!(内容其实很少,主要是注释比较多,所以,看起来比较长。)
1.数据类型
typedef signed char S8; /// signed char (8-bit integer)
typedef unsigned char U8; /// unsigned char (8-bit integer)
typedef signed short S16; /// signed 16-bit short integer
typedef unsigned short U16; /// unsigned 16-bit short integer
typedef signed int S32; /// signed 32-bit integer
typedef unsigned int U32; /// unsigned 32-bit integer
// typedef long long S64; /// signed 64-bit integer
// typedef unsigned long long U64; /// unsigned 64-bit integer
typedef float F32; /// 32-bit float
typedef double F64; /// 64-bit float
2.通信的数据
/**
* 网络数据包
* 提供打包、解包网络数据的接口
*/
class netPacket
{
public:
/**
* 生成临时数据包
*/
netPacket( void );
/**
* 生成数据包
* @para stamp 网络邮标
*/
explicit netPacket( U8 stamp );
~netPacket( void );
/**
* 生成数据包
* @para packet 用以生成新包的旧数据
*/
netPacket( const netPacket &packet );
netPacket &operator=( const netPacket &packet );
/**
* 打包数据块
* @return 无
* @para data 数据块
* @para bytes 数据块大小
*/
void pack( const void *data,U32 bytes );
/**
* 解包数据块
* @return 解包后的数据
* @para data 解包后的数据
* @para bytes 数据块大小
*/
void *unpack( void *data,U32 bytes );
/**
* 打包基本数据
* @return 无
* @para data 数据
*/
void pack( S8 data );
void pack( U8 data );
void pack( S16 data );
void pack( U16 data );
void pack( S32 data );
void pack( U32 data );
void pack( F32 data );
void pack( F64 data );
void pack( const char *data );
/**
* 解包基本数据
* @return 数据
* @para data 解得的数据
*/
S8 unpack( S8 &data );
U8 unpack( U8 &data );
S16 unpack( S16 &data );
U16 unpack( U16 &data );
S32 unpack( S32 &data );
U32 unpack( U32 &data );
F32 unpack( F32 &data );
F64 unpack( F64 &data );
char *unpack( char *data );
/**
* 打包数组数据
* @return 无
* @para arr 数组
* @para size 数据组大小
*/
void pack( const S8 arr[],U32 size );
void pack( const U8 arr[],U32 size );
void pack( const S16 arr[],U32 size );
void pack( const U16 arr[],U32 size );
void pack( const S32 arr[],U32 size );
void pack( const U32 arr[],U32 size );
void pack( const F32 arr[],U32 size );
void pack( const F64 arr[],U32 size );
/**
* 解包数组数据
* @return 数组数据块
* @para buf 解出的数组
* @para size 数组大小
*/
S8 *unpack( S8 buf[],U32 size );
U8 *unpack( U8 buf[],U32 size );
S16 *unpack( S16 buf[],U32 size );
U16 *unpack( U16 buf[],U32 size );
S32 *unpack( S32 buf[],U32 size );
U32 *unpack( U32 buf[],U32 size );
F32 *unpack( F32 buf[],U32 size );
F64 *unpack( F64 buf[],U32 size );
/**
* 数据包的大小
* @return 字节数
*/
U32 size( void ) const;
/**
* 获取数据包数据,存入指定数据块区
* @return 获得的数据块
* @para buf 数据块
* @para size 数据的大小
* @para off 取数据的偏移
*/
char *datas( char buf[],U32 size,U32 off = 0 ) const;
/**
* 数据包的网络邮标
* @return 邮标
*/
U8 stamp( void ) const;
private:
class netPacketImpl;
netPacketImpl *m_Impl;
};
3.网络处理
/**
* 网络办公室
* 提供网络相关功能的接口:网络包发送、接收
*/
class netOffice
{
public:
/**
* 连接到指定IP主机的网络通道
* @return 无
* @para ip 主机的IP地址
* @para port 主机的收听端口
*/
void connect( U32 ip,U16 port );
/**
* 连接建立通道
* @return 连接完毕否
* @para channel 建立的通道
* @para closed 通道是否已关闭
*/
bool connected( U32 &channel,bool &closed );
/**
* 在指定端口开始收听
* @return 实际的端口
* @para port 收听端口
*/
U16 listen( U16 port );
/**
* 接受建立的通道
* @return 建立成功否
* @para port 服务器收听端口
* @para channel 建立的通道
*/
bool accepted( U16 port,U32 &channel );
/**
* 关闭通道
* @return 无
* @para channel 要关闭的通道
*/
void close( U32 channel );
/**
* 已关闭的通道
* @return 有已关闭的没
* @ channel 已关闭的通道
*/
bool closed( U32 &channel );
/**
* 发送网络包给指定网络组
* @return 无
* @para channel 送给哪个通道
* @para packet 网络包
* @para group 网络组
*/
void post( U32 channel,U16 group,const netPacket &packet );
/**
* 接收网络包
* @return 是否有网络包
* @para channel 取哪个通道的
* @para group 网络组
* @para packet 接收的网络包
*/
bool fetch( U32 channel,U16 group,netPacket &packet );
/**
* 通道对应的远程IP地址
* @return IP地址
* @para channel 哪个通道的
*/
U32 remoteIP( U32 channel );
/**
* 申请网络办公室
* @return 办公室实例
*/
static netOffice &instance( void );
/**
* 转换整数ip为字符串形式
* @return ip字符串
* @para 整数ip
*/
static const char *ip_str( U32 ip );
/**
* 转换字符串ip为整数形式
* @return 整数ip
* @para ip字符串
*/
static U32 ip_int( const char * ip );
private:
netOffice( void );
~netOffice( void );
class netImpl;
netImpl *m_Impl;
};