Beispiel #1
0
 internal static ClientBase Create(ICommandCollectService service, string url, iExchange.Common.AppType appType)
 {
     if (appType == iExchange.Common.AppType.TransactionServer)
     {
         return(new TransactionClient(service, url, appType));
     }
     return(new Client(service, url, appType));
 }
Beispiel #2
0
        private static ICommandCollectService CreateCommandCollectService(string url, int timeoutInSecond = 30)
        {
            EndpointAddress address = new EndpointAddress(url);
            NetTcpBinding   binding = new NetTcpBinding(SecurityMode.None);

            binding.MaxBufferPoolSize = binding.MaxReceivedMessageSize = binding.MaxBufferSize = 16 * 1024 * 1024;
            binding.SendTimeout       = TimeSpan.FromSeconds(timeoutInSecond);
            binding.OpenTimeout       = TimeSpan.FromSeconds(timeoutInSecond);
            ChannelFactory <ICommandCollectService> factory = new ChannelFactory <ICommandCollectService>(binding, address);
            ICommandCollectService commandCollectService    = factory.CreateChannel();

            return(commandCollectService);
        }
Beispiel #3
0
        public bool KickoutPredecessor(Guid userId)
        {
            List <string> trderServerSLUrls = null;

            lock (this.lockObj)
            {
                trderServerSLUrls = new List <string>(this.silverLightTradingConsoles.Keys);
            }

            try
            {
                Parallel.ForEach(trderServerSLUrls, slTradingConsoleUrl =>
                {
                    try
                    {
                        ICommandCollectService commandCollecter = null;

                        lock (this.lockObj)
                        {
                            this.silverLightTradingConsoles.TryGetValue(slTradingConsoleUrl, out commandCollecter);
                        }

                        if (commandCollecter != null)
                        {
                            commandCollecter.KickoutPredecessor(userId);
                        }
                    }
                    catch (Exception exception)
                    {
                        lock (this.lockObj)
                        {
                            LinkedList <CommandAndToken> queue = null;
                            if (this.commandQueue.TryGetValue(slTradingConsoleUrl, out queue))
                            {
                                queue.Clear();
                            }
                            this.silverLightTradingConsoles.Remove(slTradingConsoleUrl);
                            this.HandleSuspectUrl(slTradingConsoleUrl);
                        }
                        AppDebug.LogEvent("StateServer", exception.ToString(), EventLogEntryType.Warning);
                    }
                });
            }
            catch (Exception exception)
            {
                AppDebug.LogEvent("StateServer", exception.ToString(), EventLogEntryType.Warning);
                return(false);
            }
            return(true);
        }
Beispiel #4
0
        private bool TrySuspectUrl(string url)
        {
            Guid idToTry = new Guid("11111111-2222-3333-4444-555555555555");

            try
            {
                ICommandCollectService commandCollectService = CreateCommandCollectService(url, 10);
                commandCollectService.KickoutPredecessor(idToTry);
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Beispiel #5
0
 internal TraderServerAgent(ICommandCollectService service, string serviceUrl)
 {
     _service     = service;
     _serviceUrl  = serviceUrl;
     _actionBlock = new ActionBlock <Command>(command =>
     {
         try
         {
             service.AddCommand(command);
         }
         catch (CommunicationException ex)
         {
             Console.WriteLine(ex);
             this.OnCommunicationError();
         }
     });
 }
Beispiel #6
0
        internal string CreateChannelForTraderSL(string urls, bool removeExist = false)
        {
            urls = urls.ToLower();

            lock (this.lockObj)
            {
                string[] urlArray = urls.Split(';');
                foreach (string url in urlArray)
                {
                    try
                    {
                        if (this.silverLightTradingConsoles.ContainsKey(url))
                        {
                            if (removeExist)
                            {
                                this.silverLightTradingConsoles.Remove(url);
                            }
                            else
                            {
                                continue;
                            }
                        }

                        ICommandCollectService commandCollectService = CreateCommandCollectService(url);
                        this.silverLightTradingConsoles.Add(url, commandCollectService);
                    }
                    catch (Exception exception)
                    {
                        AppDebug.LogEvent("StaterServer", "Failed to CreateChannelForTraderSL with " + url
                                          + Environment.NewLine + exception.ToString(), EventLogEntryType.Warning);
                        continue;
                    }
                }
            }
            return(urls);
        }
Beispiel #7
0
        private void SendCommand(object state)
        {
            ICommandCollectService       commandCollecter = null;
            AutoResetEvent               commandEvent     = null;
            LinkedList <CommandAndToken> queue            = null;

            string url = (string)state;

            lock (this.lockObj)
            {
                if (!this.silverLightTradingConsoles.ContainsKey(url))
                {
                    return;
                }

                commandCollecter = this.silverLightTradingConsoles[url];
                commandEvent     = this.commandEvents[url];
                queue            = this.commandQueue[url];
            }

            while (true)
            {
                commandEvent.WaitOne();

                while (true)
                {
                    CommandAndToken?commandAndToken = null;
                    lock (this.lockObj)
                    {
                        if (queue.Count > 0)
                        {
                            commandAndToken = queue.First.Value;
                        }
                    }

                    if (commandAndToken == null)
                    {
                        break;
                    }
                    else
                    {
                        try
                        {
                            commandCollecter.AddCommand(commandAndToken.Value.Token, commandAndToken.Value.Command);
                            lock (this.lockObj)
                            {
                                queue.RemoveFirst();
                            }
                        }
                        catch (Exception e)
                        {
                            AppDebug.LogEvent("StateServer", string.Format("Exception = {0}\n\n Token = {1},TargetApp = {2}\n Command = {3} url={4}", e, commandAndToken.Value.Token, AppType.TradingConsoleSilverLight, commandAndToken.Value.Command, url), EventLogEntryType.Warning);
                            if (e is EndpointNotFoundException || e is TimeoutException)
                            {
                                lock (this.lockObj)
                                {
                                    this.commandQueue[url].Clear();
                                    this.silverLightTradingConsoles.Remove(url);
                                    this.HandleSuspectUrl(url);
                                    return;
                                }
                            }
                            else //if (e is FaultException || e is CommunicationObjectFaultedException || e is ProtocolException)
                            {
                                AppDebug.LogEvent("StateServer", "Try to recover channel to trader sl: " + url, EventLogEntryType.Warning);
                                this.CreateChannelForTraderSL(url, true);
                                lock (this.lockObj)
                                {
                                    if (!this.silverLightTradingConsoles.TryGetValue(url, out commandCollecter))
                                    {
                                        commandCollecter = null;
                                    }
                                }

                                if (commandCollecter == null)
                                {
                                    Thread.Sleep(1000);
                                }
                            }
                        }
                    }
                }
            }
        }
Beispiel #8
0
 internal TransactionClient(ICommandCollectService service, string serviceUrl, iExchange.Common.AppType appType)
     : base(service, serviceUrl, appType)
 {
     _commandManager = new CommandManager(this);
 }
Beispiel #9
0
 internal Client(ICommandCollectService service, string serviceUrl, iExchange.Common.AppType appType)
     : base(service, serviceUrl, appType)
 {
     _actionBlock = new ActionBlock <Command>((Action <Command>) this.SendCommandHandle);
 }
Beispiel #10
0
 protected void Reconnect()
 {
     _service = ClientManager.CreateChannel(_serviceUrl);
     _service.Test();
 }
Beispiel #11
0
 protected ClientBase(ICommandCollectService service, string serviceUrl, iExchange.Common.AppType appType)
 {
     _service    = service;
     _serviceUrl = serviceUrl;
     _appType    = appType;
 }