下载地址:http://files.cnblogs.com/elephant-x/TCPSocketLibs_V1.0.rar

这是自己封装的一个TCPSOCKET包,是独立于cocos2d-x的,使用的时候,请把该项目加入到cocos2d-x里面去,再在项目里面包含libSocket项目和libSocket.lib

1、独立线程接收,异步连接服务端,防止界面卡的情况。

2、支持WIN32和LINUX。

3、编译linux时,在项目的Android.mk文件里必须添加下面两行:

  LOCAL_WHOLE_STATIC_LIBRARIES += socket_static

  $(call import-module,../CustomLibs/libSocket)

如图:

4、在Application.mk里面加入-std=c++11,因为源代码里面用到了c++11(VS2012)才有的std::bind和std::function,不支持c++11以下的编译器

APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -DCC_ENABLE_CHIPMUNK_INTEGRATION=1 -DCOCOS2D_DEBUG=1 -std=c++11

如果是c++11以下的编译器,可以参考:

“function对于回调函数、将操作作为参数传递等十分有用。它可以看做是C++98标准库中函数对象mem_fun_t, pointer_to_unary_function等的替代品。同样的,bind()也可以被看做是bind1st()和bind2nd()的替代品,当然比他们更强大更灵活。”

自行改写。。

使用例子代码,可以拷贝到HelloCpp项目里去测试。

.h

 #ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__ #include "cocos2d.h"
#include "TCPSocket.h" class GameScene : public cocos2d::CCLayer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init(); // there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* scene(); // a selector callback
void menuCloseCallback(CCObject* pSender);
// 注册单个协议回调函数(样例),参数:SOCKET_ID标识,数据包
void process_login(int _tag, WorldPacket & packet);
// 物品更新
void process_openbackpack(int _tag, WorldPacket & packet);
// 注册单个协议回调函数(样例),参数:SOCKET_ID标识,协议头,数据包
bool process_all(int _tag, int _cmd, WorldPacket & packet);
// 连接事件
void OnConnect(int _tag, bool isConnect);
// 断线事件
void onDisconnect(int _tag);
// implement the "static node()" method manually
CREATE_FUNC(GameScene);
}; #endif // __HELLOWORLD_SCENE_H__

.cpp

 #include "GameScene.h"
