iPhone:使用游戏套件发送大数据 [英] iPhone: Sending large data with Game Kit

查看:62
本文介绍了iPhone:使用游戏套件发送大数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个与通过Game Kit框架运行该应用程序的其他iPhone交换数据的应用程序. iPhone彼此发现并连接良好,但是当我发送数据时会发生问题.我知道iPhone的连接正确,因为当我序列化NSString并通过连接发送它时,它会从另一端出来.但是,当我尝试归档较大的对象(使用NSKeyedArchiver)时,出现错误消息"AGPSessionBroadcast失败(801c0001)".

I am trying to write an app that exchanges data with other iPhones running the app through the Game Kit framework. The iPhones discover each other and connect fine, but the problems happens when I send the data. I know the iPhones are connected properly because when I serialize an NSString and send it through the connection it comes out on the other end fine. But when I try to archive a larger object (using NSKeyedArchiver) I get the error message "AGPSessionBroadcast failed (801c0001)".

我认为这是因为我发送的数据太大(我的文件大小约为500k,Apple似乎建议最大为95k).我曾尝试将数据分成几个传输,但是我永远无法在另一端正确地将其取消存档.我想知道是否还有其他人遇到这个问题,以及您如何解决它.

I am assuming this is because the data I am sending is too large (my files are about 500k in size, Apple seems to recommend a max of 95k). I have tried splitting up the data into several transfers, but I can never get it to unarchive properly at the other end. I'm wondering if anyone else has come up against this problem, and how you solved it.

推荐答案

我在300K左右的文件中也遇到了同样的问题.问题在于发送方需要在发送下一个块之前知道接收方何时清空了管道.

I had the same problem w/ files around 300K. The trouble is the sender needs to know when the receiver has emptied the pipe before sending the next chunk.

我最终得到了一个在两侧都运行的简单状态引擎.发送方发送一个标头,其中包含要发送的总字节数和数据包大小,然后等待另一端的确认.一旦获得握手,它将继续发送固定大小的数据包,每个数据包都标记有序列号.

I ended up with a simple state engine that ran on both sides. The sender transmits a header with how many total bytes will be sent and the packet size, then waits for acknowledgement from the other side. Once it gets the handshake it proceeds to send fixed size packets each stamped with a sequence number.

接收器获取每个,读取并添加到缓冲区,然后写回管道,将其接收到具有序列号的数据包.发送方读取数据包#,切出另一个缓冲区的值,依此类推.双方都跟踪它们所处的状态(空闲,发送标头,接收标头,发送数据,接收数据,错误,完成等).双方必须跟踪何时读/写最后一个片段,因为它是可能小于整个缓冲区的大小.

The receiver gets each one, reads it and appends it to a buffer, then writes back to the pipe that it got packet with the sequence #. Sender reads the packet #, slices out another buffer's worth, and so on and so forth. Each side keeps track of the state they're in (idle, sending header, receiving header, sending data, receiving data, error, done etc.) The two sides have to keep track of when to read/write the last fragment since it's likely to be smaller than the full buffer size.

这可以正常工作(虽然有点慢),并且可以缩放到任意大小.我开始使用5K数据包大小,但运行速度非常慢.将其推到10K,但开始引起问题,因此我退回并保持在8096.它对于二进制和文本数据都可以正常工作.

This works fine (albeit a bit slow) and it can scale to any size. I started with 5K packet sizes but it ran pretty slow. Pushed it to 10K but it started causing problems so I backed off and held it at 8096. It works fine for both binary and text data.

这篇关于iPhone:使用游戏套件发送大数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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