Ejemplo n.º 1
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.º 2
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;
            }
        }