示例#1
0
        protected override bool HandleMessages(byte[] message)
        {
            try
            {
                HandShakeRequest msg = BsonUtilities.Deserialise <HandShakeRequest>(message);
                Console.WriteLine("HandShakeRequest rec");
                if (Config.Password.Equals(msg.AuthPassword))
                {
                    State = HandShakeState.Authorised;
                }
                else
                {
                    State = HandShakeState.NotAuthorised;
                }

                var waiter = new TaskCompletionSource <bool>();

                this.SendMessage(new HandShakeOutcome()
                {
                    MessageType = State == HandShakeState.Authorised ? MessageTypeEnum.AuthRequestOk : MessageTypeEnum.AuthRequestError,
                    ServerGuid  = State == HandShakeState.Authorised ? this._server.ServerGuid : Guid.Empty
                }, waiter);

                waiter.Task.Wait();
                if (waiter.Task.Result && State == HandShakeState.Authorised)
                {
                    if (msg.MessageType == MessageTypeEnum.ClientAuthRequest)
                    {
                        _server.ClientConnected(msg.RegisteredGuid, TemporaryID);
                    }
                    else if (msg.MessageType == MessageTypeEnum.ServerAuthRequest)
                    {
                        _server.ServerConnected(msg.RegisteredGuid, TemporaryID);
                    }
                }
                else
                {
                    _server.ConnectionFailed(this, TemporaryID);
                }
            }
            catch (Exception ex)
            {
                this.State = HandShakeState.ProtocolError;
                _server.ConnectionFailed(this, TemporaryID);
            }
            return(false);
        }