Пример #1
0
        public Task <MsResult <bool> > ReplyTerminal(byte module, byte method, object content, UInt32 identify, bool isSuccess, string exchangeId)
        {
            return(Task.Run(() =>
            {
                MsResult <bool> msResult = new MsResult <bool>();

                byte[] buffer = PacketHelper.Packet(module, method, isSuccess ? RequestType.ResponseSuccess : RequestType.ResponseFaild, identify, content, exchangeId);
                lock (objSend)
                {
                    if (client != null && client.Client != null && client.Client.Connected)
                    {
                        try
                        {
                            client.GetStream().Write(buffer, 0, buffer.Length);
                        }
                        catch (Exception ex)
                        {
                            msResult.IsSuccess = false;
                            msResult.Error = ex.Message;
                        }
                    }
                    else
                    {
                        msResult.IsSuccess = false;
                        msResult.Error = func.Invoke("99997");
                    }
                }
                return msResult;
            }));
        }
Пример #2
0
        private Task ReceiveMessage()
        {
            return(Task.Run(() =>
            {
                try
                {
                    byte[] buffer = new byte[1024 * 1024 * 10];
                    List <byte> tmpBuffer = new List <byte>();
                    while (true)
                    {
                        if (client == null || client.Client == null)
                        {
                            throw new Exception("client is null");
                        }

                        int count = client.Client.Receive(buffer);
                        if (count == 0)
                        {
                            throw new Exception("time out");
                        }

                        tmpBuffer.AddRange(buffer.Take(count));
                        tmpBuffer = PacketHelper.Divide(tmpBuffer, new Action <Packet>(packet =>
                        {
                            if (packet.IsReply)
                            {
                                lock (objMessage)
                                {
                                    if (messages.ContainsKey(packet.Identify))
                                    {
                                        messages[packet.Identify].Add(packet);
                                    }
                                    else
                                    {
                                        messages[packet.Identify] = new List <Packet>()
                                        {
                                            packet
                                        }
                                    };
                                }
                            }
                            else
                            {
                                requestPackets.Enqueue(packet);
                            }
                        }));
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"ReceiveMessage Exception{ex.Message}");
                    NotifyConnectStateChanged(new ConnectStateArgs {
                        ConnectState = ConnectState.Faild
                    }, ex.Message);
                }
            }));
        }
Пример #3
0
        public Task <MsResult <bool> > SendMessageNoRelayAsync(byte module, byte method, object content, string exchangeId = "")
        {
            return(Task.Run(() =>
            {
                MsResult <bool> msResult = new MsResult <bool>();
                UInt32 identifyVar = 0;
                lock (objIdentify)
                {
                    identify++;
                    identifyVar = identify;
                }

                byte[] buffer = PacketHelper.Packet(module, method, RequestType.RequestNoReply, identifyVar, content, exchangeId);
                lock (objSend)
                {
                    if (client != null && client.Client != null && client.Client.Connected)
                    {
                        try
                        {
                            client.GetStream().Write(buffer, 0, buffer.Length);
                        }
                        catch (Exception ex)
                        {
                            msResult.IsSuccess = false;
                            msResult.Error = ex.Message;
                        }
                    }
                    else
                    {
                        msResult.IsSuccess = false;
                        msResult.Error = func.Invoke("99997");
                    }
                }
                return msResult;
            }));
        }
