GCDAsyncSocket读取数据问题 [英] GCDAsyncSocket read data issue

查看:479
本文介绍了GCDAsyncSocket读取数据问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用GCDAsyncSocket作为网络接口库的Cocoa实时视频程序.播放视频帧时明显呆滞.例如,视频上的时间几乎是正常时间的两倍.通过检查性能,我发现GCDAsyncSocket的readDataToLength方法过于稀疏.下面是带有时间戳的日志:

I have a Cocoa live video program that uses GCDAsyncSocket as the network interface library. There is obvious sluggish when playing the video frames. For example, the time on the video goes almost twice as slow as normal. By inspecting the performance, I found that the GCDAsyncSocket's readDataToLength method is called too sparsely. Below is the log with time stamp:

2012-05-16 11:18:27.054 DVRLibDemo[1181:903] readDataToLength
2012-05-16 11:18:27.256 DVRLibDemo[1181:903] readDataToLength
2012-05-16 11:18:27.285 DVRLibDemo[1181:903] readDataToLength
2012-05-16 11:18:27.452 DVRLibDemo[1181:903] readDataToLength
2012-05-16 11:18:27.475 DVRLibDemo[1181:903] readDataToLength
2012-05-16 11:18:27.697 DVRLibDemo[1181:903] readDataToLength
2012-05-16 11:18:27.726 DVRLibDemo[1181:903] readDataToLength
2012-05-16 11:18:27.856 DVRLibDemo[1181:903] readDataToLength

在创建套接字时,我设置了一个单独的委托调度队列,但是并没有太大帮助.该邮件具有固定的标头,我先读取标头,然后再读取有效载荷.

I set a separate delegate dispatch queue when creating the socket, but it does not help much. The message has a fixed header and I read the header firstly then the payload.

- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{    
    DVRConnection *conn = [self getConnectionWithSocket:sock];

    //DebugLogInfo(@"Receive raw data: %@ for connection: %d", data, conn.tag);

    if (conn != nil && conn.connected)
    {
        if (tag == TAG_XM_MESSAGE_HEADER)
        {
            uint32_t bodyLen = [_parser parseMessageHeader:data];
            [sock readDataToLength:bodyLen withTimeout:-1 tag:TAG_XM_MESSAGE_PAYLOAD];
        }
        else if (tag == TAG_XM_MESSAGE_PAYLOAD)
        {
            [_parser parseMessage:data forConnection:conn device:self];
            [sock readDataToLength:XM_MESSAGE_HEADER_LENGTH withTimeout:-1 tag:TAG_XM_MESSAGE_HEADER];
        }
    }
}

有人有什么想法可以提高从套接字读取数据的速度吗?谢谢!

Does anyone have any idea to improve the speed of reading data from socket? thanks!

推荐答案

原来是parseMessage代码中的错误.每次读取后,我都没有正确清除缓冲区.

It turned to be a bug in the parseMessage code. I did not clear the buffer correctly after each read.

这篇关于GCDAsyncSocket读取数据问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