Esempio n. 1
0
        internal Task ProcessAndSendMessage(MessageEventArgs e, EncryptContext context)
        {
            var         message     = (ProudMessage)e.Message;
            var         data        = message.ToArray();
            CoreMessage coreMessage = new RmiMessage(data);

            if (message.Compress)
            {
                data        = coreMessage.ToArray();
                coreMessage = new CompressedMessage(data.Length, data.CompressZLib());
            }

            if (message.Encrypt)
            {
                data = coreMessage.ToArray();
                using (var w = new PooledMemoryStream(Service.ArrayPool).ToBinaryWriter(false))
                {
                    w.Write(context.EncryptCounter);
                    w.Write(data);

                    data = w.ToArray();
                }

                data        = context.Encrypt(data);
                coreMessage = new EncryptedReliableMessage(data);
            }

            e.Message = coreMessage;
            return(base.OnSendMessage(e));
        }
Esempio n. 2
0
 public ServerRemotePeer(IP2PGroup @group, uint hostId, bool directP2PAllowed, EncryptContext encryptContext)
 {
     Group            = @group;
     HostId           = hostId;
     DirectP2PAllowed = directP2PAllowed;
     EncryptContext   = encryptContext;
     ConnectionStates = new ConcurrentDictionary <uint, P2PConnectionState>();
 }
Esempio n. 3
0
        public void Join(uint hostId, bool directP2P)
        {
            var            encrypted      = Filter.Config.EnableP2PEncryptedMessaging;
            EncryptContext encryptContext = null;

            if (encrypted)
            {
                encryptContext = new EncryptContext(Filter.Config.EncryptedMessageKeyLength);
            }

            var remotePeer = new ServerRemotePeer(this, hostId, directP2P, encryptContext);

            if (!_members.TryAdd(hostId, remotePeer))
            {
                throw new ProudException($"Member {hostId} is already in P2PGroup {HostId}");
            }

            var session = _filter.SessionLookupByHostId.GetValueOrDefault(hostId);

            if (session != null)
            {
                session.P2PGroup = this;

                if (encrypted)
                {
                    session.Send(new P2PGroup_MemberJoinMessage(HostId, hostId, 0, encryptContext.RC4.Key, directP2P));
                }
                else
                {
                    session.Send(new P2PGroup_MemberJoin_UnencryptedMessage(HostId, hostId, 0, directP2P));
                }
            }

            foreach (var member in _members.Values.Where(member => member.HostId != hostId).Cast <ServerRemotePeer>())
            {
                var memberSession = _filter.SessionLookupByHostId.GetValueOrDefault(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?.Send(new P2PGroup_MemberJoinMessage(HostId, hostId, stateB.EventId, encryptContext.RC4.Key, directP2P));
                    session?.Send(new P2PGroup_MemberJoinMessage(HostId, member.HostId, stateA.EventId, member.EncryptContext.RC4.Key, directP2P));
                }
                else
                {
                    memberSession?.Send(new P2PGroup_MemberJoin_UnencryptedMessage(HostId, hostId, stateB.EventId, directP2P));
                    session?.Send(new P2PGroup_MemberJoin_UnencryptedMessage(HostId, member.HostId, stateA.EventId, directP2P));
                }
            }
        }
Esempio n. 4
0
        public override void Close()
        {
            Send(new ShutdownTcpAckMessage());

            base.Close();

            if (EncryptContext != null)
            {
                EncryptContext.Dispose();
                EncryptContext = null;
            }

            ReadyEvent.Reset();
        }