Socke基类:
包括Socket的初始化,主要是windows上,在android上就不需要了
如果平台为windows,则需要初始化
- SocketBase::SocketBase()
- {
- _bInitSuccess = false;
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
- WORD wVersionRequested;
- wVersionRequested = MAKEWORD(2, 0);
- WSADATA wsaData;
- int nRet = WSAStartup(wVersionRequested, &wsaData);
- if (nRet != 0)
- {
- fprintf(stderr, "Initilize Error!\n");
- return;
- }
- _bInitSuccess = true;
- #endif
- }
当然析构时也要释放资源
- SocketBase::~SocketBase()
- {
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
- if (_bInitSuccess)
- {
- WSACleanup();
- }
- #endif
- }
因为windows的socket()返回的socket句柄为SOCKET(UINT_PTR)
与android的socket()返回的socket句柄int, 类型不一样,所以都定义为HSocket
对于服务端和客户端都有关闭连接,所以基类就实现共同的。
- void SocketBase::closeConnect(HSocket socket)
- {
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
- close(socket);
- #elif (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
- closesocket(socket);
- #endif
- }
当执行socket()出错时,windows返回SOCKET_ERROR, android 返回<0,
所以实现error()
- bool SocketBase::error(HSocket socket)
- {
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
- return socket == SOCKET_ERROR;
- #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
- return socket < 0;
- #endif
- }
SocketBase.h
- #ifndef __SOCKET_BASE_H__
- #define __SOCKET_BASE_H__
- #include "cocos2d.h"
- #include <list>
- #include <thread>
- USING_NS_CC;
- // 对于windows平台
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
- #include <WinSock2.h>
- #pragma comment(lib, "WS2_32.lib")
- #define HSocket SOCKET
- // 对于android平台
- #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
- #include <arpa/inet.h> // for inet_**
- #include <netdb.h> // for gethost**
- #include <netinet/in.h> // for sockaddr_in
- #include <sys/types.h> // for socket
- #include <sys/socket.h> // for socket
- #include <unistd.h>
- #include <stdio.h> // for printf
- #include <stdlib.h> // for exit
- #include <string.h> // for bzero
- #define HSocket int
- #endif
- class SocketBase : public Ref
- {
- public:
- SocketBase();
- ~SocketBase();
- protected:
- void closeConnect(HSocket socket);
- bool error(HSocket socket);
- protected:
- std::mutex _mutex;
- private:
- bool _bInitSuccess;
- };
- #endif