private void doProcessing() { string reqType = null; Byte[] sendBytes = null; string serverResponse = null; try { NetworkStream networkStream = clientSocket.GetStream(); Console.WriteLine("Lengeth : " + clientSocket.ReceiveBufferSize); byte[] bytesFrom = new byte[(Int32)clientSocket.ReceiveBufferSize + 1]; networkStream.Read(bytesFrom, 0, (Int32)clientSocket.ReceiveBufferSize); //reqType = System.Text.Encoding.ASCII.GetString(bytesFrom, 0, 10); Console.WriteLine(" >> " + "From client-" + clNo); OrderStruct os = getStructFromBytes(bytesFrom); RequestType rt = getReqType(os.methodID); switch (rt) { case RequestType.INS: //DO inert activity //OrderStruct osIns = getStructFromBytes(bytesFrom); Orders ord = new Orders(); int OrderNo = -1; if ((OrderNo = ord.addOrder(os)) < 0) { serverResponse = "FAILURE:INS:ORDID:" + Convert.ToString(OrderNo); } else { serverResponse = "SUCCESS:INS:ORDID:" + Convert.ToString(OrderNo); } break; case RequestType.AMD: //Do amendment activity, cancel the previous and boo new. //OrderStruct osAmd = getStructFromBytes(bytesFrom); Orders AmdOrd = new Orders(); int newOrderNo = -1; int oldOrderNo = os.OrderNo; if ((newOrderNo = AmdOrd.amendOrder(ref os)) < 0) { serverResponse = "FAILURE:AMD:OLD-ORDID:" + oldOrderNo; } else { serverResponse = "SUCCESS:AMD:OLD-ORDID:" + oldOrderNo + ":NEW-ORDID:" + newOrderNo; } break; case RequestType.CAN: //Do cancel activity Orders ordCan = new Orders(); int OrderNoCan = -1; if ((OrderNoCan = ordCan.cancelOrder(os)) < 0) { serverResponse = "FAILURE:CAN:ORDID:" + Convert.ToString(os.OrderNo); } else { serverResponse = "SUCCESS:CAN:ORDID:" + Convert.ToString(OrderNoCan); } break; case RequestType.FIL: //Do cancel activity Orders ordFilDummy = new Orders(); OrderFillStruct ofs = new OrderFillStruct(); Random r = new Random(); ofs.FillID = r.Next(); ofs.OrderNo = os.OrderNo; ofs.Quantity = os.quantity; ofs.FilledQuantity = os.quantity; ofs.Price = os.price; if (ordFilDummy.addFills(ofs) < 0) { serverResponse = "FAILURE:FIL:ORDID:" + Convert.ToString(os.OrderNo); } else { serverResponse = "SUCCESS:FIL:ORDID:" + Convert.ToString(os.OrderNo); } break; case RequestType.EOD: Orders eodObj = new Orders(); int totalStatus = -1; if ((totalStatus = eodObj.takeEODPositions()) < 0) { serverResponse = "FAILURE:EOD:" + totalStatus; } else { serverResponse = "FAILURE:EOD:" + totalStatus; } break; default: //Retrn generic error reqType = System.Text.Encoding.ASCII.GetString(bytesFrom); serverResponse = "ERROR:UNK:ORDID:[" + reqType + "]"; break; } sendBytes = Encoding.ASCII.GetBytes(serverResponse); networkStream.Write(sendBytes, 0, sendBytes.Length); networkStream.Flush(); this.clientSocket.Close(); Console.WriteLine(" >> Closing connection : " + serverResponse); } catch (Exception ex) { Console.WriteLine("Exception : >> " + ex.ToString()); } }
public void FromApp(QuickFix.Message msg, SessionID sessionID) { logMe("\r\n \r\n" + "FromApp :" + msg.ToString()); try { if (msg is QuickFix.FIX42.OrderCancelReject) { Console.WriteLine("Order Cancel Reject event raised"); QuickFix.FIX42.OrderCancelReject er = (QuickFix.FIX42.OrderCancelReject)msg; if (er.IsSetClOrdID()) { Console.WriteLine("ClOrdID : " + er.ClOrdID); } if (er.IsSetOrigClOrdID()) { Console.WriteLine("OrigClOrdID : " + er.OrigClOrdID); } if (er.IsSetOrdStatus()) { Console.WriteLine("OrderStatus : " + er.OrdStatus); } if (er.IsSetCxlRejResponseTo()) { Console.WriteLine("CxlRejResponseTo : " + er.CxlRejResponseTo); } if (er.IsSetCxlRejReason()) { Console.WriteLine("CxlRejReason : " + er.CxlRejReason); } if (er.IsSetOrderID()) { Console.WriteLine("OrderID : " + er.OrderID); } if (er.CxlRejResponseTo.getValue() == CxlRejResponseTo.ORDER_CANCEL_REQUEST) { // Order Cancel Rejected reason // 1= UNKNOWN_ORDER, 2 = Broker Option, 3 = Order already in Pending Cancel or Pending Replace status string orderDetails = "CANCEL_REJECTED"; Int32 origOrderID = -1; if (er.IsSetOrigClOrdID()) { origOrderID = Convert.ToInt32(er.OrigClOrdID.getValue()); } Orders o = new Orders(); if (er.IsSetEncodedText()) { orderDetails += " : " + er.EncodedText.getValue(); } o.FIXResponseCanModRejectHandler(Convert.ToInt32(er.ClOrdID.getValue()), er.OrderID.getValue(), origOrderID, er.CxlRejResponseTo.getValue(), "CANCEL_" + getRejectType(er.CxlRejReason), orderDetails); } else if (er.CxlRejResponseTo.getValue() == CxlRejResponseTo.ORDER_CANCEL_REPLACE_REQUEST) { // Order modification Rejected reason // 1 = Broker Option // 3 = Order already in Pending Cancel or Pending Replace status string orderDetails = "MODIFY_REJECTED"; Int32 origOrderID = -1; if (er.IsSetOrigClOrdID()) { origOrderID = Convert.ToInt32(er.OrigClOrdID.getValue()); } Orders o = new Orders(); if (er.IsSetEncodedText()) { orderDetails += " : " + er.EncodedText.getValue(); } o.FIXResponseCanModRejectHandler(Convert.ToInt32(er.ClOrdID.getValue()), er.OrderID.getValue(), origOrderID, er.CxlRejResponseTo.getValue(), "MODIFY_" + getRejectType(er.CxlRejReason), orderDetails); } } else if (msg is QuickFix.FIX42.ExecutionReport) { string ordTypeField = msg.GetField(40); if (ordTypeField == "1") { IField field = new DecimalField(44, 0); msg.SetField(field); } QuickFix.FIX42.ExecutionReport er = (QuickFix.FIX42.ExecutionReport)msg; string orderDetails = Convert.ToString(er.ExecType.getValue()) + "|" + er.OrderQty.getValue() + "|" + er.Price.getValue() + "|" + er.LastShares.getValue() + "|" + er.LastPx.getValue() + "|" + er.CumQty.getValue() + "|" + er.AvgPx.getValue() + "|" + er.OrdType.getValue() + "|"; if (er.IsSetText()) { orderDetails = orderDetails + er.Text + "|"; } else { orderDetails = orderDetails + "NULL|"; } logMe("\r\n \r\n" + "Got execution Report - ExecType = " + orderDetails); Console.WriteLine("\r\n \r\n" + "Got execution Report - ExecType = " + orderDetails); Orders o = new Orders(); if (er.ExecType.getValue() == ExecType.FILL || er.ExecType.getValue() == ExecType.PARTIAL_FILL) { o.FIXResponseHandlerForFill(Convert.ToInt32(er.ClOrdID.getValue()), er.OrderID.getValue(), (float)Convert.ToDouble(er.OrderQty.getValue()), (float)Convert.ToDouble(er.LastPx.getValue()), (float)Convert.ToDouble(er.LastShares.getValue()), getExecType(er.ExecType), er.ExecType.getValue()); } else { Int32 origOrderID = -1; float price = -1; char orderType = '2'; if (er.IsSetOrigClOrdID()) { origOrderID = Convert.ToInt32(er.OrigClOrdID.getValue()); } if (er.IsSetPrice()) { price = (float)Convert.ToDouble(er.Price.getValue()); } if (er.IsSetOrdType()) { orderType = er.OrdType.getValue(); } //orderid is string since value is too large for int variable o.FIXResponseHandler(Convert.ToInt32(er.ClOrdID.getValue()), er.OrderID.getValue(), origOrderID, er.ExecType.getValue(), getExecType(er.ExecType), orderDetails, price, orderType); } } } catch (Exception e) { Console.WriteLine("exception(FixClient FromAPP) " + e.Message); } }