Ejemplo n.º 1
0
        public virtual void SendMessage(P2PMessage msg, P2PMessageSessionEventArgs se)
        {
            // if it is a regular message convert it
            P2PDCMessage p2pMessage = msg as P2PDCMessage;

            if (p2pMessage == null)
            {
                p2pMessage = new P2PDCMessage(msg);
            }

            // prepare the message
            p2pMessage.PrepareMessage();

            // this is very bloated!
            Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose, "Outgoing message:\r\n" + p2pMessage.ToDebugString(), GetType().Name);

            if (dcSocket != null)
            {
                Processor.SendSocketData(dcSocket, p2pMessage.GetBytes(), se);
            }
            else
            {
                Processor.Send(p2pMessage.GetBytes(), se);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates an acknowledgement message to a handshake message. This will only set the flag to 0.
        /// </summary>
        /// <returns></returns>
        public override P2PMessage CreateAcknowledgement()
        {
            // re-create a copy of this message, it is just the same copy!
            P2PDCMessage ackMessage = new P2PDCMessage(this);

            // set the identifier to 0 to set our own local identifier
            ackMessage.Header.Identifier = 0;
            return(ackMessage);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Sends the P2PMessage directly over the socket. Accepts P2PDCMessage and P2PMessage objects.
        /// </summary>
        /// <param name="message"></param>
        public void SendMessage(NetworkMessage message)
        {
            // if it is a regular message convert it
            P2PDCMessage p2pMessage = message as P2PDCMessage;

            if (p2pMessage == null)
            {
                p2pMessage = new P2PDCMessage(message as P2PMessage);
            }

            SendMessage(p2pMessage, null);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates an acknowledgement message to a handshake message. This will only set the flag to 0.
        /// </summary>
        /// <returns></returns>
        public override P2PMessage CreateAcknowledgement()
        {
            // re-create a copy of this message, it is just the same copy!
            P2PDCMessage ackMessage = new P2PDCMessage(this);

            // set the identifier to 0 to set our own local identifier
            ackMessage.Header.Identifier = 0;
            return ackMessage;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Discards the foo message and sends the message to all handlers as a P2PDCMessage object.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void OnMessageReceived(object sender, ByteEventArgs e)
        {
            Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose,
                "Analyzing message in DC state <" + dcState + ">", GetType().Name);

            byte[] data = e.Bytes;

            switch (dcState)
            {
                case DirectConnectionState.Established:
                    {
                        // Convert to a p2pdc message
                        P2PDCMessage dcMessage = new P2PDCMessage(version);
                        dcMessage.ParseBytes(data);

                        OnP2PMessageReceived(new P2PMessageEventArgs(dcMessage));
                    }
                    break;

                case DirectConnectionState.HandshakeReply:
                    {
                        P2PDCHandshakeMessage match = VerifyHandshake(data);

                        if (match == null)
                        {
                            Dispose();
                            return;
                        }

                        Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose,
                            "Nonce accepted: " + match.Guid + "; My Nonce: " + this.nonce + "; Need Hash: " + needHash, GetType().Name);

                        DCState = DirectConnectionState.Established;
                    }
                    break;

                case DirectConnectionState.Handshake:
                    {
                        P2PDCHandshakeMessage match = VerifyHandshake(data);

                        if (match == null)
                        {
                            Dispose();
                            return;
                        }

                        Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose,
                            "Nonce MATCH: " + match.Guid, GetType().Name);

                        match.Guid = reply;

                        if (version == P2PVersion.P2PV1)
                        {
                            startupSession.IncreaseLocalIdentifier();
                            match.Header.Identifier = startupSession.LocalIdentifier;
                        }

                        // Send Nonce Reply
                        SendMessage(match);

                        DCState = DirectConnectionState.Established;

                    }
                    break;

                case DirectConnectionState.Foo:
                    {
                        string initialData = Encoding.ASCII.GetString(data);

                        if (data.Length == 4 && initialData == "foo\0")
                        {
                            DCState = DirectConnectionState.Handshake;
                            Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose, "foo0 handled", GetType().Name);
                        }
                        else
                        {
                            Trace.WriteLineIf(Settings.TraceSwitch.TraceWarning, "foo0 expected, but it was: " + initialData, GetType().Name);
                            Dispose();
                            return;
                        }
                    }
                    break;

                case DirectConnectionState.Closed:
                    break;
            }
        }
