一個簡單的socket封裝

來源:互聯網
上載者:User

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.