Пример #1
0
        internal override void OnInitialization()
        {
            base.OnInitialization();

            _helper = Helper as INetworkHelper;
            _helper.LoadProtocolChannels(ChannelTypes);
            _helper.BeginConnectServerEvent += (cha) =>
            {
                BeginConnectServerEvent?.Invoke(cha);
            };
            _helper.ConnectServerSuccessEvent += (cha) =>
            {
                ConnectServerSuccessEvent?.Invoke(cha);
            };
            _helper.ConnectServerFailEvent += (cha) =>
            {
                ConnectServerFailEvent?.Invoke(cha);
            };
            _helper.DisconnectServerEvent += (cha) =>
            {
                DisconnectServerEvent?.Invoke(cha);
            };
            _helper.SendMessageEvent += (cha) =>
            {
                SendMessageEvent?.Invoke(cha);
            };
            _helper.ReceiveMessageEvent += (cha, mes) =>
            {
                ReceiveMessageEvent?.Invoke(cha, mes);
            };
        }
        private void SendMessageHandler(byte[] data)
        {
            SendMessage sendMessage = NetworkUtils.Deserialize <SendMessage>(data);

            Application.Current.Dispatcher.BeginInvoke(new Action(() => {
                SendMessageEvent?.Invoke(sendMessage);
            }));
        }
Пример #3
0
 /*
  * A private method which sends the two clients the maze so that they can
  * start playing it.
  */
 private void SendPlayersMaze()
 {
     foreach (KeyValuePair <string, Position> player in players)
     {
         string  response = maze.toJSON();
         JObject json     = JObject.Parse(response);
         SendMessageEvent?.Invoke(player.Key, json);
     }
 }
Пример #4
0
        private void SendMessage(string msg)
        {
            var timeStamp = DateTime.Now.ToString("[yyyy/MM/dd HH:mm:ss.fff]");

            msg = $"[{Thread.CurrentThread.ManagedThreadId}]" + timeStamp + msg;
            if (SendMessageEvent != null)
            {
                SendMessageEvent.Invoke(this, msg);
            }
        }
Пример #5
0
        /*
         * When one player wants to cancel the game, the other user is alerted
         */
        public void CancelGame(string player)
        {
            // We get the stream (saved from previously) to send the other player
            string other = GetOtherPlayer(player);

            // We send the message
            if (other != null)
            {
                SendMessageEvent?.Invoke(other, null);
            }
        }
Пример #6
0
        /*
         * This method alerts the second player that the other player has moved
         */
        private void AlertOtherPlayer(string mover, string direction)
        {
            // Generate the Json Object to send
            JObject playerAlert = new JObject();

            playerAlert["Name"]      = maze.name;
            playerAlert["Direction"] = direction;
            // We get the stream (saved from previously) to send the other player
            string other = GetOtherPlayer(mover);

            // We send the message
            SendMessageEvent?.Invoke(other, playerAlert);
        }
Пример #7
0
        internal override void OnInitialization()
        {
            base.OnInitialization();

            //加载通信协议通道
            for (int i = 0; i < ChannelTypes.Count; i++)
            {
                Type type = ReflectionToolkit.GetTypeInRunTimeAssemblies(ChannelTypes[i]);
                if (type != null)
                {
                    if (type.IsSubclassOf(typeof(ProtocolChannelBase)))
                    {
                        if (!_protocolChannels.ContainsKey(type))
                        {
                            _protocolChannels.Add(type, Activator.CreateInstance(type) as ProtocolChannelBase);
                        }
                    }
                    else
                    {
                        throw new HTFrameworkException(HTFrameworkModule.Network, "加载通信协议通道失败:通信协议通道类 " + ChannelTypes[i] + " 必须实现接口:IProtocolChannel!");
                    }
                }
                else
                {
                    throw new HTFrameworkException(HTFrameworkModule.Network, "加载通信协议通道失败:丢失通信协议通道类 " + ChannelTypes[i] + "!");
                }
            }

            //初始化通道
            foreach (var channel in _protocolChannels)
            {
                channel.Value.OnInitialization();
                channel.Value.SendMessageEvent += (cha) =>
                {
                    SendMessageEvent?.Invoke(cha);
                };
                channel.Value.ReceiveMessageEvent += (cha, message) =>
                {
                    ReceiveMessageEvent?.Invoke(cha, message);
                };
                channel.Value.DisconnectServerEvent += (cha) =>
                {
                    DisconnectServerEvent?.Invoke(cha);
                };
            }
        }
