3

If I have an array of connected peers, let's say 3 (a->b, a->c, a->d) and I want to disconnect peer "c" only, what should I be doing?

I've seen one response to a similar question state that you can only disconnect 'yourself' from a session: meaning that in the above scenario if I do this:

[mySession disconnect];

that I all be disconnecting "a" from "b", "c" and "d" simultaneously. So the suggestion was to send a notification to the peer you want disconnected ("c") and tell it to disconnect from the session.

However, I've also seen this and wondered if it does what I'm asking - removes the specific peer only:

[mySession.connectedPeers[0] disconnect];

EDIT: I just realized this is a read-only property, so ignore this portion.

Or is there yet another, perhaps better way to remove specific peers?

wayneh
  • 4,393
  • 9
  • 35
  • 70

1 Answers1

3

Each peer-to-peer connection is represented by an instance of MCSession. So in your example, you will have 3 UNIQUE pointers to MCSession objects:

MCSession *p1 = a->b
MCSession *p2 = a->c
MCSession *p3 = a->d

so to disconnect peer c you will:

[p2 disconnect]; // release the session
p2 = nil; // release the resource

Of course, you can store sessions in a mutable array and then do the same logic, but use the pointer from the array

p2 = (MCSession *)[array objectAtIndex:1];
[p2 disconnect];
[array removeObjectAtIndex:1];
300baud
  • 540
  • 4
  • 17
  • I must be really confused then - I was under the impression that a single "session" instance could have multiple peers connected, as I showed in my question. Are you saying that you can only have one peer per session and must keep an array of sessions, each with only one peer? – wayneh May 03 '14 at 17:51
  • Not at all. In your question you just said you have an array of connected peers - not how you connected them. You can have multiple peers (up to 8) in a single session, or in my case, because of workarounds with the frameworks current not-so-stable state, I keep each session a 1:1 relationship between the peers. AFAIK when you "disconnect" you are telling MPC that all the peers you are connected to in that session need to be disconnected. If you want better granularity, go with the 1:1 approach – 300baud May 04 '14 at 01:31
  • Seems reasonable, especially since there doesn't seem to be a way to disconnect a specific peer when multiples are on a single session. Thanks. Let's see if anyone disagrees now that I've accepted your answer.... – wayneh May 05 '14 at 15:19