Ejemplo n.º 1
0
        private void StartReceiveMsg(TcpClient client, CancellationToken ct)
        {
            string from = client.Client.RemoteEndPoint.ToString();

            Logger.Log.InfoFormat("Ready receive data from {0}", from);

            //利用TcpClient对象GetStream方法得到网络流
            NetworkStream stream = client.GetStream();

            stream.ReadTimeout = 600000;
            Byte[] data;

            while (!ct.IsCancellationRequested)
            {
                try
                {
                    Read(stream, out data);
                    Logger.Log.InfoFormat("Received msg from {0}: {1}", from, BitConverter.ToString(data));
                    // 回应ACK
                    Byte[] ack = { 0x41, 0x43, 0x4B };
                    stream.Write(ack, 0, ack.Length);

                    // 解析消息内容
                    IXMessage msg = new IXMessage();
                    msg = (IXMessage)tool.BytesToStruct(data, msg.GetType());
                    EventHandle(msg);
                }
                catch (Exception e)
                {
                    Logger.Log.ErrorFormat("{0}:{1}", from, e.Message);
                    stream.Close();
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        private void EventHandle(IXMessage ix)
        {
            Msg msg = new Msg();
            // 默认取SrcID
            string display = Encoding.ASCII.GetString(ix.SrcID).TrimEnd('\0');

            if (display.CompareTo(master) == 0)
            {
                display = Encoding.ASCII.GetString(ix.DstID).TrimEnd('\0');
            }


            // 通话
            if (ix.EventType[0] == 0x30 && ix.EventType[1] == 0x30)
            {
                if (ix.EventType[2] == 0x30 && (ix.EventType[3] == 0x30))
                {
                    msg.type = EventType.Call;
                }
                else if (ix.EventType[2] == 0x30 && (ix.EventType[3] == 0x31))
                {
                    msg.type = EventType.Connect;
                }
                else if (ix.EventType[2] == 0x31 && (ix.EventType[3] == 0x31))
                {
                    msg.type = EventType.HangUp;
                }
                else
                {
                    Logger.Log.ErrorFormat("忽略事件:事件({0}),分机({1})", BitConverter.ToString(ix.EventType), display);
                    return;
                }
            }
            else
            {
                return;
            }

            // 通知主线程
            msg.display = display;
            handler?.Invoke(this, msg);
        }