3 Star 2 Fork 5

张远浩/call_demo_server

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
BaseSocket.h 2.00 KB
一键复制 编辑 原始数据 按行查看 历史
zhangyuanhao 提交于 2015-08-15 22:38 . [MOD] 修改实现
/*
* a wrap for non-block socket class for Windows, LINUX and MacOS X platform
*/
#ifndef __SOCKET_H__
#define __SOCKET_H__
#include "ostype.h"
#include "util.h"
enum
{
SOCKET_STATE_IDLE,
SOCKET_STATE_LISTENING,
SOCKET_STATE_CONNECTING,
SOCKET_STATE_CONNECTED,
SOCKET_STATE_CLOSING
};
class CBaseSocket : public CRefObject
{
public:
CBaseSocket();
virtual ~CBaseSocket();
SOCKET GetSocket() { return m_socket; }
void SetSocket(SOCKET fd) { m_socket = fd; }
void SetState(uint8_t state) { m_state = state; }
void SetCallback(callback_t callback) { m_callback = callback; }
void SetCallbackData(void* data) { m_callback_data = data; }
void SetRemoteIP(char* ip) { m_remote_ip = ip; }
void SetRemotePort(uint16_t port) { m_remote_port = port; }
void SetSendBufSize(uint32_t send_size);
void SetRecvBufSize(uint32_t recv_size);
const char* GetRemoteIP() { return m_remote_ip.c_str(); }
uint16_t GetRemotePort() { return m_remote_port; }
const char* GetLocalIP() { return m_local_ip.c_str(); }
uint16_t GetLocalPort() { return m_local_port; }
public:
int Listen(
const char* server_ip,
uint16_t port,
callback_t callback,
void* callback_data);
net_handle_t Connect(
const char* server_ip,
uint16_t port,
callback_t callback,
void* callback_data);
int Send(void* buf, int len);
int Recv(void* buf, int len);
int Close();
public:
void OnRead();
void OnWrite();
void OnClose();
private:
int _GetErrorCode();
bool _IsBlock(int error_code);
void _SetNonblock(SOCKET fd);
void _SetReuseAddr(SOCKET fd);
void _SetNoDelay(SOCKET fd);
void _SetAddr(const char* ip, const uint16_t port, sockaddr_in* pAddr);
void _AcceptNewSocket();
private:
string m_remote_ip;
uint16_t m_remote_port;
string m_local_ip;
uint16_t m_local_port;
callback_t m_callback;
void* m_callback_data;
uint8_t m_state;
SOCKET m_socket;
};
CBaseSocket* FindBaseSocket(net_handle_t fd);
#endif
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/bluefoxah/call_demo_server.git
git@gitee.com:bluefoxah/call_demo_server.git
bluefoxah
call_demo_server
call_demo_server
master

搜索帮助