Esempio n. 1
0
        public static string ReadVariable(ref Socket clientSocket, string variableRead, GH_Component component)
        {
            if (clientSocket == null)
            {
                throw new ArgumentNullException(nameof(clientSocket));
            }
            if (variableRead == null)
            {
                throw new ArgumentNullException(nameof(variableRead));
            }
            if (component == null)
            {
                throw new ArgumentNullException(nameof(component));
            }

            byte[] messageReq    = ReadMessageRequest(variableRead, out var outputString);
            byte[] receivedData  = new byte[256];
            int    receivedBytes = 0;

            try                                                //Try to receive message.
            {
                int sentBytes = clientSocket.Send(messageReq); //Request a specific message according to VarRead variable

                component.AddRuntimeMessage(GH_RuntimeMessageLevel.Remark,
                                            "Sent:" + sentBytes.ToString() + " bytes as " + outputString);
                component.Message = "Sent";

                receivedBytes = clientSocket.Receive(receivedData); //Receive data back.
            }
            catch (SocketException e)
            {
                component.ClearRuntimeMessages();
                component.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Your connection is abruptly closed by the host, make sure to check your cables and if the server is running.");
            }
            catch (ArgumentNullException e)
            {
                component.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Read Variable :{0}" + e.ToString());
            }
            catch (ObjectDisposedException e)
            {
                component.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Read Variable :{0}" + e.ToString());
            }
            catch (SecurityException e)
            {
                component.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Write Variable Receive :{0}" + e.ToString());
            }

            //Format received data to extract value.
            MessageReceiveFormat response = new MessageReceiveFormat(receivedData);

            component.AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Received:" + receivedBytes.ToString() + " bytes as" + response._varValue);
            component.Message = "Received";

            return(response._varValue);
        }
Esempio n. 2
0
        public static string WriteVariable(ref Socket clientSocket, string variableWrite, string dataWrite, GH_Component component)
        {
            if (clientSocket == null)
            {
                throw new ArgumentNullException(nameof(clientSocket));
            }
            if (variableWrite == null)
            {
                throw new ArgumentNullException(nameof(variableWrite));
            }
            if (dataWrite == null)
            {
                throw new ArgumentNullException(nameof(dataWrite));
            }
            if (component == null)
            {
                throw new ArgumentNullException(nameof(component));
            }

            byte[] messageReq    = WriteMessageRequest(variableWrite, dataWrite, out string outputString);
            byte[] receivedData  = new byte[256];
            int    receivedBytes = 0;

            try
            {
                int sentBytes = clientSocket.Send(messageReq);
                component.AddRuntimeMessage(GH_RuntimeMessageLevel.Remark,
                                            "Sent:" + sentBytes.ToString() + " bytes as " + outputString);
                component.Message = "Sent";
                receivedBytes     = clientSocket.Receive(receivedData);
            }
            catch (SocketException e)
            {
                component.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "SocketException :{0}" + e.ToString());
            }
            catch (ArgumentNullException e)
            {
                component.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Write Variable :{0}" + e.ToString());
            }
            catch (ObjectDisposedException e)
            {
                component.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Write Variable :{0}" + e.ToString());
            }
            catch (SecurityException e)
            {
                component.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Write Variable Receive :{0}" + e.ToString());
            }

            MessageReceiveFormat response = new MessageReceiveFormat(receivedData);

            component.AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Received:" + receivedBytes.ToString() + " bytes as " + response._varValue);
            component.Message = "Received";
            return(response._varValue);
        }