public void Join(uint hostId)
        {
            // using (_sync.Lock())
            {
                var   encrypted = _server.Configuration.EnableP2PEncryptedMessaging;
                Crypt crypt     = null;
                if (encrypted)
                {
                    crypt = new Crypt(_server.Configuration.EncryptedMessageKeyLength,
                                      _server.Configuration.FastEncryptedMessageKeyLength);
                }

                var session    = _server.Sessions[hostId];
                var remotePeer = new RemotePeer(this, session, crypt);
                if (!_members.TryAdd(hostId, remotePeer))
                {
                    throw new ProudException($"Member {hostId} is already in P2PGroup {HostId}");
                }

                _server.Configuration.Logger?.Debug("Client({HostId}) joined P2PGroup({GroupHostId})", hostId, HostId);
                session.P2PGroup = this;

                if (encrypted)
                {
                    session.SendAsync(new P2PGroup_MemberJoinMessage(HostId, hostId, 0, crypt.AES.Key, AllowDirectP2P));
                }
                else
                {
                    session.SendAsync(new P2PGroup_MemberJoin_UnencryptedMessage(HostId, hostId, 0, AllowDirectP2P));
                }

                foreach (var member in _members.Values.Where(member => member.HostId != hostId))
                {
                    var memberSession = _server.Sessions[member.HostId];
                    var stateA        = new P2PConnectionState(member);
                    var stateB        = new P2PConnectionState(remotePeer);

                    remotePeer.ConnectionStates[member.HostId] = stateA;
                    member.ConnectionStates[remotePeer.HostId] = stateB;
                    if (encrypted)
                    {
                        memberSession.SendAsync(new P2PGroup_MemberJoinMessage(HostId, hostId, stateB.EventId,
                                                                               crypt.AES.Key, AllowDirectP2P));
                        session.SendAsync(new P2PGroup_MemberJoinMessage(HostId, member.HostId, stateA.EventId,
                                                                         crypt.AES.Key, AllowDirectP2P));
                    }
                    else
                    {
                        memberSession.SendAsync(
                            new P2PGroup_MemberJoin_UnencryptedMessage(HostId, hostId, stateB.EventId, AllowDirectP2P));
                        session.SendAsync(new P2PGroup_MemberJoin_UnencryptedMessage(HostId, member.HostId,
                                                                                     stateA.EventId, AllowDirectP2P));
                    }
                }
            }
        }
Exemple #2
0
 public P2PConnectionState(RemotePeer remotePeer)
 {
     RemotePeer = remotePeer;
     EventId    = (uint)Guid.NewGuid().GetHashCode();
 }