Esempio n. 1
0
        public void Write(String message)
        {
            try
            {
#if DUMP_COMMANDS_TO_YCOMP
                dumpWriter.Write(message);
                dumpWriter.Flush();
#endif
                byte[] data = Encoding.ASCII.GetBytes(message);
                stream.Write(data, 0, data.Length);
            }
            catch (Exception)
            {
                stream = null;
                if (closing)
                {
                    return;
                }
#if DUMP_COMMANDS_TO_YCOMP
                dumpWriter.Write("connection lost!\n");
                dumpWriter.Flush();
#endif
                ConnectionLostHandler handler = OnConnectionLost;
                if (handler != null)
                {
                    handler();
                }
            }
        }
Esempio n. 2
0
        public void OpenConnection()
        {
            isShutdown = false;

            MessageHandler?.Invoke("Opening API connection.");

            //Start the listener thread to listen for Proto messages from the server
            _tcpClient = new TcpClient(Config.ApiHost, Config.ApiPort);;
            _apiSocket = new SslStream(_tcpClient.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
            _apiSocket.AuthenticateAsClient(Config.ApiHost);

            StartListenThread();

            //use another thread to transmit a queue of Proto messages because there is a limit of 30 messages per second
            Thread transmitThread = new Thread(() =>
            {
                Thread.CurrentThread.IsBackground = true;
                try
                {
                    //A continuos loop to transmit messages from a queue
                    Transmit();
                }
                catch (Exception e)
                {
                    ErrorHandler?.Invoke("Transmitter throws exception: " + e);
                    isShutdown = true;
                    //try to reconnect
                    ConnectionLostHandler?.Invoke();
                }
            });


            //Connection Flow:
            //Authorise the App
            //For each user account if accountID not loaded request from API
            //For each user account authorise the account
            //For each user account if symbols not loaded request from API
            //For each user account subscribe to all symbols
            //Start the heartbeat time

            //Authorise the application - when message recieved go to beginConnection
            var msgFactory = new OpenApiMessagesFactory();
            var msg        = msgFactory.CreateAppAuthorizationRequest(Config.ClientId, Config.ClientSecret);

            _trasmitQueue.Enqueue(msg);

            transmitThread.Start();
        }
Esempio n. 3
0
 /// <summary>
 /// Reads up to 256 bytes from the stream
 /// </summary>
 /// <returns>The read bytes converted to a String using ASCII encoding</returns>
 public String Read()
 {
     try
     {
         int bytesRead = stream.Read(readBuffer, 0, 256);
         return(Encoding.ASCII.GetString(readBuffer, 0, bytesRead));
     }
     catch (Exception)
     {
         stream = null;
         ConnectionLostHandler handler = OnConnectionLost;
         if (handler != null)
         {
             handler();
         }
         return(null);
     }
 }
Esempio n. 4
0
        /// <summary>
        /// Creates a new instance with the specified options.
        /// </summary>
        /// <param name="successfulConnectHandler">The handler of the successful connection.</param>
        /// <param name="failedConnectHandler">The handler of the failed connection.</param>
        /// <param name="receiveHandler">The handler of received data.</param>
        /// <param name="connectionLostHandler">The handler of the disconnect.</param>
        public TcpClientSocket(SuccessfulConnectHandler successfulConnectHandler, FailedConnectHandler failedConnectHandler,
                               ReceiveHandler receiveHandler, ConnectionLostHandler connectionLostHandler)
        {
            _successfulConnectHandler = successfulConnectHandler;
            _failedConnectHandler     = failedConnectHandler;
            _receiveHandler           = receiveHandler;
            _connectionLostHandler    = connectionLostHandler;

            _socket = new Socket(SocketType.Stream, ProtocolType.Tcp)
            {
                ReceiveBufferSize = DoubleProtocol.TcpSocketBufferSize,
                SendBufferSize    = DoubleProtocol.TcpSocketBufferSize,
                ReceiveTimeout    = DoubleProtocol.SocketOperationTimeout,
                SendTimeout       = DoubleProtocol.SocketOperationTimeout,
                NoDelay           = true
            };
            _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
        }
Esempio n. 5
0
 public void Write(String message)
 {
     try
     {
         byte[] data = Encoding.ASCII.GetBytes(message);
         stream.Write(data, 0, data.Length);
     }
     catch (Exception)
     {
         stream = null;
         if (closing)
         {
             return;
         }
         ConnectionLostHandler handler = OnConnectionLost;
         if (handler != null)
         {
             handler();
         }
     }
 }
Esempio n. 6
0
        /// <summary>
        /// Creates a new instance with the specified options and instantly starts it.
        /// </summary>
        /// <param name="newConnectionHandler">The handler of new connections.</param>
        /// <param name="receiveHandler">The handler of received data.</param>
        /// <param name="connectionLostHandler">The handler of disconnects.</param>
        /// <param name="connectedSockets">A collection which always contains all connected sockets.</param>
        /// <param name="maxPendingConnections">The maximum count of pending connections (backlog).</param>
        /// <param name="port">The port the server should listen on.</param>
        public TcpServerSocket(NewConnectionHandler newConnectionHandler, ReceiveHandler receiveHandler,
                               ConnectionLostHandler connectionLostHandler, ICollection <Socket> connectedSockets,
                               int maxPendingConnections, int port)
        {
            _newConnectionHandler  = newConnectionHandler;
            _receiveHandler        = receiveHandler;
            _connectionLostHandler = connectionLostHandler;
            _connectedSockets      = connectedSockets;

            _socket = new Socket(SocketType.Stream, ProtocolType.Tcp)
            {
                ReceiveBufferSize = DoubleProtocol.TcpSocketBufferSize,
                SendBufferSize    = DoubleProtocol.TcpSocketBufferSize,
                ReceiveTimeout    = DoubleProtocol.SocketOperationTimeout,
                SendTimeout       = DoubleProtocol.SocketOperationTimeout,
                NoDelay           = true
            };
            _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);

            _socket.Bind(new IPEndPoint(IPAddress.IPv6Any, port));
            _socket.Listen(maxPendingConnections);
            StartAccepting();
        }