Ejemplo n.º 6
0
        public virtual void SendMessage(P2PMessage msg, P2PMessageSessionEventArgs se)
        {
            // if it is a regular message convert it
            P2PDCMessage p2pMessage = msg as P2PDCMessage;
            if (p2pMessage == null)
            {
                p2pMessage = new P2PDCMessage(msg);
            }

            // prepare the message
            p2pMessage.PrepareMessage();

            // this is very bloated!
            Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose, "Outgoing message:\r\n" + p2pMessage.ToDebugString(), GetType().Name);

            if (dcSocket != null)
                Processor.SendSocketData(dcSocket, p2pMessage.GetBytes(), se);
            else
                Processor.Send(p2pMessage.GetBytes(), se);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Sends the P2PMessage directly over the socket. Accepts P2PDCMessage and P2PMessage objects.
        /// </summary>
        /// <param name="message"></param>
        public void SendMessage(NetworkMessage message)
        {
            // if it is a regular message convert it
            P2PDCMessage p2pMessage = message as P2PDCMessage;
            if (p2pMessage == null)
            {
                p2pMessage = new P2PDCMessage(message as P2PMessage);
            }

            SendMessage(p2pMessage, null);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Discards the foo message and sends the message to all handlers as a P2PDCMessage object.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void OnMessageReceived(object sender, ByteEventArgs e)
        {
            Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose,
                              "Analyzing message in DC state <" + dcState + ">", GetType().Name);

            byte[] data = e.Bytes;

            switch (dcState)
            {
            case DirectConnectionState.Established:
            {
                // Convert to a p2pdc message
                P2PDCMessage dcMessage = new P2PDCMessage(version);
                dcMessage.ParseBytes(data);

                OnP2PMessageReceived(new P2PMessageEventArgs(dcMessage));
            }
            break;

            case DirectConnectionState.HandshakeReply:
            {
                P2PDCHandshakeMessage match = VerifyHandshake(data);

                if (match == null)
                {
                    Dispose();
                    return;
                }

                Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose,
                                  "Nonce accepted: " + match.Guid + "; My Nonce: " + this.nonce + "; Need Hash: " + needHash, GetType().Name);

                DCState = DirectConnectionState.Established;
            }
            break;

            case DirectConnectionState.Handshake:
            {
                P2PDCHandshakeMessage match = VerifyHandshake(data);

                if (match == null)
                {
                    Dispose();
                    return;
                }

                Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose,
                                  "Nonce MATCH: " + match.Guid, GetType().Name);

                match.Guid = reply;

                if (version == P2PVersion.P2PV1)
                {
                    startupSession.IncreaseLocalIdentifier();
                    match.Header.Identifier = startupSession.LocalIdentifier;
                }

                // Send Nonce Reply
                SendMessage(match);

                DCState = DirectConnectionState.Established;
            }
            break;

            case DirectConnectionState.Foo:
            {
                string initialData = Encoding.ASCII.GetString(data);

                if (data.Length == 4 && initialData == "foo\0")
                {
                    DCState = DirectConnectionState.Handshake;
                    Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose, "foo0 handled", GetType().Name);
                }
                else
                {
                    Trace.WriteLineIf(Settings.TraceSwitch.TraceWarning, "foo0 expected, but it was: " + initialData, GetType().Name);
                    Dispose();
                    return;
                }
            }
            break;

            case DirectConnectionState.Closed:
                break;
            }
        }