Esempio n. 1
0
    /// <summary>
    /// Repalce the ip address + port that is in hex notation with an ip address + port that is in decimal notation
    /// </summary>
    /// <param name="rawResult"></param>
    static void ReplaceHexNotation(string rawResult)
    {
        string[] splitResults = rawResult.Trim().Split('\n');

        Console.WriteLine("\n" + splitResults[0]);

        for (int i = 0; i < splitResults.Length; i++)
        {
            if (i == 1)
            {
                Console.WriteLine(header);
            }

            if (i > 2)
            {
                TCPResult result  = new TCPResult(splitResults[i]);
                string    message = result.GetMessage();
                if (message != null)
                {
                    Console.WriteLine(message);
                }
            }
        }
    }
Esempio n. 2
0
        public void ProcessArrow(TCPConnection TCPConnection)
        {
            #region Start

            TCPConnection.WriteLineToResponseStream(ServiceBanner);
            TCPConnection.NoDelay = true;

            Byte Byte;
            var  Buffer       = new Byte[1024];
            var  MemoryStream = new MemoryStream();
            var  EndOfCSVLine = EOLSearch.NotYet;
            var  ClientClose  = false;
            var  ServerClose  = false;

            #endregion

            try
            {
                do
                {
                    switch (TCPConnection.TryRead(out Byte, MaxInitialWaitingTimeMS: ReadTimeout))
                    {
                        #region DataAvailable

                    case TCPClientResponse.DataAvailable:

                        #region Check for CSV line ending...

                        if (EndOfCSVLine == EOLSearch.NotYet)
                        {
                            // 0x00 or \n
                            if (Byte == 0x00 || Byte == 0x0a)
                            {
                                EndOfCSVLine = EOLSearch.EoL_Found;
                            }

                            // \r
                            else if (Byte == 0x0d)
                            {
                                EndOfCSVLine = EOLSearch.R_Read;
                            }
                        }

                        // \n after a \r
                        else if (EndOfCSVLine == EOLSearch.R_Read)
                        {
                            if (Byte == 0x0a)
                            {
                                EndOfCSVLine = EOLSearch.EoL_Found;
                            }
                            else
                            {
                                EndOfCSVLine = EOLSearch.NotYet;
                            }
                        }

                        #endregion

                        #region ...or append read value(s) to internal buffer

                        if (EndOfCSVLine == EOLSearch.NotYet)
                        {
                            MemoryStream.WriteByte(Byte);
                        }

                        #endregion


                        #region If end-of-line -> process data...

                        else if (EndOfCSVLine == EOLSearch.EoL_Found)
                        {
                            if (MemoryStream.Length > 0 && OnNotification != null)
                            {
                                #region Check UTF8 encoding

                                var CSVLine = String.Empty;

                                try
                                {
                                    CSVLine = Encoding.UTF8.GetString(MemoryStream.ToArray());
                                }
                                catch (Exception)
                                {
                                    TCPConnection.WriteLineToResponseStream("Protocol Error: Invalid UTF8 encoding!");
                                }

                                #endregion

                                #region Check CSV separation

                                String[] CSVArray = null;

                                try
                                {
                                    CSVArray = CSVLine.Trim().
                                               Split(SplitCharacters,
                                                     StringSplitOptions.None).
                                               Select(v => v.Trim()).
                                               ToArray();
                                }
                                catch (Exception)
                                {
                                    TCPConnection.WriteLineToResponseStream("Protocol Error: Invalid CSV data!");
                                }

                                #endregion

                                #region Call OnNotification delegate

                                TCPResult <String> Result = null;

                                var OnNotificationLocal = OnNotification;
                                if (OnNotificationLocal != null)
                                {
                                    TCPConnection.WriteLineToResponseStream(OnNotificationLocal(TCPConnection,
                                                                                                DateTime.Now,
                                                                                                CSVArray));
                                }

                                #endregion
                            }

                            MemoryStream.SetLength(0);
                            MemoryStream.Seek(0, SeekOrigin.Begin);
                            EndOfCSVLine = EOLSearch.NotYet;
                        }

                        #endregion

                        break;

                        #endregion

                        #region CanNotRead

                    case TCPClientResponse.CanNotRead:
                        ServerClose = true;
                        break;

                        #endregion

                        #region ClientClose

                    case TCPClientResponse.ClientClose:
                        ClientClose = true;
                        break;

                        #endregion

                        #region Timeout

                    case TCPClientResponse.Timeout:
                        ServerClose = true;
                        break;

                        #endregion
                    }
                } while (!ClientClose && !ServerClose);
            }

            #region Process exceptions

            catch (IOException ioe)
            {
                if (ioe.Message.StartsWith("Unable to read data from the transport connection"))
                {
                }
                else if (ioe.Message.StartsWith("Unable to write data to the transport connection"))
                {
                }

                else
                {
                    //if (OnError != null)
                    //    OnError(this, DateTime.Now, ConnectionIdBuilder(newTCPConnection.RemoteIPAddress, newTCPConnection.RemotePort), ioe, MemoryStream);
                }
            }

            catch (Exception e)
            {
                //if (OnError != null)
                //    OnError(this, DateTime.Now, ConnectionIdBuilder(newTCPConnection.RemoteIPAddress, newTCPConnection.RemotePort), e, MemoryStream);
            }

            #endregion

            #region Close the TCP connection

            try
            {
                TCPConnection.Close(ClientClose ? ConnectionClosedBy.Client : ConnectionClosedBy.Server);
            }
#pragma warning disable RCS1075  // Avoid empty catch clause that catches System.Exception.
#pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
            catch (Exception)
            { }
#pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
#pragma warning restore RCS1075  // Avoid empty catch clause that catches System.Exception.

            #endregion
        }