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 portstruct ssso* ssso_new(const char* host_name, int port);//close socketvoid ssso_free(struct ssso* so);//update socket, write data from buffer to socket, and if there is data readable, it return 1int ssso_check(struct ssso* so);//peek whether has data of size in read buffer, return data or NULLvoid* ssso_peek(struct ssso* so, size_t size);//discard data of size in read buffer, mainly use after geting integrate message data using ssso_peekvoid ssso_discard(struct ssso* so, size_t size);//write data to buffer, after do ssso_check, the data may be send through socketvoid 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