当前位置: 首页 > news >正文

AsyncSocket中tag参数的用处

tag参数是为了在回调方法中匹配发起调用的方法的,不会加在传输数据中。


调用write方法,等待接收消息。收到消息后,会回调didReadData的delegate方法,
delegate方法中的tag和发起read的方法中的tag是对应的。
- (void)readDataWithTimeout:(NSTimeInterval)timeout tag:(long)tag;
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag;


write和read是一样:writeData方法和对应的delegate方法didWriteDataWithTag的tag是对应的。
- (void)writeData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag;


需注意的一点是:发送时的tag和接收时的tag是无关的。


以read为例分析:
- (void)readDataWithTimeout:(NSTimeInterval)timeout tag:(long)tag
上面的方法会生成一个数据类:AsyncReadPacket,此类中包含tag,并把此对象放入数组theReadQueue中。
在CFStream中的回调方法中,会取theReadQueue最新的一个,在回调方法中取得tag,并将tag传
给回调方法:
- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag;
如此而已。


官方解释:


In addition to this you've probably noticed the tag parameter. The tag you pass during the read/write operation is passed back to you via the delegate method once the read/write operation completes. It does not get sent over the socket or read from the socket. It is designed to help simplify the code in your delegate method. For example, your delegate method might look like this:


#define TAG_WELCOME 10
#define TAG_CAPABILITIES 11
#define TAG_MSG 12


... 


- (void)socket:(AsyncSocket *)sender didReadData:(NSData *)data withTag:(long)tag
{
    if (tag == TAG_WELCOME)
    {
        // Ignore welcome message
    }
    else if (tag == TAG_CAPABILITIES)
    {
        [self processCapabilities:data];
    }
    else if (tag == TAG_MSG)
    {
        [self processMessage:data];
    }

相关文章:

  • 给我深爱着的人,祝你中秋快乐……
  • 用UILocalNotification实现闹钟的坑
  • .NET中的Event与Delegates,从Publisher到Subscriber的衔接!
  • 略时间中秒的部分,使时间中的秒变成0
  • 不可忽视的力量,插件主题网站
  • 日期只精确到天 省略时间的时间部分
  • 10月7日培训日记
  • Xcode6建的工程修改为在iPhone6和iPhone6P下等比放大
  • iOS7以上使导航栏半透明
  • 过程建模EPC,我拿什么拯救你
  • 用图作背景的cell选中时背景图变暗
  • Guru.com - The world's largest online marketplace for freelance talent.
  • view controller的automaticallyAdjustsScrollViewInsets属性
  • Blog小技巧之三-在地图上显示访问Blog读者的分布信息
  • 简单的在工程中集成RAC
  • avalon2.2的VM生成过程
  • axios 和 cookie 的那些事
  • Date型的使用
  • IndexedDB
  • interface和setter,getter
  • java8 Stream Pipelines 浅析
  • java概述
  • JS基础之数据类型、对象、原型、原型链、继承
  • Linux CTF 逆向入门
  • node.js
  • PAT A1092
  • Python 基础起步 (十) 什么叫函数?
  • Python语法速览与机器学习开发环境搭建
  • TiDB 源码阅读系列文章(十)Chunk 和执行框架简介
  • vuex 学习笔记 01
  • windows下使用nginx调试简介
  • 海量大数据大屏分析展示一步到位:DataWorks数据服务+MaxCompute Lightning对接DataV最佳实践...
  • 盘点那些不知名却常用的 Git 操作
  • 一起参Ember.js讨论、问答社区。
  • Python 之网络式编程
  • 长三角G60科创走廊智能驾驶产业联盟揭牌成立,近80家企业助力智能驾驶行业发展 ...
  • 关于Android全面屏虚拟导航栏的适配总结
  • ​二进制运算符:(与运算)、|(或运算)、~(取反运算)、^(异或运算)、位移运算符​
  • # C++之functional库用法整理
  • #Z2294. 打印树的直径
  • #图像处理
  • $HTTP_POST_VARS['']和$_POST['']的区别
  • (51单片机)第五章-A/D和D/A工作原理-A/D
  • (C语言)输入一个序列,判断是否为奇偶交叉数
  • (python)数据结构---字典
  • (心得)获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列。
  • (新)网络工程师考点串讲与真题详解
  • (学习日记)2024.02.29:UCOSIII第二节
  • (转)Android学习系列(31)--App自动化之使用Ant编译项目多渠道打包
  • (转)eclipse内存溢出设置 -Xms212m -Xmx804m -XX:PermSize=250M -XX:MaxPermSize=356m
  • (转)使用VMware vSphere标准交换机设置网络连接
  • (轉貼) 資訊相關科系畢業的學生,未來會是什麼樣子?(Misc)
  • ... fatal error LINK1120:1个无法解析的外部命令 的解决办法
  • .h头文件 .lib动态链接库文件 .dll 动态链接库
  • .Mobi域名介绍