/// <summary> /// Opens an existing file for reading. /// </summary> protected override BinaryReader OpenRead(ConnectedClient client, RelativePath path) { ScadaInstance instance = GetClientInstance(client); string fileName; if (IsServiceFolder(path) && path.AppFolder == AppFolder.Log) { fileName = instance.PathBuilder.GetAbsolutePath(path); } else if (IsConfigFolder(path) && path.Path.StartsWith(AgentConst.DownloadConfigPrefix)) { fileName = Path.Combine(coreLogic.AppDirs.TempDir, path.Path); if (!instance.PackConfig(fileName, path)) { throw new ProtocolException(ErrorCode.InternalServerError); } } else { throw new ProtocolException(ErrorCode.IllegalFunctionArguments, CommonPhrases.PathNotSupported); } Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); return(new BinaryReader(stream, Encoding.UTF8, false)); }
/// <summary> /// Redirects the request from the Administrator client to an Agent client. /// </summary> private bool RedirectRequest(ConnectedClient adminClient, ScadaInstance instance, DataPacket dataPacket) { if (clientBundles.TryGetValue(instance.Name, out ClientBundle clientBundle) && clientBundle.AdminClient == adminClient && clientBundle.AgentClient is ConnectedClient agentClient) { try { lock (agentClient) { dataPacket.SessionID = agentClient.SessionID; dataPacket.Encode(); agentClient.NetStream.Write(dataPacket.Buffer, 0, dataPacket.BufferLength); agentClient.RegisterActivity(); } return(true); } catch (Exception ex) { log.WriteError(Locale.IsRussian ? "Ошибка при переадресации запроса на {0}: {1}" : "Error redirecting request to {0}: {1}", agentClient.Address, ex.Message); return(false); } finally { dataPacket.SessionID = adminClient.SessionID; } } else { return(false); } }
/// <summary> /// Gets the instance by name. /// </summary> public bool GetInstance(string name, out ScadaInstance scadaInstance) { if (instances == null) { scadaInstance = null; return(false); } else { return(instances.TryGetValue(name, out scadaInstance)); } }
/// <summary> /// Получить экземпляр системы по наименованию /// </summary> public ScadaInstance GetScadaInstance(string name) { if (settings.Instances.TryGetValue(name, out ScadaInstanceSettings instanceSettings)) { object syncRoot = locks.GetOrAdd(name, (key) => { return(new object()); }); ScadaInstance scadaInstance = new ScadaInstance(instanceSettings, syncRoot, log); return(scadaInstance); } else { return(null); } }
/// <summary> /// Gets the current status of the specified service. /// </summary> private void GetServiceStatus(ConnectedClient client, ScadaInstance instance, DataPacket request, out ResponsePacket response) { ServiceApp serviceApp = (ServiceApp)client.InBuf[ArgumentIndex]; bool result = instance.GetServiceStatus(serviceApp, out ServiceStatus serviceStatus); byte[] buffer = client.OutBuf; response = new ResponsePacket(request, buffer); int index = ArgumentIndex; CopyBool(result, buffer, ref index); CopyByte((byte)serviceStatus, buffer, ref index); response.BufferLength = index; }
/// <summary> /// Sends the command to the service. /// </summary> private void ControlService(ConnectedClient client, ScadaInstance instance, DataPacket request, out ResponsePacket response) { byte[] buffer = client.InBuf; int index = ArgumentIndex; ServiceApp serviceApp = (ServiceApp)GetByte(buffer, ref index); ServiceCommand serviceCommand = (ServiceCommand)GetByte(buffer, ref index); int timeout = GetInt32(buffer, ref index); bool result = instance.ControlService(serviceApp, serviceCommand, timeout); response = new ResponsePacket(request, client.OutBuf); index = ArgumentIndex; CopyBool(result, client.OutBuf, ref index); response.BufferLength = index; }
/// <summary> /// Registers the newly connected client for the proxy instance. /// </summary> private void RegisterClient(ConnectedClient client, ScadaInstance instance) { if (instance.ProxyMode && clientBundles.TryGetValue(instance.Name, out ClientBundle clientBundle)) { lock (clientBundle) { if (client.RoleID == AgentRoleID.Administrator) { clientBundle.AdminClient = client; } else if (client.RoleID == AgentRoleID.Agent) { clientBundle.AgentClient = client; } } } }
/// <summary> /// Redirects the response from the Agent client to an Administrator client. /// </summary> private void RedirectResponse(ConnectedClient agentClient, ScadaInstance instance, DataPacket dataPacket) { if (clientBundles.TryGetValue(instance.Name, out ClientBundle clientBundle) && clientBundle.AgentClient == agentClient && clientBundle.AdminClient is ConnectedClient adminClient) { try { lock (adminClient) { dataPacket.SessionID = adminClient.SessionID; dataPacket.Encode(); adminClient.NetStream.Write(dataPacket.Buffer, 0, dataPacket.BufferLength); } } catch (Exception ex) { log.WriteError(Locale.IsRussian ? "Ошибка при переадресации ответа на {0}: {1}" : "Error redirecting response to {0}: {1}", agentClient.Address, ex.Message); } } }
/// <summary> /// Set authorized user data /// </summary> public void SetUser(string username, ScadaInstance scadaInstance) { LoggedOn = true; Username = username; ScadaInstance = scadaInstance; }
/// <summary> /// Gets the instance associated with the client. /// </summary> private bool GetClientInstance(ConnectedClient client, out ScadaInstance scadaInstance) { scadaInstance = GetClientTag(client).Instance; return(scadaInstance != null); }