コード例 #1
0
        /// <summary>
        /// Listens for and hangles UDP transactions
        /// </summary>
        /// <param name="PortNum">Port to listen on for TCP connections</param>
        /// <returns>None</returns>
        private void UDPListener(int PortNum)
        {
            UdpClient  vUDPclient = new UdpClient(PortNum);                                  // Setup new UDP listen server
            IPEndPoint vEndPoint  = new IPEndPoint(IPAddress.Any, PortNum);                  // Setup the endpoint to any address available on computer

            Console.WriteLine("UDP Server Listening on Port " + PortNum.ToString() + "..."); // Write to console
            while (true)                                                                     // Loop forever TODO: Add a way to end loop
            {
                try
                {
                    String          vMessageIN  = Encoding.Default.GetString(vUDPclient.Receive(ref vEndPoint));                              // Wait for UDP data and convert it to string
                    string          vClientIP   = vEndPoint.Address.ToString();                                                               // Store the client IP Address
                    int             vClientPort = vEndPoint.Port;                                                                             // Store the client Port number
                    MXSocketMessage vMessage    = new MXSocketMessage(vClientIP, vClientPort, vMessageIN);                                    // Create a new Socket Message from received data
                    Console.WriteLine("UDP Client " + vMessage.IPAddress + ":" + vMessage.Port.ToString() + " - RCV Data:  " + vMessage.Raw); // Write to Console

                    OnMessageReceived(new MessageReceivedArgs(vMessage));                                                                     // Raise OnMessageReceived event to host object
                    if (vMessage.Responce == "")
                    {
                        vMessage.Responce = "Error - No Listeners Available";                                                         // If no responce was set, set responce to Error
                    }
                    string vOut = vMessage.Command + "," + vMessage.Responce;                                                         // Create string to send back to client
                    Console.WriteLine("UDP Client " + vMessage.IPAddress + ":" + vMessage.Port.ToString() + " - SND Data:  " + vOut); // Write to Console
                    byte[] vBytes = Encoding.ASCII.GetBytes(vOut);                                                                    // Convert output string to byte array
                    vUDPclient.Send(vBytes, vBytes.Length, vEndPoint);                                                                // Send byte array to client
                }
                catch (Exception vErr)
                {
                    Console.WriteLine("UDP Error - " + vErr.Message); // Write Error to console
                }
                Thread.Sleep(1);                                      // Sleep 1ms so we don't swamp the CPU
            }
        }
コード例 #2
0
        /// <summary>
        /// Handles the IP command from the socket and sends back the hostname, IP Address, # of current TCP connections.
        /// </summary>
        /// <param name="vMessage">Contains the Socket message that was received</param>
        /// <returns>Returns string that is then sent back to the client</returns>
        public string SocketIP(MXSocketMessage vMessage)
        {
            List <string> vIPs = GetMyIPs();
            string        vIP  = vIPs[0];

            foreach (string vAddress in vIPs)
            {
                if (vMessage.IPAddress.Contains(vAddress.Remove(vAddress.LastIndexOf("."))))
                {
                    vIP = vAddress;
                    break;
                }
            }
            return(Dns.GetHostName() + "," + vIP + "," + vServer.TCPcnt.ToString());
        }
コード例 #3
0
        /// <summary>
        /// Handles a single TCP connection.  Listens for new TCP traffic and raises the OnMessageReceived Event
        /// when TCP data is received.  After event is handled it sends back the responce to the TCP client.
        /// </summary>
        /// <param name="TCPclient">TcpClient class that handles the TCP connection</param>
        /// <returns>None</returns>
        private void TCPHandler(TcpClient TCPclient)
        {
            TCPcnt++;                                                                                     // Increment the TCP client connection counter
            string vClientIP   = ((IPEndPoint)TCPclient.Client.RemoteEndPoint).Address.ToString();        // Store the client IP address
            int    vClientPort = ((IPEndPoint)TCPclient.Client.RemoteEndPoint).Port;                      // Store the client Port number

            Console.WriteLine("TCP Client " + vClientIP + ":" + vClientPort.ToString() + " - Connected"); // Write to console

            TCPclient.ReceiveTimeout = 15000;
            while (TCPclient.Connected)  // Loop until the client is not longer connected or the connection times out
            {
                try
                {
                    NetworkStream vStream    = TCPclient.GetStream();                    // Create a new stream for reading the writing to client
                    Byte[]        vBuffer    = new Byte[TCPclient.ReceiveBufferSize];    // Create a buffer for reading and writing to client
                    int           vBytesRead = vStream.Read(vBuffer, 0, vBuffer.Length); // Read data from the client stream
                    if (vBytesRead > 0)
                    {
                        String          vMessageIN = Encoding.Default.GetString(vBuffer, 0, vBytesRead);                                          // Convert data read from client to string
                        MXSocketMessage vMessage   = new MXSocketMessage(vClientIP, vClientPort, vMessageIN);                                     // Create new Socket Message object from received data
                        Console.WriteLine("TCP Client " + vMessage.IPAddress + ":" + vMessage.Port.ToString() + " - RCV Data:  " + vMessage.Raw); // Write to Console

                        OnMessageReceived(new MessageReceivedArgs(vMessage));                                                                     // Raise OnMessageReceived event to host object
                        if (vMessage.Responce == "")
                        {
                            vMessage.Responce = "Error - No Listeners Available";                                                         // If no responce was set, set responce to Error
                        }
                        string vOut = vMessage.Command + "," + vMessage.Responce;                                                         // Create string to send back to client
                        Console.WriteLine("TCP Client " + vMessage.IPAddress + ":" + vMessage.Port.ToString() + " - SND Data:  " + vOut); // Write to Console
                        vBuffer = Encoding.ASCII.GetBytes(vOut);                                                                          // Convert output string to byte array
                        vStream.Write(vBuffer, 0, vBuffer.Length);                                                                        // Write byte array to client stream
                    }
                    else
                    {
                        TCPclient.Close();   // Client closed connection
                    }
                }
                catch (Exception)
                {
                    Console.WriteLine("TCP Client " + vClientIP + ":" + vClientPort.ToString() + " - Timed Out"); // Write to Console
                    TCPclient.Close();                                                                            // Make sure Connection is closed
                }
                Thread.Sleep(1);                                                                                  // Sleep 1ms so we don't swamp the CPU
            }

            Console.WriteLine("TCP Client " + vClientIP + ":" + vClientPort.ToString() + " - Disconnected"); // Write to Console
            TCPcnt--;                                                                                        // Decrement the TCP client connection counter
        }
