📄 usrsocklib.h
字号:
/* 库定义: 为上层应用提供c-s模式的Server的管理功能, 为上层屏蔽具体的通信细节及Server管理细节,
* 使上层更快的完成其关心的功能(从原先的SockTransLayer继承而来)
* 创建日期: 2006/10/11
* 定版日期: 2006/10/11
* 版本: V1.03
* 编写人: 陈幸幸
* 修改日志:
* 2006/10/15> STL_OpenServer添加了参数taskStack
* 2006/10/15> 添加了IOCTL命令STL_IOCTL_GET_IDLE_INT及STL_IOCTL_SET_IDLE_INT
* 2006/10/20> 修正了函数STL_Ioctl中的一个BUG
*/
#ifndef ___USR_SOCK_LIB___
#define ___USR_SOCK_LIB___
#define ___SOCK_TRANS_LAYER_H___
#include "../commonlib/pdefine.h"
#include "../commonlib/DataStruct.h"
/* 相关的宏定义, 以下这些参数并不是一定的, 用户可根据实际情况作出修改 */
#define STL_MODULE_NAME "STL" /* 模块名 */
#define STL_MAX_SERVER_NUMBER (2) /* 最大支持服务端数(默认) */
#define STL_MAX_INPUT_BUFFER (1024) /* 最大接收缓冲区 */
#define STL_MAX_OUTPUT_BUFFER (1024) /* 最大发送缓冲区 */
/* 消息定义 */
typedef enum
{
STL_MSG_ACCEPT = 1, /* 有新的连接 */
STL_MSG_ABORT = 2, /* 连接被中断 */
STL_MSG_ERROR = 3, /* 连接异常出错 */
STL_MSG_READ = 4, /* 可读 */
STL_MSG_IDLE = 5 /* 空闲(可写) */
}STL_MESSAGE;
/* 回调函数定义 */
typedef STATUS (*STL_CallBack)( int handle, STL_MESSAGE msg, int p0, int p1 );
/* 传输相关的结构定义 */
/* 连接项 */
typedef struct
{
SOCKET sock; /* 连接套接字 */
struct sockaddr_in addr; /* 连接对方的socket地址 */
struct
{
UINT8 accepted : 1; /* 是否已被应用层接受 */
UINT8 flag : 7; /* 连接状态 */
}state;
struct timeval tmo; /* 等待网络消息的超时值 */
}STL_Connection;
/* 提供给外部的连接信息 */
typedef STL_Connection STL_extConn;
/* 连接状态定义 */
#define STL_CONN_STATE_INVALID (0) /* 无效 */
#define STL_CONN_STATE_CONNECTED (1) /* 已连接, 准备添加至连接表 */
#define STL_CONN_STATE_NORMAL (2) /* 正常 */
#define STL_CONN_STATE_ERROR (3) /* 出错 */
#define STL_CONN_STATE_ABORT (4) /* 中断 */
/* 服务器定义 */
typedef struct
{
char name[32]; /* 服务器名称 */
SOCKET sock; /* 监听套接字 */
struct sockaddr_in addr; /* 监听地址 */
UINT8 state :3; /* 服务器状态 */
UINT8 bShareTask :1; /* 是否共享处理任务 */
UINT8 bAutoAccept:1; /* 是否具有自行接受连接请求的权力 */
UINT8 bKeepAlive :1; /* 是否需要keepalive维持 */
USHORT max_conn; /* 最大连接数 */
UINT32 tIdelMax; /* 空闲单位时间(us) */
UINT8 taskPriority; /* 任务优先级 */
int taskStack; /* 任务堆栈大小 */
STL_CallBack callback; /* 回调函数 */
MEMORIZER_ID conn_list; /* 连接表 */
struct timeval tmo; /* 等待网络消息的超时值 */
}STL_Server;
/* 服务器状态定义 */
#define STL_SERVER_STATE_INVALID (0) /* 无效 */
#define STL_SERVER_STATE_NORMAL (1) /* 正常 */
#define STL_SERVER_STATE_ERROR (2) /* 出错 */
#define STL_SERVER_STATE_CLOSE (3) /* 关闭 */
/* 格式化连接句柄 */
#define STL_MAKE_CLIENT_HANDLE( s, c ) \
( ((UINT32)(s << 24)) | ((UINT32)(c & 0x00FFFFFF)) )
#define STL_GET_SERVER_HANDLE( h ) (h >> 24)
#define STL_GET_CLIENT_HANDLE( h ) (h & 0x00FFFFFF)
/* IOCTL 命令码定义 */
#define STL_IOCTL_GET_IDLE_INT (10000) /* 获取空闲间隔(us) */
#define STL_IOCTL_SET_IDLE_INT (10001) /* 设置空闲间隔(us) */
/************************************* 接口函数 *************************************/
#ifdef __cplusplus
extern "C" {
#endif
/* 初始化传输层 */
BOOL STL_Open( UINT8 max_servers );
/* 关闭传输层 */
void STL_Close( void );
/* 启动一个服务器 */
int STL_OpenServer( const char *server_name,
const char *IP,
USHORT PORT,
USHORT max_conn,
BOOL bShareTask,
BOOL bAutoAccept,
BOOL bKeepAlive,
UINT32 tIdelMax,
UINT8 taskPriority,
int taskStack,
STL_CallBack callback
);
/* 关闭一个服务器 */
void STL_CloseServer( int hServer );
/* 重启一个服务器 */
int STL_RestartServer( int hServer );
/* 接受连接请求 */
int STL_Accept( int hServer );
/* 中断连接 */
void STL_Abort( int hConn );
/* 读取数据 */
int STL_Read( int hConn, /* 连接句柄 */
void *in_buf, /* 输入缓冲区 */
int in_buf_size /* 输入缓冲区容量(byte) */
);
/* 发送数据 */
BOOL STL_Write( int hConn, /* 连接句柄 */
void *out_buf, /* 输出缓冲区 */
int out_buf_size /* 输出缓冲区中的实际字节数(byte) */
);
/* 广播即发送给所有连接者(不是UDP级的广播) */
void STL_Broadcast( int hServer, /* 服务器句柄 */
void *out_buf, /* 输出缓冲区 */
int out_buf_size /* 输出缓冲区中的实际字节数(byte) */
);
/* 函数功能: ioctl
* 参数说明:
* 返回值:
*/
int STL_Ioctl( int hConn, int cmd, int arg );
/* 获取连接信息 */
BOOL STL_GetConnection( int hConn, /* 连接句柄 */
STL_extConn *pConn /* 用来存放连接信息 */
);
/* 获取服务器信息 */
BOOL STL_GetServer( int hServer, /* 服务器句柄 */
STL_Server *pServer /* 用来存放服务器信息 */
);
#ifdef __cplusplus
}
#endif
#endif /* ndef ___USR_SOCK_LIB___ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -