Пример #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
        public async Task <MsResult <bool> > Start()
        {
            MsResult <bool> msResult = new MsResult <bool>();

            try
            {
                client = new TcpClient();
                await client.ConnectAsync(this.host, this.port);

                //isStart = true;
                isStop = false;

                taskQueue   = RequestPacketQueue();
                taskReceive = ReceiveMessage();

                msResult.IsSuccess = true;
                IsConnected        = true;

                if (timerKeepAlive == null)
                {
                    timerKeepAlive = new Timer(KeepAlive, null, 5000, 5000);
                }

                NotifyConnectStateChanged(new ConnectStateArgs {
                    ConnectState = ConnectState.Success
                }, "");
            }
            catch (Exception ex)
            {
                msResult.IsSuccess = false;
                msResult.Error     = ex.Message;
                NotifyConnectStateChanged(new ConnectStateArgs {
                    ConnectState = ConnectState.Faild
                }, ex.ToString());
            }

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