一个简单的socket封装

SSSocket, 意思是simple stupid socket interface


目的:

编写一个易于消息数据读写、非阻塞、跨平台的socket封装。目前用于手机网游客户端


优点:

1.采用非阻塞的连接和IO,即使是单线程也不会阻塞UI

2.提供读写buffer

3.对完整消息读取友好的API


缺点:

1.单线程使用方式,目前考虑是游戏的每一帧,非阻塞的读写一次数据。与多线程或者阻塞IO方式相比,读写速度上差了一些

2.buffer使用的只是原生数组。从缓冲区取出数据后,缓冲区尾部未读取数据会用内存拷贝的方式移动到头部


接口:

 

//create a socket , ready to connect to host define by host_name and port

struct ssso* ssso_new(const char* host_name, int port);



//close socket

void  ssso_free(struct ssso* so);



//update socket, write data from buffer to socket, and if there is data readable, it return 1

int   ssso_check(struct ssso* so);



//peek whether has data of size in read buffer, return data or NULL

void* ssso_peek(struct ssso* so, size_t size);



//discard data of size in read buffer, mainly use after geting integrate message data using ssso_peek

void  ssso_discard(struct ssso* so, size_t size);



//write data to buffer, after do ssso_check, the data may be send through socket

void  ssso_write(struct ssso* so, const void* data, size_t size);



enum SSSO_STATUS ssso_status(struct ssso* so);


源代码地址:

https://github.com/wtyqm/SSSocket


参考:

API设计参考了云风的mread

http://blog.codingnow.com/2012/04/mread.html


 

你可能感兴趣的:(socket)