0

My server program, deploy on many product environment run well, buy on one of machine which doesn't run as we expect.

On this machine , when we call SslStream.BeginRead, then when SslStream.EndRead Called with 0 return, if we don't close the stream, and call the SslStream again, there is an exception throwed。

private void Read()
    {
        try
        {
            if (this._IsClosed)
            {
                return;
            }
            this._Stream.BeginRead(this._Buffer, 0,     BufferManager.Default.OutterReadBufferSize, this.EndRead, null);
        }
        catch (Exception ex)
        {
            _Logger.WarnFormat("Begin Read, session:{0}, {1}", this._Id, ex);
            this.Close();
        }
    }





private void EndReadTaskAction(object ar)
    {
        try
        {
            int len = this._Stream.EndRead((IAsyncResult)ar);
            int used = 0;
            if (len <= 0)
            {
                _Logger.WarnFormat("EndReadTaskAction len less or equal zero, len={0}, session={1}", len, _Id);
                this.Close();
                return;
            }

            if (this._HasPartialPacket)
            {
                if (this._PartialReadedLenth < PacketConstants.HeadLength)
                {
                    int needToRead = PacketConstants.HeadLength - this._PartialReadedLenth;
                    if (needToRead > len)
                    {
                        Buffer.BlockCopy(this._Buffer, used, this._Buffer, GetCurrentPartialReadIndex(), len);
                        this._PartialReadedLenth += len;
                        Read();
                        return;
                    }
                    Buffer.BlockCopy(this._Buffer, used, this._Buffer, GetCurrentPartialReadIndex(), needToRead);
                    len -= needToRead;
                    used += needToRead;
                    this._PartialReadedLenth += needToRead;
                }
                int packetLength = PacketHelper.GetPacketLength(this._Buffer, this._PartialReadIndex);
                int howMuchNeedToRead = packetLength - this._PartialReadedLenth;
                if (howMuchNeedToRead > len)
                {
                    Buffer.BlockCopy(this._Buffer, used, this._Buffer, GetCurrentPartialReadIndex(), len);
                    this._PartialReadedLenth += len;
                    Read();
                    return;
                }
                Buffer.BlockCopy(this._Buffer, used, this._Buffer, GetCurrentPartialReadIndex(), howMuchNeedToRead);
                ProcessPackage(this._Buffer, this._PartialReadIndex, packetLength);
                len -= howMuchNeedToRead;
                used += howMuchNeedToRead;
            }
            this._HasPartialPacket = false;
            this._PartialReadedLenth = 0;
            while (true)
            {
                if (len <= 0)
                {
                    break;
                }

                if (len < PacketConstants.HeadLength)
                {
                    Buffer.BlockCopy(this._Buffer, used, this._Buffer, this._PartialReadIndex, len);
                    this._PartialReadedLenth = len;
                    this._HasPartialPacket = true;
                    break;
                }
                int packetLength = PacketHelper.GetPacketLength(this._Buffer, used);
                if (len < packetLength)
                {
                    Buffer.BlockCopy(this._Buffer, used, this._Buffer, this._PartialReadIndex, len);
                    this._PartialReadedLenth = len;
                    this._HasPartialPacket = true;
                    break;
                }
                ProcessPackage(this._Buffer, used, packetLength);
                used += packetLength;
                len -= packetLength;
            }
            if (_isBeginRead)
            {
                Read();
            }
        }
        catch (Exception ex)
        {
            _Logger.WarnFormat("End read, session:{0}, {1}", this._Id, ex);
            this.Close();
        }
    }
Robert Hu
  • 1
  • 1

1 Answers1

0

Your are saying on the server deployed you dont have any problem and in the new machine you got a problem, Please check all dll's if include and its dependencies, .net framework run time.

Hope it helps..

Vijunav Vastivch
  • 4,153
  • 1
  • 16
  • 30
  • Dll's and its dependencies are all good。when I readed the first packet from the client, then don't read from the stream, after the login operation complete, and send the login information to the client. After the above operations , then start read, and it's run normally。 Buy this hack looks strangly – Robert Hu Jul 22 '16 at 01:39