#include "AppDelegate.h"
#include "AppMacros.h"
USING_NS_CC; CCScene* GameScene::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create(); // 'layer' is an autorelease object
GameScene *layer = GameScene::create(); // add layer as a child to scene
scene->addChild(layer); // return the scene
return scene;
}
void GameScene::process_login(int _tag, WorldPacket & packet)
{
CCLOG("process_login len:%u", packet.size());
// 定义协议包
WorldPacket newP;
newP.clear();
newP.SetOpcode(0x00B6);// 设置协议头
newP << uint16(0x00B6)
<< uint16();// 协议长度
newP.SetLength(newP.size());// 设置协议长度
sSocketMgr.SendPacket(, &newP);// 发送数据
} void GameScene::process_openbackpack(int _tag, WorldPacket & packet)
{
CCLOG("process_openbackpack len:%u", packet.size());
} bool GameScene::process_all(int _tag, int _cmd, WorldPacket & packet)
{
CCLOG("process_all _tag:%u, _cmd:%u, len:%u", _tag, _cmd, packet.size());
return false;
} void GameScene::OnConnect(int _tag, bool isConnect)
{
// 定义协议包
WorldPacket packet;
packet.clear();
packet.SetOpcode(0x0010);// 设置协议头
packet << uint16(0x0010)
<< uint16()// 协议长度
<< uint8()
<< uint8();
// 加入字符串数据(uint8表示字符串长度所占字节,此处为1字节)
packet.AppendPacketString<uint8>(std::string("aaa:88889083:d5956683c17d7e284d33ee295b277b52"));
packet.SetLength(packet.size());// 设置协议长度
sSocketMgr.SendPacket(, &packet);// 发送数据
CCLOG("OnConnect:%u, isConnect[%u]", _tag, isConnect);
} void GameScene::onDisconnect(int _tag)
{
CCLOG("desconnect:%u", _tag);
} // on "init" you need to initialize your instance
bool GameScene::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
} CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); /////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it. // add a "close" icon to exit the progress. it's an autorelease object
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(GameScene::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/ ,
origin.y + pCloseItem->getContentSize().height/)); // create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, ); /////////////////////////////
// 3. add your codes below... // add a label shows "Hello World"
// create and initialize a label CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", TITLE_FONT_SIZE); // position the label on the center of the screen
pLabel->setPosition(ccp(origin.x + visibleSize.width/,
origin.y + visibleSize.height - pLabel->getContentSize().height)); // add the label as a child to this layer
this->addChild(pLabel, ); // add "HelloWorld" splash screen"
CCSprite* pSprite = CCSprite::create("HelloWorld.png"); // position the sprite on the center of the screen
pSprite->setPosition(ccp(visibleSize.width/ + origin.x, visibleSize.height/ + origin.y)); // add the sprite as a child to this layer
this->addChild(pSprite, );
///////////////////////////////////////////////
// 创建SOCKET管理器
CREATE_TCPSOCKETMGR();
uint64 t_begin = GetCurrentTime();
// 创建并添加SOCKET,参数:服务器IP,端口,自定义的SOCKET_ID标识
sSocketMgr.createSocket("192.168.0.183", , );
uint64 t_end = GetCurrentTime();
// 注册协议,参数:包头,回调函数
sSocketMgr.register_process(0x10, SCT_CALLBACK_2(GameScene::process_login, this));
sSocketMgr.register_process(0x2d, SCT_CALLBACK_2(GameScene::process_openbackpack, this));
// 注册消息截获事件,注册此事件后可以截获收到的所有消息,若回调函数返回true则本次事件会继续分发注册过的协议,返回false则不分发
sSocketMgr.register_connect(SCT_CALLBACK_2(GameScene::OnConnect, this));
sSocketMgr.register_disconnect(SCT_CALLBACK_3(GameScene::onDisconnect, this)); CCLOG("GameScene::init end! [%u]", t_end - t_begin);
///////////////////////////////////////////////
return true;
} void GameScene::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit();
#endif
#endif
}

TCPSocket v1.0 for cocos2d-x下载的更多相关文章

  1. RDIFramework.NET平台代码生成器V1.0发布(提供下载)

    RDIFramework.NET平台代码生成器V1.0发布(提供下载)   RDIFramework.NET(.NET快速开发整合框架)框架做为信息化系统快速开发.整合的框架,其目的一至是给用户和开发 ...

  2. 痞子衡嵌入式:超级下载算法RT-UFL v1.0发布,附J-Link下安装教程

    痞子衡主导的"学术"项目 <RT-UFL - 一个适用全平台i.MXRT的超级下载算法设计> 历时 8 个月终于迎来了 v1.0 版发布,因为是第一个正式版,为了保证质 ...

  3. 《驱蚊神器v1.0》android应用 赶走那些烦人的臭蚊子

    <驱蚊神器v1.0>能够非常好地赶走那些个烦人又恼人伤人的臭蚊子,它总是搞得自己没有好的睡眠或歇息,得努力地拍巴巴掌,这下可好了,也少些烦恼了,先深情地眯缝一会儿...此声波怡人不会对人产 ...

  4. JuiceFS v1.0 beta3 发布,支持 etcd、Amazon MemoryDB、Redis Cluster

    JuiceFS v1.0 beta3 在元数据引擎方面继续增强,新增 etcd 支持小于 200 万文件的使用场景,相比 Redis 可以提供更好的可用性和安全性.同时支持了 Amazon Memor ...

  5. Cocos2D将v1.0的tileMap游戏转换到v3.4中一例(一)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 首先说一下为什么要转换,这是为了后面的A*寻路算法做准备.由于在 ...

  6. 自动化测试管理平台ATMS(V1.0.1_7.29)下载

    自动化测试管理平台ATMS(V1.0.1_7.29)下载http://automationqa.com/forum.php?mod=viewthread&tid=2582&fromui ...

  7. VisualCom软件仿真平台V1.0发布(附安装包下载链接)

    自我们借助VisualCom(暂定名称,后续可能会变更)软件平台撰写技术文章以来,有不少粉丝发私信询问该软件哪里来的,以及哪里有安装包,这里回复一下:VisualCom软件平台是由本微信公众号组织开发 ...

  8. 痞子衡嵌入式:超级下载算法RT-UFL v1.0在恩智浦MCUXpresso IDE下的使用

    痞子衡主导的"学术"项目 <RT-UFL - 一个适用全平台i.MXRT的超级下载算法设计> v1.0 版发布近 4 个月了,部分客户已经在实际项目开发调试中用上了这个 ...

  9. 痞子衡嵌入式:超级下载算法RT-UFL v1.0在IAR EW for Arm下的使用

    痞子衡主导的"学术"项目 <RT-UFL - 一个适用全平台i.MXRT的超级下载算法设计> v1.0 版发布近 4 个月了,部分客户已经在实际项目开发调试中用上了这个 ...