コード例 #4
0
        /// <summary>
        /// Handles the EQUIPMENTSTATUS command from the socket and sends back the status of
        /// the requested equipment.
        /// </summary>
        /// <param name="vMessage">Contains the Socket message that was received</param>
        /// <returns>Returns string that is then sent back to the client</returns>
        public string SocketEQUIPMENTSTATUS(MXSocketMessage vMessage)
        {
            string vRet = "";  // Return string to client

            if (vMessage.Data.Length == 0)
            {
                vRet  = $"LED_Safe;{ safe }";
                vRet += $",LED_HV;{ hvEnable }";
                vRet += $",LED_XRays;{ xRays }";
                vRet += $",LED_Testing;{ testing }";
                vRet += $",LED_Error;{ error }";
                vRet += $",EStop;{ eStopPressed }";
            }
            else
            {
                vRet = "Error - Wrong Number of Arguments"; // Return error
            }
            return(vRet);                                   // Return string to client
        }
コード例 #5
0
        /// <summary>
        /// Handles the STATIONSTATUS command from the socket and sends back the status of
        /// the requested station.
        /// </summary>
        /// <param name="vMessage">Contains the Socket message that was received</param>
        /// <returns>Returns string that is then sent back to the client</returns>
        public string SocketSTATIONSTATUS(MXSocketMessage vMessage)
        {
            string vRet = "";                                              // Return string to client

            if (vMessage.Data.Length == 1)                                 // Make sure station number was sent
            {
                int        StationNum = Convert.ToInt16(vMessage.Data[0]); // Station Number data is requested for
                ctlStation s          = GetStation(StationNum);
                if (s == null)
                {
                    vRet = $"Station;{ StationNum },Connected;false";
                }
                else
                {
                    vRet  = $"Station;{ StationNum }";
                    vRet += $",StationName;{ s.Name }";
                    vRet += $",Connected;{ s.tub.connected }";
                    vRet += $",SerialNum;{ s.serialNum }";
                    vRet += $",TubeName;{ s.tub.name }";
                    vRet += $",TubeStatus;{ s.tub.status }";
                    string hv = s.tub.on ? "On" : "Off";
                    vRet += $",HVStatus;{ hv }";
                    vRet += $",SetKV;{ s.tub.setkV }";
                    vRet += $",SetUA;{ s.tub.setuA }";
                    vRet += $",MonKV;{ s.tub.monkV }";
                    vRet += $",MonUA;{ s.tub.monuA }";
                    vRet += $",Status;{ s.lblStatus.Text }";
                    vRet += $",Progress;{ s.progressBar.Value }";
                    vRet += $",ErrorReason;{ s.errReason }";
                    vRet += $",ProductImage;{ s.tub.productImageFilePath}";
                    vRet += $",LoggingEnabled;{ s.chkLog.Checked }";
                    vRet += $",LEDOnColor;{ s.ledStatus.OnColor.ToKnownColor().ToString() }";
                    vRet += $",LEDOffColor;{ s.ledStatus.OffColor.ToKnownColor().ToString() }";
                    vRet += $",LEDEnabled;{ s.ledStatus.Value }";
                }
            }
            else
            {
                vRet = "Error - Wrong Number of Arguments"; // Return error
            }
            return(vRet);                                   // Return string to client
        }
コード例 #6
0
 /// <summary>
 /// Handles the VERSION command from the socket and sends back the application version.
 /// </summary>
 /// <param name="vMessage">Contains the Socket message that was received</param>
 /// <returns>Returns string that is then sent back to the client</returns>
 public string SocketVERSION(MXSocketMessage vMessage)
 {
     return(appVersion);
 }
コード例 #7
0
 /// <summary>
 /// Handles the PING command from the socket
 /// </summary>
 /// <param name="vMessage">Contains the Socket message that was received</param>
 /// <returns>Returns string that is then sent back to the client</returns>
 public string SocketPING(MXSocketMessage vMessage)
 {
     return("PING");
 }
コード例 #8
0
 public MessageReceivedArgs(MXSocketMessage _Message)
 {
     vMessage = _Message;
 }