Beispiel #1
0
        /// <summary>
        /// 根据Id获取消息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static KevMessageBoxModel GetMessage(long id)
        {
            KevMessageBoxModel kmbModel = null;

            if (message_dic.ContainsKey(id))
            {
                message_dic.TryRemove(id, out kmbModel);
            }
            return(kmbModel);
        }
Beispiel #2
0
        /// <summary>
        /// 处理超时消息
        /// </summary>
        /// <param name="state"></param>
        private void HandleTimeOutMessage(object state)
        {
            KevMessageBoxModel ksbModel = state as KevMessageBoxModel;

            if (ksbModel == null)
            {
                return;
            }

            UDPClientRouteHelper.GetInstanse().HandleTimeOutMessage(ksbModel);
        }
Beispiel #3
0
        /// <summary>
        /// 处理超时消息
        /// </summary>
        /// <param name="ksbModel"></param>
        internal void HandleTimeOutMessage(KevMessageBoxModel ksbModel)
        {
            if (ksbModel == null || ksbModel.SocketModel == null || ksbModel.IP == null)
            {
                return;
            }

            IUDPClientDelegate del = GetDelegate(ksbModel.SocketModel.MessageType);

            if (del != null)
            {
                del.HandleTimeoutMessage(ksbModel);
            }
        }
Beispiel #4
0
 /// <summary>
 /// 处理消息盒子中的过期消息
 /// </summary>
 /// <param name="ksbModel"></param>
 internal void HandleTimeOutMessage(KevMessageBoxModel ksbModel)
 {
 }
Beispiel #5
0
 /// <summary>
 /// 将Id添加到结尾
 /// </summary>
 /// <param name="message"></param>
 /// <param name="id"></param>
 public static void Enqueue(long id, KevMessageBoxModel message)
 {
     messageId_queue.Enqueue(id);
     message_dic.TryAdd(id, message);
 }
Beispiel #6
0
        /// <summary>
        /// 开始
        /// </summary>
        /// <returns></returns>
        public bool Start()
        {
            isRuning = true;

            Socket socket_client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            if (this.ServerIPPoint == null)
            {
                throw new Exception("IPPoint must be a valid value");
            }

            socket_client.SendTo(Encoding.UTF8.GetBytes(Handshake1), ServerIPPoint);
            SocketRegister.Add(SocketClient, socket_client);

            Thread thread = new Thread(() =>
            {
                EndPoint remoteEP = (EndPoint) new IPEndPoint(IPAddress.Any, 0);

                while (isRuning)
                {
                    int recv;
                    byte[] datas = new byte[8 * 1024];
                    try
                    {
                        recv = socket_client.ReceiveFrom(datas, ref remoteEP);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        continue;
                    }

                    UDPModel udpModel = new UDPModel {
                        Body = datas, IPPoint = (IPEndPoint)remoteEP, Length = recv
                    };
                    ThreadPool.QueueUserWorkItem(new WaitCallback(HandleMessage), udpModel);
                }
            });

            thread.IsBackground = true;
            thread.Start();

            Thread thread_deepMessage = new Thread(() =>
            {
                while (isRuning)
                {
                    Thread.Sleep(100);
                    KevMessageBoxModel kmbModel = KevMessageBox.GetFirstMessage();
                    if (kmbModel == null)
                    {
                        continue;
                    }

                    if (kmbModel.TimeOut.Ticks <= DateTime.Now.Ticks)
                    {
                        //跳转到超时
                        ThreadPool.QueueUserWorkItem(new WaitCallback(HandleTimeOutMessage), kmbModel);
                    }
                    else
                    {
                        KevMessageBox.Enqueue(kmbModel.SocketModel.MessageId, kmbModel);
                    }
                }
            });

            thread_deepMessage.IsBackground = true;
            thread_deepMessage.Start();

            return(true);
        }
Beispiel #7
0
        /// <summary>
        /// 处理消息
        /// </summary>
        /// <param name="state"></param>
        private void HandleMessage(object state)
        {
            UDPModel uModel = state as UDPModel;

            if (uModel == null)
            {
                return;
            }

            string message = Encoding.UTF8.GetString(uModel.Body, 0, uModel.Length);

            if (string.IsNullOrEmpty(message))
            {
                return;
            }

            Socket socket = SocketRegister.Get(SocketClient);

            if (socket == null)
            {
                return;
            }

            if (message == Handshake2)
            {
                UDPSocketServer.SendMessage(socket, Handshake3, uModel.IPPoint);
            }

            KevSocketModel ksModel = JsonHelper.ParseFromJson <KevSocketModel>(message);

            if (ksModel == null)
            {
                return;
            }

            Console.WriteLine("HandleMessage" + ksModel.MessageId + "::" + ksModel.MessageType);

            if (ksModel.NetworkType == NetworkType.Request)
            {
                long clientId = KevRegister.Get <long>(UDPPrimaryKey.Client_ThisDeviceId);

                //非指向本机请求一律抛弃
                if (ksModel.ReceiveDeviceId != clientId)
                {
                    return;
                }

                int responseCode = UDPClientRouteHelper.GetInstanse().HandleRequestMessage(ksModel, uModel);
                if (responseCode == ResponseCode.NoResponse)
                {
                    return;
                }

                ksModel.NetworkType  = NetworkType.Response;
                ksModel.ResponseCode = responseCode;

                //做出相应

                UDPSocketServer.SendMessage(socket, ksModel, uModel.IPPoint);
            }

            if (ksModel.NetworkType == NetworkType.Response)
            {
                //从消息盒子中移除数据
                KevMessageBoxModel kmbModel = KevMessageBox.GetMessage(ksModel.MessageId);
                if (kmbModel != null)
                {
                    UDPClientRouteHelper.GetInstanse().HandleResponseMessage(ksModel, uModel);
                }
            }
        }