Ejemplo n.º 1
0
        private void onMessage()
        {
            TCPMessage tcpMsg = server.GetMessage();

            if (tcpMsg == null)
            {
                return;
            }
            string msgStr = tcpMsg.Message;

            Debug.Log(msgStr);
            try
            {
                Dictionary <string, string> form = JsonHelper.Deserialize <Dictionary <string, string> >(msgStr);
                if (form == null || form.Count == 0)
                {
                    Debug.Log("收到无效消息");
                }
                MessageHandler handler = HandlerFactory.Produce(form, tcpMsg.SourcePoint, this);
                handler.Handle();
            }
            catch (Exception e)
            {
                Debug.Error(e.Message);
            }
        }
Ejemplo n.º 2
0
        private void DoReceiveData(IAsyncResult ar)
        {
            if (stopRecieve)
            {
                return;
            }

            TcpClientInfo internalClient = (TcpClientInfo)ar.AsyncState;
            NetworkStream networkStream;
            int           read = 0;

            try
            {
                networkStream = internalClient.client.GetStream();
                read          = networkStream.EndRead(ar);
            }
            catch
            {
                read = 0;
            }

            if (read == 0)
            {
                ///客户端已经断开
                lock (Clients)
                {
                    Clients.Remove(internalClient);
                }

                //触发客户端断开事件
                OnDisconnectServer?.Invoke(internalClient.socket);
                try
                {
                    internalClient.socket.Close();
                    internalClient.socket.Dispose();
                    internalClient = null;
                }
                catch (Exception e)
                {
                }
                return;
            }
            byte[] recvData = new byte[read];
            Array.Copy(internalClient.RecvBuff, 0, recvData, 0, read);
            //string ssss = Encoding.UTF8.GetString(recvData);

            //将收到的消息存入队列
            TCPMessage message = new TCPMessage(internalClient.client.Client, this.socket, recvData);

            recvQueue.Enqueue(message);

            //发送消息到达通知
            OnMessageArrived?.Invoke();
            //OnRecivedMessage?.Invoke(internalClient.socket, recvData);
            //Console.WriteLine(internalClient.Ip + ":" + internalClient.Port + "有消息:" + ssss);
            internalClient.socket.BeginReceive(internalClient.RecvBuff, 0, internalClient.RecvBuff.Length, SocketFlags.None, new AsyncCallback(DoReceiveData), internalClient);
        }
Ejemplo n.º 3
0
        private void DoSendMsg()
        {
            Console.WriteLine("发送线程启动");

            while (!stopSend)
            {
                if (sendQueue.Count > 0)
                {
                    TCPMessage message = sendQueue.Dequeue();
                    //Console.WriteLine("要发送消息啦" + message.Message);
                    //没有指定发送目标 向所有连接的客户端发送
                    if (message.TargetPoint == null)
                    {
                        foreach (TcpClientInfo tcpinfo in Clients)
                        {
                            tcpinfo.socket.Send(message.data, message.data.Length, SocketFlags.None);
                        }
                        OnMessageSend?.Invoke(null);
                    }
                    else
                    {
                        TcpClientInfo clientInfo = FindTcpClinet(message.TargetPoint);
                        Socket        To         = null;
                        //通过Socket没有找到
                        if (clientInfo == null)
                        {
                            To = FindClient(message.TargetIP, message.TargetPort);
                            if (To == null)
                            {
                                //TODO:报错,没有找到客户端
                                Error?.Invoke("发送数据时没有找到目标");
                                continue;
                            }
                        }
                        else
                        {
                            To = clientInfo.socket;
                        }

                        To.Send(message.data, message.data.Length, SocketFlags.None);
                        OnMessageSend?.Invoke(To);
                    }
                }
                Thread.Sleep(10);
            }
        }
Ejemplo n.º 4
0
        public void Send(Socket client, string message)
        {
            TCPMessage msg = new TCPMessage(socket, client, message);

            Send(msg);
        }
Ejemplo n.º 5
0
 public void Send(TCPMessage message)
 {
     sendQueue.Enqueue(message);
 }