Exemplo n.º 1
0
        public static bool insertOrder(string symbol, string expiry, string callput, string exch, string strike, double price, int qty, char action, TradingBoxV2 tb)
        {
            tradingBox = tb;
            try
            {
                //Console.WriteLine("Format : INS:SYM:Price:Quantity:direction:machineID:userID\n");
                //Console.WriteLine("usage Number of Test Cases followed by input per line <INS:AAPL:111.90:1:B> or <CAN:AAPL:0:111.90:1:B>\nDesc : INS:SYMBOL:PRICE:QTY:DIR");

                /*byte[] bytesFrom = new byte[(int)clientSocket.ReceiveBufferSize + 1];
                 * networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                 * reqType = System.Text.Encoding.ASCII.GetString(bytesFrom);
                 * Console.WriteLine("Received : " + reqType);
                 * clientSocket.Close();
                 */

                string reqType   = null;
                Byte[] sendBytes = null;

                //stringToStruct("INS:AAPL0000:111.90:1:B:21:25", ref os);
                string input = "INS:" + symbol + ":" + price + ":" + qty + ":" + strike + ":" + action + ":" + expiry + ":" + callput + ":" + exch + ":" + MachineGuid + ":" + UserGuid;
                tradingBox.displayTextArea(input);

                OrderStruct os = new OrderStruct(8, 8);
                stringToStruct(input, ref os);
                os.display();

                TcpClient clientSocket = new TcpClient();
                tradingBox.displayTextArea("Connecting.....");

                clientSocket.Connect("127.0.0.1", 5552);
                // use the ipaddress as in the server program

                tradingBox.displayTextArea("Connected ...");

                NetworkStream networkStream = clientSocket.GetStream();

                sendBytes = (getBytes(os));

                networkStream.Write(sendBytes, 0, sendBytes.Length);
                tradingBox.displayTextArea("written " + sendBytes.Length + " bytes.");


                System.Threading.Thread.Sleep(200);
                networkStream.Flush();

                byte[] bytesFrom = new byte[(Int32)clientSocket.ReceiveBufferSize + 1];
                networkStream.Read(bytesFrom, 0, (Int32)clientSocket.ReceiveBufferSize);
                reqType = System.Text.Encoding.ASCII.GetString(bytesFrom);
                tradingBox.displayTextArea("Received : " + reqType);
                clientSocket.Close();

                return(true);
            }
            catch (Exception e)
            {
                tradingBox.displayTextArea("Error..... " + e.StackTrace);
            }
            return(false);
        }
Exemplo n.º 2
0
        static byte[] getBytes(OrderStruct str)
        {
            try
            {
                int size = Marshal.SizeOf(typeof(OrderStruct));
                tradingBox.displayTextArea("Size of str : " + size);

                byte[] arr = new byte[size];

                IntPtr ptr = Marshal.AllocHGlobal(size);
                Marshal.StructureToPtr(str, ptr, true);
                Marshal.Copy(ptr, arr, 0, size);
                Marshal.FreeHGlobal(ptr);
                return(arr);
            }
            catch (Exception ex)
            {
                tradingBox.displayTextArea("Exception : " + ex.Message);
                return(null);
            }
        }