TCPSocket v1.0 for cocos2d-x下载

时间:2022-09-01 21:21:28

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

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

TCPSocket v1.0 for cocos2d-x下载

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

2、支持WIN32和LINUX。

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

  LOCAL_WHOLE_STATIC_LIBRARIES += socket_static

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

如图:

TCPSocket v1.0 for cocos2d-x下载

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&period;NET平台代码生成器V1&period;0发布(提供下载)

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

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

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

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

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

  4. Cocos2D将v1&period;0的tileMap游戏转换到v3&period;4中一例&lpar;一&rpar;

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

  5. 自动化测试管理平台ATMS&lpar;V1&period;0&period;1&lowbar;7&period;29&rpar;下载

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

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

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

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

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

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

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

  9. 痞子衡嵌入式:超级下载算法RT-UFL v1&period;0在Keil MDK下的使用

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

随机推荐

  1. 第三章 springboot &plus; jedisCluster&lpar;转载&rpar;

    本编博客转发自:http://www.cnblogs.com/java-zhao/p/5347703.html 如果使用的是redis2.x,在项目中使用客户端分片(Shard)机制. 如果使用的是r ...

  2. Popwindow自定义动画(nexus5不支持暂未解决)

    遇到一个问题,先记录一下 PopWindow自定义动画 import android.app.Activity; import android.graphics.drawable.BitmapDraw ...

  3. Android IOS WebRTC 音视频开发总结(六六)-- 三个角度分析美女视频直播这个行业

    本文主要从用户,公司和技术角度分析美女视频直播这个行业,文章最早发表在我们的微信公众号上,支持原创,详见这里, 欢迎关注微信公众号blackerteam,更多详见www.rtc.help 美女视频直播 ...

  4. style currentStyle getComputedStyle的区别和用法

    先介绍下层叠样式表的三种形式: 1.内联样式,在html标签中style属性设置. <p style="color:#f90">内联样式</p> 2.嵌入样 ...

  5. python none&comma;null&comma;&comma;&comma;&comma;&comma;类型

    内建类型None表示一个空对象,没有方法和属性. None是一个特殊的常量. None和False不同. None不是0. None不是空字符串. None和任何其他的数据类型比较永远返回False. ...

  6. 项目管理——WBS工作分解法

    首先我们要了解什么是WBS工作分解法 工作分解结构(Work Breakdown Structure,简称WBS)跟因数分解是一个原理,就是把一个项目,按一定的原则分解,项目分解成任务,任务再分解成一 ...

  7. Putty6&period;0 提示Access denied

    1.如果putty能正常使用,解决方法很简单: 只要在Putty的configuration里面Connection->SSH->Auth->GSSAPI的配置中,去掉默认的Atte ...

  8. MVC中的HtmlHelper详解

    熟悉MVC开发的朋友都应该知道在MVC中,每一个Controller都对应一个View,并且CS文件和对应的ASPX文件也被分离了,更重要的是不再有服务器端控件在工具箱中,不再是代码后至了.MVC中的 ...

  9. 删除Mac OS X中Finder文件打开方式列表的重复程序或失效的

    清理列表, 可以在终端中输入下面提供的一行命令: /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices ...

  10. JDK观察者模式和事件机制比较&lt&semi;转&gt&semi;

    原文:(六)观察者模式详解(包含观察者模式JDK的漏洞以及事件驱动模型) 作者:zuoxiaolong8810(左潇龙),转载请注明出处. 本章我们讨论一个除前面的单例以及代理模式之外,一个WEB项目 ...