예제 #1
0
        private void menuItemRun_Click(object sender, System.EventArgs e)
        {
            ThreadArgument threadArgument = new ThreadArgument(new ThreadHandler(Language.ExecutionCommand),
                                                               RuleCompiler.compilerResults);
            Thread thread = new Thread(new ThreadStart(threadArgument.StartThread));

            thread.IsBackground = true;
            thread.Start();
        }
예제 #2
0
        /// <summary>
        /// Starts a simulated price feed.
        /// </summary>
        /// <param name="transaction">Used to commit or reject one or more operations as a unit.</param>
        /// <param name="remoteMethod">A metadata description of the method to be executed.</param>
        public static void Start(Transaction transaction, RemoteMethod remoteMethod)
        {
            // Extract the parameters from the batch.
            string name = remoteMethod.Parameters.GetRequiredString("name");

            // The 'name' used to start this thread is really a broker symbol.  Use the tables to find a broker symbol that matches
            // the name.  This will become the broker identifier that this simulation thread responds to.
            object brokerId = null;

            try
            {
                // Lock the tables
                Debug.Assert(!ServerMarketData.AreLocksHeld);
                ServerMarketData.BrokerLock.AcquireReaderLock(Timeout.Infinite);

                // Search, the hard way, for a symbol that matches the simulator name.
                foreach (ServerMarketData.BrokerRow brokerRow in ServerMarketData.Broker)
                {
                    if (brokerRow.Symbol == name)
                    {
                        brokerId = brokerRow.BrokerId;
                    }
                }
            }
            finally
            {
                // Release the broker table.
                if (ServerMarketData.BrokerLock.IsReaderLockHeld)
                {
                    ServerMarketData.BrokerLock.ReleaseReaderLock();
                }
            }

            // If the broker identifier was found, then start the simulation threads to service all electronic placements that are
            // destined for that broker.
            if (brokerId != null)
            {
                // This list is used to filter out brokers that aren't recognized by this simulator.
                Broker.brokerList.Add(brokerId);

                // This thread will remove placements from the queue and create orders in the local order book.
                Thread placementThread = new Thread(new ThreadStart(PlacementHandler));
                placementThread.Start();

                // Start the thread to handle the random execution of orders in the local order book.
                ThreadArgument threadArgument  = new ThreadArgument(new ThreadHandler(ExecutionThread), "Execution Thread", brokerId);
                Thread         executionThread = new Thread(new ThreadStart(threadArgument.StartThread));
                executionThread.Start();
            }
        }
예제 #3
0
        private void TryToKeepConnected(ThreadArgument param)
        {
            ClientHandler handler = param.Handler;

            Thread.CurrentThread.Name = "connection_monitor_proc";

            // Start up.
            if (param.StartCallback != null)
            {
                param.StartCallback(handler);
            }

            try
            {
                while (!handler.ShouldStopConnectionMonitor)
                {
                    // If it is not connected, then try to connect.
                    if (!handler.Client.IsConnected)
                    {
                        handler.Client.Connect(Host, Port, true);
                    }

                    // Wait for a disconnection or a connection failure before checking again.
                    handler.Client.NeedsReconnectionEvent.WaitOne();

                    // If it didn't really connect, then wait and try again.
                    if (!handler.Client.IsConnected)
                    {
                        Thread.Sleep(10000);                         // Wait 10 seconds before trying to reconnect.
                    }
                }
            }
            catch (ThreadInterruptedException)
            {
                // Handle interruption.
                logger.Debug("Interrupted worker {0}", Thread.CurrentThread.Name);
            }
            finally
            {
                logger.Debug("Cleaning up thread {0}", Thread.CurrentThread.Name);

                // Clean up.
                if (param.FinishCallback != null)
                {
                    param.FinishCallback(handler);
                }
            }
        }
예제 #4
0
        private void ProcessClient(ThreadArgument param)
        {
            ClientContext context = param.ClientContext;

            Thread.CurrentThread.Name = string.Format("client_proc-{0}", context.ClientKey);

            // Start up.
            if (param.StartCallback != null)
            {
                param.StartCallback(context);
            }

            try
            {
                int pollTimeout = 200000;                 // 1/5 of a second in microseconds

                while (!context.ShouldStop)
                {
                    try
                    {
                        // Check whether the previous read indicated a client disconnection.
                        if (context.ClientSocket == null)
                        {
                            break;
                        }

                        bool canWrite = context.ClientSocket.Poll(pollTimeout, SelectMode.SelectWrite);
                        // true if:
                        // - A Connect method call has been processed, and the connection has succeeded.
                        // - Data can be sent.
                        if (canWrite)
                        {
                            // Dequeue data from write queue and send it.
                            if (!context.OutBuffer.IsEmpty)
                            {
                                byte[] buffer = null;
                                context.OutBuffer.TryDequeue(out buffer);
                                //logger.Debug("BeginSend: {0}", Encoding.UTF8.GetString(buffer));

                                try
                                {
                                    context.ClientSocket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None,
                                                                   new AsyncCallback(EndSend), context);
                                }
                                catch (SocketException ex)
                                {
                                    logger.Error("BeginSend failed: {0}", ex.Message);
                                    HandleSocketError(ex.SocketErrorCode, context);
                                }
                            }
                        }
                    }
                    catch (ObjectDisposedException)
                    {
                        // Socket was already closed.
                    }

                    Thread.Sleep(1);
                }
            }
            catch (ThreadInterruptedException)
            {
                // Handle interruption.
                logger.Debug("Interrupted worker for client {0}.", context.ClientKey);
            }
            finally
            {
                logger.Debug("Cleaning up worker for client {0}.", context.ClientKey);

                // Clean up.
                if (param.FinishCallback != null)
                {
                    param.FinishCallback(context);
                }
            }
        }
예제 #5
0
        private static void ProcessClientStatic(object param)
        {
            ThreadArgument obj = (ThreadArgument)param;

            obj.Server.ProcessClient(obj);
        }
예제 #6
0
        private static void ProcessSocketStatic(object param)
        {
            ThreadArgument obj = (ThreadArgument)param;

            obj.Client.ProcessSocket(obj);
        }
예제 #7
0
        private static void TryToKeepConnectedStatic(object param)
        {
            ThreadArgument obj = (ThreadArgument)param;

            obj.Handler.TryToKeepConnected(obj);
        }