Пример #1
0
        /// <summary>
        /// 发送消息给服务器
        /// </summary>
        /// <param name="ksModel"></param>
        /// <returns></returns>
        public bool SendMessage <T>(KevSocketModel <T> ksModel)
        {
            Socket socket = SocketRegister.Get(SocketClient);

            if (socket == null)
            {
                return(false);
            }

            bool isSuccess = UDPSocketServer.SendMessage(socket, ksModel, this.ServerIPPoint);

            if (isSuccess)
            {
                Console.WriteLine("SendMessage" + ksModel.MessageId + "::" + ksModel.MessageType);

                KevMessageBox.Enqueue(ksModel.MessageId, new KevMessageBoxModel
                {
                    IP          = ServerIPPoint,
                    SocketModel = ksModel,
                    TimeOut     = DateTime.Now.AddSeconds(this.TimeOutSeconds)
                });
            }

            return(isSuccess);
        }
Пример #2
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);
        }
Пример #3
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);
                }
            }
        }