Пример #4
0
        public Task <MsResult <T> > SendMessageAsync <T>(byte module, byte method, object content, string exchangeId = "", int timeout = 10)
        {
            Console.WriteLine($"SendMessageAsync:{module}:{method}\t:{JsonConvert.SerializeObject(content)}");
            return(Task.Run(() =>
            {
#if DEBUG
                //var stopwatch = new System.Diagnostics.Stopwatch();
                //stopwatch.Restart();
#endif
                MsResult <T> msResult = new MsResult <T>();
                UInt32 identifyVar = 0;
                lock (objIdentify)
                {
                    identify++;
                    identifyVar = identify;
                }

                byte[] buffer = PacketHelper.Packet(module, method, RequestType.RequestReply, identifyVar, content, exchangeId);

                lock (objSend)
                {
                    try
                    {
                        if (client != null && client.Client != null && client.Client.Connected)
                        {
                            if (client != null && client.Client != null && client.Client.Connected)
                            {
                                client.GetStream().Write(buffer, 0, buffer.Length);
                            }
                            else
                            {
                                msResult.IsSuccess = false;
                                msResult.Error = func.Invoke("99997");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        msResult.IsSuccess = false;
                        msResult.Error = ex.Message;
                    }
                }
                var startTime = DateTime.Now;

                while (true)
                {
                    if (module == 26 && method == 3)
                    {
                    }
                    lock (objMessage)
                    {
                        if (messages.ContainsKey(identifyVar))
                        {
                            var packets = messages[identifyVar];
                            foreach (var packet in packets)
                            {
                                if (module == 26 && method == 3)
                                {
                                }

                                if (packet.IsSuccess)
                                {
                                    try
                                    {
                                        msResult.Content = ConvertResult <T>(packet.Content);
                                        msResult.IsSuccess = true;

                                        Console.WriteLine($"SendMessageAsync:{module}:{method}\t:Success【{(DateTime.Now - startTime).TotalMilliseconds}】");
                                    }
                                    catch (Exception ex)
                                    {
                                        //Console.WriteLine($"SendMessageAsync exception: {ex.Message}");
                                        msResult.IsSuccess = false;
                                        msResult.Error = ex.Message;
                                        Console.WriteLine($"SendMessageAsync:{module}:{method}\t:{msResult.Error}");
                                    }
                                }
                                else
                                {
                                    msResult.IsSuccess = false;
                                    msResult.Error = func.Invoke(Encoding.UTF8.GetString(packet.Content));
                                    Console.WriteLine($"SendMessageAsync:{module}:{method}\t:{msResult.Error}");
                                }
                            }
                            messages.Remove(identifyVar);
                            break;
                        }
                        else if ((DateTime.Now - startTime).TotalSeconds > timeout)
                        {
                            msResult.Error = func.Invoke("99998");
                            msResult.IsSuccess = false;

                            Console.WriteLine($"SendMessageAsync:{module}:{method}\t:{msResult.Error}");
                            break;
                        }
                    }
                    Thread.Sleep(10);
                }
#if DEBUG
                //Console.WriteLine($"SendMessageAsync module: {module} method: {method} expend:  {stopwatch.ElapsedMilliseconds}");
#endif
                return msResult;
            }));
        }
Пример #5
0
        public Task <MsResult <T> > SendMessageAsyncMultiple <T>(byte module, byte method, object content, Action <MsResult <T> > action, string exchangeId = "", int timeout = 10)
        {
            return(Task.Run(() =>
            {
                MsResult <T> msResult = new MsResult <T>();
                UInt32 identifyVar = 0;
                lock (objIdentify)
                {
                    identify++;
                    identifyVar = identify;
                }

                byte[] buffer = PacketHelper.Packet(module, method, RequestType.RequestReply, identifyVar, content, exchangeId);
                lock (objSend)
                {
                    try
                    {
                        if (client != null && client.Client != null && client.Client.Connected)
                        {
                            if (client != null && client.Client != null && client.Client.Connected)
                            {
                                client.GetStream().Write(buffer, 0, buffer.Length);
                            }
                            else
                            {
                                msResult.IsSuccess = false;
                                msResult.Error = func.Invoke("99997");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        msResult.IsSuccess = false;
                        msResult.Error = ex.Message;
                    }
                }
                var startTime = DateTime.Now;
                var loopTimes = 0;
                while (!msResult.IsFinished)
                {
                    loopTimes++;
                    //Console.WriteLine($"【{startTime.ToString("HH:mm:ss.ffff")}^^^{DateTime.Now.ToString("HH:mm:ss.ffff")}】:Not Finished");
                    lock (objMessage)
                    {
                        if (messages.ContainsKey(identifyVar))
                        {
                            var packets = messages[identifyVar];
                            foreach (var packet in packets)
                            {
                                if (packet.IsSuccess)
                                {
                                    try
                                    {
                                        msResult.Content = ConvertResult <T>(packet.Content);
                                        msResult.IsSuccess = true;
                                    }
                                    catch (Exception ex)
                                    {
                                        Console.WriteLine($"SendMessageAsync exception: {ex.Message}");
                                        msResult.IsSuccess = false;
                                        msResult.Error = ex.Message;
                                    }
                                }
                                else
                                {
                                    msResult.IsSuccess = false;
                                    msResult.Error = func.Invoke(Encoding.UTF8.GetString(packet.Content));
                                }
                                action?.Invoke(msResult);
                            }
                            messages.Remove(identifyVar);
                        }
                        else if ((DateTime.Now - startTime).TotalSeconds > timeout)
                        {
                            msResult.Error = func.Invoke("99998");
                            msResult.IsSuccess = false;
                            break;
                        }
                    }
                    Thread.Sleep(10);
                }

                Console.WriteLine($"【{startTime.ToString("HH:mm:ss.ffff")}^^^{DateTime.Now.ToString("HH:mm:ss.ffff")}】:Finished");
                return msResult;
            }));
        }