Пример #8
0
        /// <summary>
        /// 发送消息(不需要保持连接的协议)
        /// </summary>
        private void SendMessageNoConnect()
        {
            while (_isEnableThread)
            {
                if (_isCanSend && _sendDataBuffer.Count > 0)
                {
                    int sendCount = Client.SendTo(_sendDataBuffer[0], Main.m_Network.ServerEndPoint);
                    if (sendCount > 0)
                    {
                        _sendDataBuffer.RemoveAt(0);

                        Main.Current.QueueOnMainThread(() =>
                        {
                            SendMessageEvent?.Invoke(this);
                        });
                    }
                }
            }
        }
Пример #9
0
        /// <summary>
        /// 发送消息(需要保持连接的协议)
        /// </summary>
        private void SendMessageNeedConnect()
        {
            while (_isEnableThread)
            {
                if (IsConnect && _isCanSend && _sendDataBuffer.Count > 0)
                {
                    int sendCount = Client.Send(_sendDataBuffer[0], _sendDataBuffer[0].Length, 0);
                    if (sendCount > 0)
                    {
                        _sendDataBuffer.RemoveAt(0);

                        Main.Current.QueueOnMainThread(() =>
                        {
                            SendMessageEvent?.Invoke(this);
                        });
                    }
                }
            }
        }
        /// <summary>
        /// 加载通信管道
        /// </summary>
        /// <param name="channelTypes">启用的通信协议通道类型</param>
        public void LoadProtocolChannels(List <string> channelTypes)
        {
            for (int i = 0; i < channelTypes.Count; i++)
            {
                Type type = ReflectionToolkit.GetTypeInRunTimeAssemblies(channelTypes[i]);
                if (type != null)
                {
                    if (type.IsSubclassOf(typeof(ProtocolChannelBase)))
                    {
                        if (!ProtocolChannels.ContainsKey(type))
                        {
                            ProtocolChannels.Add(type, Activator.CreateInstance(type) as ProtocolChannelBase);
                        }
                    }
                    else
                    {
                        throw new HTFrameworkException(HTFrameworkModule.Network, "加载通信协议通道失败:通信协议通道类 " + channelTypes[i] + " 必须继承至基类:ProtocolChannelBase!");
                    }
                }
                else
                {
                    throw new HTFrameworkException(HTFrameworkModule.Network, "加载通信协议通道失败:丢失通信协议通道类 " + channelTypes[i] + "!");
                }
            }

            foreach (var channel in ProtocolChannels)
            {
                channel.Value.OnInitialization();
                channel.Value.SendMessageEvent += (cha) =>
                {
                    SendMessageEvent?.Invoke(cha);
                };
                channel.Value.ReceiveMessageEvent += (cha, message) =>
                {
                    ReceiveMessageEvent?.Invoke(cha, message);
                };
                channel.Value.DisconnectServerEvent += (cha) =>
                {
                    DisconnectServerEvent?.Invoke(cha);
                };
            }
        }
Пример #11
0
 /// <summary>
 /// 发布事件
 /// </summary>
 /// <param name="message"></param>
 protected void OnUiShowMessage(DataToolsUIMsg message)
 {
     SendMessageEvent?.Invoke(message);
 }
Пример #12
0
 public void InvokeMessage(Message message)
 {
     SendMessageEvent?.Invoke(this, message);
 }
Пример #13
0
 public void OnSendMessageEvent(object sender, ByteArrayEventArgs e)
 {
     SendMessageEvent?.Invoke(sender, e);
 }