示例#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);
        }