/// <summary> /// Send Order Book to clients /// </summary> /// <param name="OrderBook">OrderBook for send.</param> public static void SendOrderBooktoClients(TOrderBook OrderBook, TcpClient client = null) { if (OrderBook != null && OrderBook.Orders.Count > 0) { try { var datasend = JsonConvert.SerializeObject(_OB); byte[] buffer = Encoding.ASCII.GetBytes("/ORDERBOOK;" + datasend); lock (_lock) { if (client != null) { Sendbuf2Client(client, buffer); } else { foreach (TcpClient c in _DClients.Values) { Sendbuf2Client(c, buffer); } } } } catch (Exception ex) { ErrorCatcher(ex, "SendOrderBooktoClients"); } } }
public static void SetOrderBook(TOrderBook OB) { _OB = OB; }
public static void OBInit() { _OB = new TOrderBook(); _OB.Init(); }
/// <summary> /// Read Command from Client /// </summary> /// <param name="client">TCPCLIENT</param> /// <param name="clientid">ClientID.</param> /// <param name="sCommand">received command text.</param> /// <param name="byte_count">received length of sCommand.</param> public static void readCommand(TcpClient client, string clientid, byte[] sCommand, int byte_count) { string exception4send = ""; string ReadData = Encoding.ASCII.GetString(sCommand, 0, byte_count); Console.WriteLine(client.Client.RemoteEndPoint + ":" + ReadData); switch (ReadData.Split(';').First()) { case "/UPDATELASTGUIDORDERS": string guid4update = ReadData.Split(';')[1].ToString(); _OB.UpdateLastGuid(guid4update, clientid); _TRH.UpdateLastGuid(guid4update, clientid); break; case "/GETORDERBOOK": SendOrderBooktoClients(_OB, client); SendTradeHistorytoClients(_TRH); break; case "/ADDNEWORDER": string OrderStr = ReadData.Split(';')[1].ToString(); if (OrderStr.Length > 0) { var O = TOrder.Deserialize(OrderStr); bool TradeDone = CheckOrderForTrade(O); if (!TradeDone) { _OB.AddNewOrder(O); } else { SendTradeHistorytoClients(_TRH); } SendOrderBooktoClients(_OB); } break; case "/REMOVEORDER": SendOrderBooktoClients(_OB); break; case "/CLIENTNUMBER": clientid = ReadData.Split(';')[1]?.ToString(); SetCurrentUserID(clientid); if (_LastUserID.Length > 0) { Sendbuf2Client(client, Encoding.ASCII.GetBytes("/UPDATELASTGUIDORDERS;" + _LastUserID)); } File.WriteAllText("_lastguid", clientid); Sendbuf2Client(client, Encoding.ASCII.GetBytes("/GETORDERBOOK;")); break; case "/ORDERBOOK": string jsdataOB = ReadData.Split(';')[1]?.ToString(); _OB = JsonConvert.DeserializeObject <TOrderBook>(jsdataOB); break; case "/TRADEHISTORYBOOK": string jsdataTH = ReadData.Split(';')[1]?.ToString(); _TRH = JsonConvert.DeserializeObject <TTradeHistory>(jsdataTH); break; } if (exception4send.Length > 0) { Sendbuf2Client(client, Encoding.ASCII.GetBytes("/ERROR;" + exception4send)); } }