public void RunThread()
        {
            try
            {
                tcpClient = SocketServer.listener.AcceptTcpClient();

                lock (SocketServer)
                {
                    KeepAliveTimer = 0;

                    socketStream = new nsRSMPGS.cSocketStream(cHelper.IsSettingChecked("UseEncryption"));

                    cTcpSocketServerThread ServerThread = new cTcpSocketServerThread(SocketServer);
                    new Thread(new ThreadStart(ServerThread.RunThread)).Start();
                    SocketServer.ServerSockets.Add(ServerThread);
                }

                if (socketStream.InitializeAsServerAndGetStream(tcpClient, RSMPGS.EncryptionSettings))
                {
                    encoding = Encoding.GetEncoding("Windows-1252");

                    sClientIP = ((IPEndPoint)(tcpClient.Client.RemoteEndPoint)).Address.ToString() + ":" + ((IPEndPoint)(tcpClient.Client.RemoteEndPoint)).Port.ToString();

                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "Client connection was accepted " + sClientIP);

                    tcpClient.NoDelay = RSMPGS.MainForm.ToolStripMenuItem_DisableNagleAlgorithm.Checked;

                    RSMPGS.MainForm.BeginInvoke(RSMPGS.MainForm.DelegateSocketWasConnected);

                    while (SocketServer.TcpSocket.ThreadShouldRun)
                    {
                        if (RSMPGS.RSMPConnection.ReadBytesAndParsePacket(ref socketStream, ref tcpClient, ref inBuffer, ref inBufferLength) == false)
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (socketStream != null)
                {
                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "Some problem occured (" + sClientIP + "), closed socket: " + e.Message);
                }
            }

            SocketServer.Disconnect();

            if (sClientIP.Length > 0)
            {
                RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Warning, "Client connection " + sClientIP + " was closed");
            }

            lock (SocketServer)
            {
                SocketServer.ServerSockets.Remove(this);
            }
        }
        private void ConnectToServerAndMaintainStream()
        {
            try
            {
                tcpClient = new TcpClient();
                try
                {
                    tcpClient.Connect(SocketClient.ServerAddress, SocketClient.ServerPort);
                }
                catch (Exception e)
                {
                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "Could not connect (" + sClientIP + "): " + e.Message);
                    SocketClient.Disconnect();
                    return;
                }

                KeepAliveTimer = 0;

                inBufferLength = 0;
                socketStream   = new nsRSMPGS.cSocketStream(cHelper.IsSettingChecked("UseEncryption"));

                if (socketStream.InitializeAsClientAndGetStream(tcpClient, RSMPGS.EncryptionSettings))
                {
                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Info, "We connected to RSMP client " + sClientIP);

                    tcpClient.NoDelay = RSMPGS.MainForm.ToolStripMenuItem_DisableNagleAlgorithm.Checked;

                    RSMPGS.MainForm.BeginInvoke(RSMPGS.MainForm.DelegateSocketWasConnected);

                    while (SocketClient.TcpSocket.ThreadShouldRun)
                    {
                        if (RSMPGS.RSMPConnection.ReadBytesAndParsePacket(ref socketStream, ref tcpClient, ref inBuffer, ref inBufferLength) == false)
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (socketStream != null)
                {
                    RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "Some problem occured (" + sClientIP + "), closed socket: " + e.Message);
                }
            }
            SocketClient.Disconnect();
        }
Exemple #3
0
        public bool ReadBytesAndParsePacket(ref cSocketStream socketStream, ref TcpClient tcpClient, ref byte[] inBuffer, ref int inBufferLength)
        {
            string sJSon;
            int    iPacketLength;

            bool bSuccess = true;

            // Read the server message into a byte buffer
            int iReadBytes = socketStream.Read(inBuffer, inBufferLength, inBuffer.GetLength(0) - inBufferLength);

            if (iReadBytes <= 0)
            {
                return(false);
            }

            RSMPGS.SysLog.AddRawDebugData(true, cSysLogAndDebug.Direction_In, false, inBuffer, inBufferLength, iReadBytes);

            //TimeoutTimer = 0;
            inBufferLength += iReadBytes;

            switch (WrapMethod)
            {
            case cTcpHelper.WrapMethod_None:

                sJSon = Encoding.UTF8.GetString(inBuffer, 0, inBufferLength);
                RSMPGS.MainForm.BeginInvoke(RSMPGS.MainForm.DelegateDecodeJSonPacket, new Object[] { sJSon });
                inBufferLength = 0;
                break;

            case cTcpHelper.WrapMethod_LengthPrefix:

                // At least we need to have the length bytes
                while (inBufferLength >= 4)
                {
                    // Determine packet length
                    iPacketLength = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(inBuffer, 0));

                    // Hopefully not too big packet (or something went very wrong)
                    if (iPacketLength >= (inBuffer.GetLength(0) - 4))
                    {
                        RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "Packet size was reported to {0} bytes, but could be no more than {1} bytes", iPacketLength, inBuffer.GetLength(0) - 4);
                        bSuccess = false;
                    }
                    else
                    {
                        // Got enough of bytes?
                        if ((iPacketLength + 4) <= inBufferLength)
                        {
                            sJSon = Encoding.UTF8.GetString(inBuffer, 4, iPacketLength);
                            RSMPGS.MainForm.BeginInvoke(RSMPGS.MainForm.DelegateDecodeJSonPacket, new Object[] { sJSon });
                            RSMPGS.Statistics["RxPackets"]++;
                            RSMPGS.Statistics["RxBytes"] += iPacketLength + 4;
                        }
                        else
                        {
                            // Did not get enough, wait for more
                            break;
                        }
                    }
                    // Some more, do some buffer byte move or just clear the length
                    inBufferLength -= 4;
                    inBufferLength -= iPacketLength;
                    if (inBufferLength > 0)
                    {
                        Buffer.BlockCopy(inBuffer, iPacketLength + 4, inBuffer, 0, inBufferLength);
                    }
                }
                break;

            case cTcpHelper.WrapMethod_FormFeed:

                // Find LF's, repeat until no more
                bool bFoundFormFeed;

                do
                {
                    bFoundFormFeed = false;
                    for (iPacketLength = 0; iPacketLength < inBufferLength; iPacketLength++)
                    {
                        if (inBuffer[iPacketLength].CompareTo(0x0c) == 0)
                        {
                            sJSon = Encoding.UTF8.GetString(inBuffer, 0, iPacketLength);
                            RSMPGS.MainForm.BeginInvoke(RSMPGS.MainForm.DelegateDecodeJSonPacket, new Object[] { sJSon });
                            iPacketLength++;
                            inBufferLength -= iPacketLength;
                            if (inBufferLength > 0)
                            {
                                Buffer.BlockCopy(inBuffer, iPacketLength, inBuffer, 0, inBufferLength);
                            }
                            RSMPGS.Statistics["RxPackets"]++;
                            RSMPGS.Statistics["RxBytes"] += iPacketLength;
                            bFoundFormFeed = true;
                            break;
                        }
                    }
                } while (bFoundFormFeed == true && inBufferLength > 0);

                break;
            }

            if (inBuffer.GetLength(0) == inBufferLength)
            {
                RSMPGS.SysLog.SysLog(cSysLogAndDebug.Severity.Error, "Packet size was too big (> {0} bytes)", inBuffer.GetLength(0));
                bSuccess = false;
            }

            return(bSuccess);
        }