Browse Source

jxing 下载重试, 断线重连

mengxiangge 4 years ago
parent
commit
42387f6756

+ 45 - 5
Dispatcher/Client/ClientApp.cs

@@ -9,15 +9,17 @@ namespace Client
 {
     public class ClientApp
     {
-        TaskNettyClient client;                    // 网络传输客户端
-        SimpleMessageHandler simpleHandler;        // 网络传输处理
+        static TaskNettyClient client;                    // 网络传输客户端
+        static SimpleMessageHandler simpleHandler;        // 网络传输处理
         TaskService taskService;                   // 持久化任务
         TaskDownloadManager taskDownloadManager;   // 下载任务文件(Http)
         MessageHandler messageHandler;             // 处理客户端收到的消息
         TaskHandler taskHandler;                   // 处理服务端发送的任务(监视下载状况等待)
         int maxTaskCount;                          // 能处理的最大任务数量, 影响是否拒绝服务器分发的任务
         string defaultFileDir;                     // 默认文件目录
-        IRevitCommandExcutor revitCommandExcutor;  
+        private Action<SimpleMessageHandler> heartbeat;
+        IRevitCommandExcutor revitCommandExcutor;
+        private Action<int> reconnectAction;
         public ClientApp(string ip, int port, string dir, int maxTaskCount, IRevitCommandExcutor revitCommandExcutor, int maxDownTaskCount=5) {
             client = new TaskNettyClient(ip, port);
             taskService = new TaskService();
@@ -46,13 +48,29 @@ namespace Client
             }
 
             // 起动心跳线程
-            Action<SimpleMessageHandler> heartbeat = SendHeartbeat;
+            heartbeat = SendHeartbeat;
             heartbeat.BeginInvoke(simpleHandler, null, null);
 
             client.RunClientAsync(simpleHandler).Wait();
+
+            = Reconnect;
+            reconnectAction.BeginInvoke(30, null, null);
         }
 
-        static void SendHeartbeat(SimpleMessageHandler simpleHandler)
+        void Reconnect(int interval)
+        {
+            while (true)
+            {
+                if (!TaskNettyClient.isConnected)
+                {
+                    Console.WriteLine(DateTime.Now + ": reconnecting....");
+                    Restart();
+                }
+                Thread.Sleep(interval * 1000);
+            }
+        }
+
+        void SendHeartbeat(SimpleMessageHandler simpleHandler)
         {
             while (true)
             {
@@ -83,6 +101,28 @@ namespace Client
             taskDownloadManager.resumeAllTasks();
             messageHandler.ResumeAllTasks();
         }
+
+        public void Restart()
+        {
+            simpleHandler = new SimpleMessageHandler();
+            if (messageHandler == null)
+            {
+                taskHandler = new TaskHandler(simpleHandler, taskService, taskDownloadManager, defaultFileDir, revitCommandExcutor);
+                taskHandler.SyncAllTask();
+                messageHandler = new MessageHandler(simpleHandler, maxTaskCount, taskHandler);
+                messageHandler.ResumeAllTasks();
+                
+                ThreadPool.QueueUserWorkItem(messageHandler.HandleMessage);
+                ThreadPool.QueueUserWorkItem(taskHandler.RunTasks);
+            }
+            else
+            {
+                messageHandler.setSimpleMessageHandler(simpleHandler);
+                taskHandler.setSimpleMessageHandler(simpleHandler);
+            }
+
+            client.RunClientAsync(simpleHandler).Wait();
+        }
     }
 
     

+ 1 - 0
Dispatcher/Client/Start/ServiceMBIClientHandler.cs

@@ -29,6 +29,7 @@ namespace Client.Start
             //192.168.20.225
             m_Client = new ClientApp(ip, port, dir, maxtask, new RevitCmdExecutor(), maxDownloadTask);
             m_Client.Start();
+            //Console.WriteLine("begin");
             //Console.ReadKey();
         }
 

+ 37 - 20
Dispatcher/HttpDownload/HttpDownloader.cs

@@ -5,6 +5,7 @@ using System.Text;
 using System.Threading.Tasks;
 using System.IO;
 using System.Net;
