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); } }
public static bool modifyOrder(int orderid, string symbol, string expiry, string callput, string exch, string strike, double price, int qty, char action, int OrderType, int TIF, TradingBoxV3 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 + ":" + OrderType + ":" + TIF; string input = "AMD:" + symbol + ":" + orderid + ":" + price + ":" + qty + ":" + strike + ":" + action + ":" + expiry + ":" + callput + ":" + exch + ":" + MachineGuid + ":" + UserGuid + ":" + OrderType + ":" + TIF; tradingBox.displayTextArea(input); OrderStruct os = new OrderStruct(11, 8); stringToStruct(input, ref os); os.display(); TcpClient clientSocket = new TcpClient(); tradingBox.displayTextArea("Connecting....."); //clientSocket.Connect("127.0.0.1", 5552); clientSocket.Connect("158.69.193.253", 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.ShowStatus1(reqType); tradingBox.displayTextArea("Received : " + reqType); clientSocket.Close(); try { String[] arr = reqType.Split(':'); //if (arr.Length == 4) { string strOrdId = arr[3]; if (strOrdId.Contains('\0')) { tradingBox.orderid = strOrdId.Substring(0, strOrdId.IndexOf('\0')); } else { tradingBox.orderid = strOrdId; tradingBox.ShowStatus1("OrderId : " + tradingBox.orderid); } } } catch (Exception e) { tradingBox.ShowStatus1("Exception in OrderId Generation " + e.Message); } return(true); } catch (Exception e) { tradingBox.ShowStatus1("Erro..... Order Details"); tradingBox.displayTextArea("Error..... " + e.StackTrace); } return(false); }