随机推荐

  1. Quartz 并发/单线程

    Quartz 并发/单线程 Quartz定时任务默认都是并发执行的,不会等待上一次任务执行完毕,只要间隔时间到就会执行, 如果定时任执行太长,会长时间占用资源,导致其它任务堵塞.1.在Spring中这 ...

  2. CentOS7 升级python同时解决yum损坏问题

    CentOS7中的python版本为python2.7.5,升级到最新版的python时需要注意两个问题 新版的python安装好后要修改python的系统默认指向问题 升级到最新版python后yu ...

  3. android actionbar标题栏

    在android的actionBar中,actionBar的视图是固定的,左边是程序的图标和title,右边是添加的menuItem,如果想要定制actionbar中的view就要自定义视图. 首先要 ...

  4. MyEclipse Blue Edition 6.5 注册码生成程序

    import java.text.DecimalFormat; import java.text.NumberFormat; import java.text.SimpleDateFormat; im ...

  5. 简单的XPath入门

    XPath即为XML路径语言,它是一种用来确定XML(标准通用标记语言的子集)文档中某部分位置的语言.XPath基于XML的树状结构,提供在数据结构树中找寻节点的能力.XPath 是 XML 的查询语 ...

  6. apk反编译(7)用ProGuard混淆代码,初级防止反编译

    eclipse为例 1,project.properties去掉 #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:pro ...

  7. 无法生成临时类(result=1)。 error CS0229: “DCSoftDotfuscate.aam.a”与“DCSoftDotfuscate.aam.a()”之间存在二义性

    对于错误无法生成临时类(result=1).error CS0229: “DCSoftDotfuscate.aam.a”与“DCSoftDotfuscate.aam.a()”之间存在二义性 出现这个错 ...

  8. 1888. Pilot Work Experience(dfs+bfs)

    1888 dfs找出连通块 块内构造数据 bfs找出最值 如果有多个连通块 那max就为49 可以起点不同 这样记得修改后面的数据 写的老长了.. #include <iostream> ...

  9. bzoj3994

    智商太低了 详细题解在这里http://blog.csdn.net/zmoiynlp/article/details/45176129 ; ..max] of longint; g:..max] of ...

  10. Android开发时提示Your project contains error(s),please fix them be

    有次在使用eclipse写好Android的代码,代码没有报错.然后 想在AVD中运行测试时,总是会弹出错误框,提示信息为:    “Your project contains error(s),pl ...