Exemplo n.º 1
0
        void connect()
        {
            SSLConfiguration sSLConfiguration = null;

            while (true)
            {
                if (_microServiceHost.ServiceProvider == null || _microServiceHost.Id == null)
                {
                    Thread.Sleep(1000);
                    continue;
                }
                if (sSLConfiguration == null)
                {
                    sSLConfiguration = _microServiceHost.ServiceProvider.GetService <SSLConfiguration>();
                    _logger          = _microServiceHost.ServiceProvider.GetService <ILogger <MapFileManager> >();
                }
                try
                {
                    using (var client = new GatewayClient(_gatewayAddress, sSLConfiguration))
                    {
                        client.WriteServiceData(new GatewayCommand {
                            Type    = CommandType.ListenFileChange,
                            Content = _dict.Keys.ToJsonString()
                        });

                        client.ReadTimeout = 60000;
                        while (true)
                        {
                            var ret = client.ReadServiceObject <InvokeResult <string> >();
                            if (ret.Data != null)
                            {
                                string filepath = ret.Data;
                                _logger?.LogInformation("文件映射系统收到新的文件:{0}", filepath);
                                int len  = client.ReadInt();
                                var data = client.ReceiveDatas(len);
                                try
                                {
                                    var    item      = _dict[filepath];
                                    string localpath = item.LocalPath;
                                    File.WriteAllBytes(localpath, data);
                                    if (item.Callback != null)
                                    {
                                        item.Callback(filepath, localpath);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    _logger?.LogError(ex, ex.Message);
                                }
                            }

                            client.WriteServiceData(new InvokeResult());
                        }
                    }
                }
                catch (Exception ex)
                {
                }
                Thread.Sleep(1000);
            }
        }