+using System.Threading;
 
 namespace HttpDownload
 {
@@ -41,14 +42,24 @@ namespace HttpDownload
             {
                 return;
             }
-            try
-            {
-                for (; !finished && running;)
-                    HttpDownloadFile();
-            }
-            catch (Exception ex)
+
+            bool hasError = true;
+            int retryTimes = 200;
+            while (hasError && retryTimes > 0)
             {
-                throw ex;
+                try
+                {
+                    hasError = false;
+                    for (; !finished && running;)
+                        HttpDownloadFile();
+                }
+                catch (Exception ex)
+                {
+                    hasError = true;
+                    --retryTimes;
+                    if(retryTimes <= 0)
+                        throw ex;
+                }
             }
         }
         const string errorInfo = "下载结束, 但是文件长度与下载字节数不一致!";
@@ -166,22 +177,28 @@ namespace HttpDownload
         public static long GetFileContentLength(string url)
         {
             HttpWebRequest request = null;
-            try
+            int retryTimes = 200;
+            while (retryTimes > 0)
             {
-                request = (HttpWebRequest)HttpWebRequest.Create(url);
-                request.Timeout = TimeOutWait;
-                request.ReadWriteTimeout = ReadWriteTimeOut;
-                //向服务器请求,获得服务器回应数据流
-                WebResponse respone = request.GetResponse();
-                request.Abort();
-                return respone.ContentLength;
-            }
-            catch (Exception e)
-            {
-                if (request != null)
+                try
+                {
+                    --retryTimes;
+                    request = (HttpWebRequest) HttpWebRequest.Create(url);
+                    request.Timeout = TimeOutWait;
+                    request.ReadWriteTimeout = ReadWriteTimeOut;
+                    //向服务器请求,获得服务器回应数据流
+                    WebResponse respone = request.GetResponse();
                     request.Abort();
-                return 0;
+                    return respone.ContentLength;
+                }
+                catch (Exception e)
+                {
+                    if (request != null)
+                        request.Abort();
+                }
+                Thread.Sleep(100);
             }
+            return -1;
         }
         private bool isDownloadFinished(long offset)
         {

+ 2 - 0
Dispatcher/NettyClient/SimpleMessageHandler.cs

@@ -18,6 +18,7 @@ namespace NettyClient
         public override void ChannelActive(IChannelHandlerContext context)
         {
             //MessageBox.Show("connected");
+            TaskNettyClient.isConnected = true;
             this.context = context;
         }
         public override void ChannelRead(IChannelHandlerContext context, object message)
@@ -43,6 +44,7 @@ namespace NettyClient
         {
             base.ChannelInactive(context);
             Console.WriteLine("1");
+            TaskNettyClient.isConnected = false;
         }
 
         public override void ChannelUnregistered(IChannelHandlerContext context)

+ 5 - 3
Dispatcher/NettyClient/TaskNettyClient.cs

@@ -18,6 +18,7 @@ namespace NettyClient
         IChannel clientChannel;
         MultithreadEventLoopGroup group;
         Bootstrap bootstrap;
+        public static bool isConnected = true;
         public TaskNettyClient(string ip, int port)
         {
             this.ip = ip;
@@ -51,7 +52,7 @@ namespace NettyClient
             }
             catch (Exception ex)
             {
-                MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace + $"ip:{ip};port{port}");
+                Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace + $"ip:{ip};port{port}");
             }
             finally
             {
@@ -73,15 +74,16 @@ namespace NettyClient
 
         public static void Main(string[] args)
         {
-            TaskNettyClient client = new TaskNettyClient("127.0.0.1", 6666);
+            TaskNettyClient client = new TaskNettyClient("47.94.89.44", 6666);
             SimpleMessageHandler simpleHandler = new SimpleMessageHandler();
             client.RunClientAsync(simpleHandler).Wait();
             while (true)
             {
                 Thread.Sleep(15000);
+                //Console.WriteLine("aaaaaa");
                 client.CloseAsync().Wait();
                 // 重启
-                //client.RunClientAsync(new SimpleHandler()).Wait();
+                //client.RunClientAsync(simpleHandler).Wait();
             }
         }
     }