private void HandleMarketmakerThread(object obj) { TcpClient client = obj as TcpClient; while (true) { //получаем команду, парсим и ставим в очередь string received = SocketIO.Read(client); Console.WriteLine(DateTime.Now + " MARKETMAKER: received " + received); if (received == "dc") { break; } string[] json_fcs = received.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < json_fcs.Length; i++) { //JSON-парсинг вызова функции ядра int func_id; string[] str_args; bool _parsed = JsonManager.ParseTechJson(json_fcs[i], out func_id, out str_args); if (_parsed) { Console.WriteLine(DateTime.Now + " MARKETMAKER: to queue function #" + func_id); MarketmakerRequest.QueueFC(client, func_id, str_args); //попытка парсинга аргументов и постановки в очередь соответствующей функции } else //ошибка JSON-парсинга { CoreResponse.RejectInvalidJson(client); } } } client.Close(); Console.WriteLine(DateTime.Now + " MARKETMAKER: connection closed"); }
internal static void RejectInvalidFuncArgs(TcpClient client) { SocketIO.Write(client, JsonManager.FormTechJson((int)StatusCodes.ErrorInvalidFunctionArguments)); Console.WriteLine(DateTime.Now + " CORE: [invalid arguments provided]"); }
internal static void AcceptFC(TcpClient client, long func_call_id) //запрос успешно обработан и будет поставлен в очередь { SocketIO.Write(client, JsonManager.FormTechJson((int)StatusCodes.Success, func_call_id)); Console.WriteLine(DateTime.Now + " CORE: accepted call #" + func_call_id); }
internal static void RejectMarketClosed(TcpClient client) //отклонение запроса из-за приостановки торгов { SocketIO.Write(client, JsonManager.FormTechJson((int)StatusCodes.ErrorMarketClosed)); Console.WriteLine(DateTime.Now + " CORE: [market closed]"); }
internal static void RejectInvalidJson(TcpClient client) //отклонение запроса из-за невалидного JSON { SocketIO.Write(client, JsonManager.FormTechJson((int)StatusCodes.ErrorInvalidJsonInput)); Console.WriteLine(DateTime.Now + " CORE: [tech JSON parse failed]"); }
internal static void RejectFuncNotFound(TcpClient client) //отклонение запроса из-за отсутствия функции в ядре { SocketIO.Write(client, JsonManager.FormTechJson((int)StatusCodes.ErrorFunctionNotFound)); Console.WriteLine(DateTime.Now + " CORE: [function not found]"); }
private void ListenHandleDaemonThread() //TODO add IP restriction { //запуск TCP-акцептора try { TcpListener listener = new TcpListener(IPAddress.Any, DaemonPort); listener.Start(); while (true) { Console.WriteLine(DateTime.Now + " DAEMON: waiting for a connection"); //ожидаем подключения сервисных клиентов TcpClient client = listener.AcceptTcpClient(); //проверка по IP клиента: DAEMON string remote_ip = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString(); //получаем IP подключившегося клиента Console.WriteLine(DateTime.Now + " DAEMON: connection request from " + remote_ip); // <<<< MOVE INSIDE THE COMMENTED SNIPPET BELOW (TO ADD IP RECTRICTION) Console.WriteLine(DateTime.Now + " DAEMON: client connected [test mode]"); while (true) { //попытка преобразовать сообщение в JSON и отправить его демону IJsonSerializable msg; if (Queues.daemon_queue.TryPeek(out msg)) { bool _sent = SocketIO.Write(client, msg.Serialize()); if (_sent) { Console.WriteLine(DateTime.Now + " DAEMON: message sent"); Queues.daemon_queue.TryDequeue(out msg); } else { Console.WriteLine(DateTime.Now + " DAEMON: failed to send a message (dc)"); break; } } } client.Close(); Console.WriteLine(DateTime.Now + " DAEMON: connection closed"); //if (remote_ip == DaemonIP) //DAEMON //{ // Console.WriteLine(DateTime.Now + " DAEMON: client connected"); // // //логика выгребания из очереди в сокет // //MOVE LOGIC HERE // //} //else //неизвестный клиент //{ // client.Close(); // Console.WriteLine(DateTime.Now + " DAEMON: client rejected by IP restriction"); //} } } catch (Exception e) { Console.WriteLine(DateTime.Now + " ==TCP ERROR=="); Console.WriteLine(e.ToString()); } }