iPhone-如何在P2P中传输数据 [英] iPhone - how is data transferred in P2P

查看:174
本文介绍了iPhone-如何在P2P中传输数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现P2P应用程序.我要做的是在设备之间传输自定义NSObject类的数据.我无法弄清楚GKSession何时调用了"receivedData"方法.

I am trying to implement a P2P application. What I try to do is transfer a custom NSObject class's data between the devices. I am not able to figure out when the "receivedData" method is invoked by GKSession.

这就是我所做的

  1. 通常的连接,并显示一个带有对等方的对话框

  1. The usual connection and showing a dialog box with peers

当应用收到对等方的状态更改并且状态为已连接"时,我正在使用NSCoding方法将数据发送给所有对等方(理想情况下,当每个对等方接受连接时都应调用此数据)

When the app receives a state change for a peer and the state is Connected, I am sending the data to all peers (ideally this should be called when each peer accepts the connection) using NSCoding methods

在ReceivedData中,我正在解码NSData并取回NSObject实例

In receivedData, I am decoding the NSData and getting back the NSObject instance

问题是在两个设备上都没有调用 session:peer:didChangeState:方法.只需在点击PeerPicker警报中的连接"按钮的设备上调用它即可.

The problem is the session:peer:didChangeState: method is not getting called on both the devices. It just gets called on the device which tapped the "Connect" button in the PeerPicker alert.

问题

  1. ChangeState何时调用,谁调用?

  1. When is didChangeState called and for whom? Shouldn't it be called for each peer when they are connected?

何时调用ReceivedData:方法,并为谁调用?是用来确认收到消息(握手)还是只是传输其他对等方发送的数据?

When is the receivedData: method called and for whom? Is it called to confirm receipt of a message (handshake) or just to transmit the data sent by other peers?

感谢您的帮助.

推荐答案

更多地将其视为主/从"而不是对等".大概双方都已经使用GameKit进行了设置,并指派了他们的代表.通过GKPeerStateConnected状态更改值来通知发起连接的一侧,即接收方已接受邀请并已建立连接,以便接收方可以开始发送内容.

Think of it more as 'master/slave' instead of 'peer to peer.' Presumably both sides have been set up with GameKit and had their delegates assigned. The one side that initiates the connection gets notified via the GKPeerStateConnected state change value that the receiver has accepted the invitation and a connection has been made so it can start sending stuff over.

但是,接收方首先注意到有人要通过didReceiveConnectionRequestFromPeer委托方法与之交谈.它保存会话以供以后使用,然后转过身并调用acceptConnectionFromPeer接受它.从那时起,它将获得通过receiveData方法发送的任何内容.

The receiving side, however, gets its first notice that someone wants to talk to it through the didReceiveConnectionRequestFromPeer delegate method. It saves the session for later use then turns around and calls acceptConnectionFromPeer to accept it. From then on, it gets whatever is sent over through the receiveData method.

执行此操作的最佳方法是使用一个小的状态机来管理双方所处的状态.发件人仅在收到GKPeerStateConnected事件时才进行发送.接收器一旦接受连接请求,便会进入connected状态,这样它就可以在开始获取数据之前进行所需的任何设置.

The best way to do this is to have a little state-machine that manages what state each side is in. The sender only sends when it's gotten the GKPeerStateConnected event. The receiver is put into connected state once it accepts a connection request so it can do any setup it needs to do before it starts getting data.

由于网络抽象为GKSession,因此接收者可以转身并使用它在didReceiveConnectionRequestFromPeer中获得的相同会话实例,并使用它写回发送者(后者应具有自己的receiveData方法,调用接收方的回复).

Since the network abstraction is GKSession the receiver can then turn around and use the same session instance it got in didReceiveConnectionRequestFromPeer and use it to write back to the sender (who should have its own receiveData method called with responses from the receiver).

要记住的一件事是,可以通过GameKit发送的缓冲区存在隐式数据大小限制.您可能需要尝试找出适合您系统的功能(由于某些原因,我得到的数值介于5K和10K之间).您可能需要使用代码将NSData拆分为多个块,并管理对数据进行打包/解包.

The one thing to keep in mind is that there's an implicit data size limit on the buffer you can send through GameKit. You may want to experiment to find what that is for your system (I get something between 5K and 10K--for some reason it varies). You may want to have code to split up your NSData into chunks and manage packetizing/depacketizing the data.

这篇关于iPhone-如何在P2P中传输数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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