public bool LoginAS(string asHost, int asPort, string accountName, string password, string randomSeed, int channelId, string version, DeviceInfoPro deviceInfo)
        {
            connection = NetworkManager.GetInstance().CreateConnection(this.type);
            connection.SetNetworkInitializer(networkInitializer);
            try
            {
                connection.ConnectAsync(new IPEndPoint(IPAddress.Parse("0.0.0.0"), 0), new IPEndPoint(NetUtil.GetIPV4Address(asHost), asPort));

                CA_LoginAuthMessage loginMsg = new CA_LoginAuthMessage();
                loginMsg.Protocol.email      = accountName;
                loginMsg.Protocol.password   = password;
                loginMsg.Protocol.channelID  = channelId;
                loginMsg.Protocol.randomSeed = randomSeed;
                loginMsg.Protocol.version    = version;
                loginMsg.Protocol.deviceInfo = deviceInfo;

                waitSendAuthMsg = loginMsg;
                //connection.Send(waitSendAuthMsg);
                return(true);
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                Console.Write(ex.ToString());
                return(false);
            }


            //If using ConnectAsync, need set the connection handler
            //ASLoginConnectionHandler connectionHandler = new ASLoginConnectionHandler(req);
            //connectionHandler.ClientHelper = this;
            //messageInitializer.SetConnectionActiveHandler(connectionHandler);
        }
 /// <summary>
 /// Returns whether or not a message received on a peer channel should be propagated, and if so, the destination of the message.
 /// </summary>
 /// <param name="message">The message to evaluate for propagation.</param>
 /// <param name="origination">A <see cref="T:System.ServiceModel.PeerMessageOrigination"/> enumeration value that specifies the origin (local or remote) of the message under evaluation.</param>
 /// <returns>
 /// A <see cref="T:System.ServiceModel.PeerMessagePropagation"/> enumeration value that indicates the destination of the message (local, remote, both, or no propagation at all).
 /// </returns>
 public override PeerMessagePropagation ShouldMessagePropagate(Message message, PeerMessageOrigination origination)
 {
     PeerMessagePropagation destination = PeerMessagePropagation.LocalAndRemote;
     if (origination == PeerMessageOrigination.Local)
         destination = PeerMessagePropagation.Remote;
     return destination;
 }
        public bool BindAccountAS(string asHost, int asPort, string accountName, string password, string randomSeed, int channelId, string version, DeviceInfoPro deviceInfo)
        {
            connection = NetworkManager.GetInstance().CreateConnection(this.type);
            connection.SetNetworkInitializer(networkInitializer);
            try
            {
                connection.ConnectAsync(new IPEndPoint(IPAddress.Parse("0.0.0.0"), 0), new IPEndPoint(NetUtil.GetIPV4Address(asHost), asPort));

                CA_BindAccountMessage message = new CA_BindAccountMessage();
                message.Protocol.email      = accountName;
                message.Protocol.password   = password;
                message.Protocol.channelID  = channelId;
                message.Protocol.randomSeed = randomSeed;
                message.Protocol.version    = version;
                message.Protocol.deviceInfo = deviceInfo;

                waitSendAuthMsg = message;
                return(true);
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                Console.Write(ex.ToString());
                return(false);
            }
        }
 public void Resolve(string protocol)
 {
     Message message = new Message();
     List<byte> guid = Guid.NewGuid().ToByteArray().Take(2).ToList();
     requestId = (ushort)(guid[0] * byte.MaxValue + guid[1]);
     message.ID = requestId;
     message.Questions.Add(new Question(protocol));
     Send(message, EndPoint);
 }
        private bool _SendMessage(Network.Message message)
        {
            if (connection != null)
            {
                return(connection.Send(message));
            }

            return(false);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="UnconnectedMessageEventArgs"/> class.
        /// </summary>
        /// <param name="message">The message received connectionlessly.</param>
        /// <param name="from">Where the message came from.</param>
        /// <exception cref="ArgumentNullException"><paramref name="message"/> or <paramref name="from"/> is <c>null</c>.</exception>
        public UnconnectedSendMessageEventArgs(Message message, EndPoint to)
        {
            if (message == null)
                throw new ArgumentNullException("message");
            if (to == null)
                throw new ArgumentNullException("to");

            Message = message;
            To = to;
        }
 public bool Send(Message message, IPEndPoint ep)
 {
     try
     {
         byte[] byteMessage = message.GetBytes();
         client.BeginSendTo(byteMessage, 0, byteMessage.Length, ep, OnSendTo, null);
         return true;
     }
     catch (Exception)
     {
         return false;
     }
 }
Example #8
0
        public void OnReceivedTick(Message _message)
        {
            bool needUpdateFlags = false;

            PlayerTick msg = PlayerTick.Deserialize(_message.read, this.m_lastPlayerTickPacket, true);

            this.PlayerOwner.PlayerButtons = (E_PlayerButton)msg.inputState.buttons;
            if (ModelState.Equal(this.PlayerOwner.PlayerModelState, msg.modelState) == false)
            {
                this.PlayerOwner.PlayerModelState = msg.modelState;
                needUpdateFlags = true;
            }

            if (this.PlayerOwner.IsSleeping)
            {
                if (this.PlayerOwner.HasPlayerButton(E_PlayerButton.FIRE_PRIMARY) || this.PlayerOwner.HasPlayerButton(E_PlayerButton.FIRE_SECONDARY) || this.PlayerOwner.HasPlayerButton(E_PlayerButton.JUMP))
                {
                    this.PlayerOwner.SetPlayerFlag(E_PlayerFlags.Sleeping, false);
                    needUpdateFlags = true;
                }
            }
            else
            {
                if ((this.PlayerOwner.ActiveItem?.UID ?? 0) != msg.activeItem)
                {
                    this.PlayerOwner.OnChangeActiveItem(Item.ListItemsInWorld.TryGetValue(msg.activeItem, out Item item) ? item : null);
                    needUpdateFlags = true;
                }
                if (this.PlayerOwner.Position != msg.position || this.PlayerOwner.Rotation != msg.inputState.aimAngles)
                {
                    this.PlayerOwner.Position = msg.position;
                    this.PlayerOwner.Rotation = msg.inputState.aimAngles;
                    this.PlayerOwner.OnPositionChanged();
                }
            }

            if (needUpdateFlags == true)
            {
                this.PlayerOwner.SendNetworkUpdate_PlayerFlags();
            }
            this.m_lastPlayerTickPacket = msg.Copy();
        }
Example #9
0
        private void OnFrontendMessageReceived(IRemote conn, Message msg)
        {
            Console.WriteLine("OnFrontendMessageReceived id:{0} ip:{1} port:{2}", conn.Id, conn.RemoteIp, conn.RemotePort);
            Console.WriteLine("OnFrontendMessageReceived msgLen:{0}", msg.Buffer.Length);

            // forward frontend msg to backend, 
            // which is based on uuid associated with the msg

            var stream = new MemoryStream(msg.Buffer);
            var br = new BinaryReader(stream);
            var msgType = (GateMessage)br.ReadByte();

            Console.WriteLine("OnFrontendMessageReceived msgType:{0}", msgType);

            switch (msgType)
            {
                case GateMessage.Send:
                    {
                        var uuid = new Guid(br.ReadBytes(UuidLen));
                        var key = br.ReadInt32();

                        Console.WriteLine("OnFrontendMessageReceived key:{0} uuid:{1}", key, uuid);
                        broker.RouteData(key, uuid, msg.Buffer, msg.Buffer.Length - 17, 17);
                    }
                    break;
                case GateMessage.Handshake:
                    {
                        var uuid = new Guid(br.ReadBytes(UuidLen));

                        Console.WriteLine("OnFrontendMessageReceived uuid:{0}", uuid);

                        uuidToClients[uuid] = conn;

                        // send back a ack
                    }
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
        public bool ChangePasswordAS(string asHost, int asPort, string accountName, string oldPassword, string newPassword)
        {
            connection = NetworkManager.GetInstance().CreateConnection(this.type);
            connection.SetNetworkInitializer(networkInitializer);
            try
            {
                connection.ConnectAsync(new IPEndPoint(IPAddress.Parse("0.0.0.0"), 0), new IPEndPoint(NetUtil.GetIPV4Address(asHost), asPort));

                CA_ChangePasswordMessage message = new CA_ChangePasswordMessage();
                message.Protocol.email       = accountName;
                message.Protocol.newPassword = newPassword;
                message.Protocol.oldPassword = oldPassword;

                waitSendAuthMsg = message;
                return(true);
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                Console.Write(ex.ToString());
                return(false);
            }
        }
Example #11
0
        private void OnBackendMessageReceived(IRemote conn, Message msg)
        {
            Console.WriteLine("OnBackendMessageReceived id:{0} ip:{1} port:{2}", conn.Id, conn.RemoteIp, conn.RemotePort);
            Console.WriteLine("OnBackendMessageReceived msgLen:{0}", msg.Buffer.Length);

            // forward backend msg to frontend, 
            // which is based on forwardId associated with the msg

            var stream = new MemoryStream(msg.Buffer);
            var br = new BinaryReader(stream);
            var msgType = (GateMessage)br.ReadByte();

            Console.WriteLine("OnBackendMessageReceived msgType:{0}", msgType);

            switch (msgType)
            {
                #region Broker Component
                case GateMessage.Subscribe:
                    {
                        var key = br.ReadInt32();
                        var uuid = new Guid(br.ReadBytes(UuidLen));

                        Console.WriteLine("OnBackendMessageReceived key:{0} uuid:{1}", key, uuid);
                        broker.AddRouting(key, uuid, conn);
                    }
                    break;
                case GateMessage.Unicast:
                    {
                        var uuid = new Guid(br.ReadBytes(UuidLen));

                        Console.WriteLine("OnBackendMessageReceived uuid:{0}", uuid);
                        // uuid to client IRemote
                        IRemote remote = null;
                        if (!uuidToClients.TryGetValue(uuid, out remote))
                        {
                            return;
                        }

                        PushReceivedMessage(remote, msg.Buffer, msg.Buffer.Length - UuidLen - 1, UuidLen + 1);
                    }
                    break;
                #endregion

                #region Forward Component
                case GateMessage.AddForward:
                case GateMessage.RemoveForward:
                    {
                        var uuid = new Guid(br.ReadBytes(UuidLen));
                        var forwardId = br.ReadInt32();

                        Console.WriteLine("OnBackendMessageReceived uuid:{0} forwardId:{1}", uuid, forwardId);
                        // todo 
                        // what if backend control forward first,
                        // then client handshake?
                        switch (msgType)
                        {
                            case GateMessage.AddForward:
                                forwardManager.AddForward(uuid, forwardId);
                                break;
                            case GateMessage.RemoveForward:
                                forwardManager.RemoveForward(uuid, forwardId);
                                break;
                        }
                    }
                    break;
                case GateMessage.Multicast:
                    {
                        var forwardId = br.ReadInt32();

                        Console.WriteLine("OnBackendMessageReceived forwardId:{0}", forwardId);
                        // forwardId to client IRemotes
                        foreach (var uuid in forwardManager.GetForwardGroup(forwardId))
                        {
                            IRemote remote = null;
                            if (uuidToClients.TryGetValue(uuid, out remote))
                            {
                                PushReceivedMessage(remote, msg.Buffer, msg.Buffer.Length - 4 - 1, 4 + 1);
                            }
                        }
                    }
                    break;
                #endregion
            }
        }
Example #12
0
        public void SendUnconnectedMessage(Message message, IPEndPoint endPoint)
        {
            if (message == null)
                throw new ArgumentNullException("message");
            if (endPoint == null)
                throw new ArgumentNullException("endPoint");

            var outgoingMsg = netPeer.CreateMessage();
            message.WritePayload(outgoingMsg);

            netPeer.SendUnconnectedMessage(outgoingMsg, endPoint);

            //Trace.WriteLine("Sent " + ((CustomMessageType)message.MessageType).ToString() + " unconnected to " + endPoint.ToString() + ".");

            if (OnUnconnectedMessageSent != null)
                OnUnconnectedMessageSent(this, new UnconnectedSendMessageEventArgs(message, endPoint));
        }
Example #13
0
        public void SendMessage(Message message, NetDeliveryMethod deliveryMethod, int sequenceChannel)
        {
            var outgoingMessage = netPeer.CreateMessage();
            message.WritePayload(outgoingMessage);

            if (Key != null)
            {
                IsAuthenticated = true;
                //outgoingMessage.Encrypt(cryptoAlgorithm);
            }

            netPeer.SendMessage(outgoingMessage, Connections, deliveryMethod, sequenceChannel);

            //Trace.WriteLine("Sent " + ((CustomMessageType)message.MessageType).ToString() + ".");

            if (OnConnectedMessageSent != null)
                OnConnectedMessageSent(this, new MessageEventArgs(this, message));
        }
Example #14
0
 public void SendMessage(Message message, NetDeliveryMethod deliveryMethod)
 {
     SendMessage(message, deliveryMethod, 0);
 }
Example #15
0
 public void SendMessage(Message message)
 {
     SendMessage(message, NetDeliveryMethod.ReliableOrdered, 0);
 }
Example #16
0
        // io thread entrance
        private void MessageQueueEnqueue(byte[] buf)
        {
            var msg = new Message(buf);

            // msgQueue will be polled when user thread invoke  
            lock (msgQueue.Lock) 
            {
                msgQueue.In.Enqueue(msg);
            }
        }
 public bool SendMessage(Network.Message message)
 {
     return(_SendMessage(message));
 }
Example #18
0
 public void handleMessage(IConnection connection, Network.Message message)
 {
 }
Example #19
0
 private void MessageQueueEnqueue(byte[] buf, int len)
 {
     var msg = new Message(buf, len);
     lock (msgQueue.Lock) // 消息被压入队列,供主线程的 RefreshMessageQueue 处理
     {
         msgQueue.In.Enqueue(msg);
     }
 }
Example #20
0
        private void OnHandleMessageReceived(Message msg)
        {
            var stream = new MemoryStream(msg.Buffer);
            var br = new BinaryReader(stream);

            var mode = (GateMessage)br.ReadByte();

            if (mode == GateMessage.Received)
            {
                var serviceId = br.ReadInt32();
                var rpcMode = MetaData.GetMode(br.ReadByte());
                var toQuery = rpcMode == Mode.Return ? delegates : implements;
                IMessageConsumer msgConsumer;

                if (toQuery.TryGetValue(serviceId, out msgConsumer))
                {
                    msgConsumer.OnReceiveMessage(rpcMode, msg.Buffer, 1+4+1, null);
                }
            }
        }