Example #1
0
        /// <summary>
        /// closes socket and all streams connected to it
        /// </summary>
        public void TrueClose()
        {
            if (Log.IsDebugEnabled)
            {
                Log.Debug(GetLocalizedString("true close socket").Replace("$$Socket$$", ToString()).Replace("$$Lifespan$$", DateTime.Now.Subtract(_created).ToString()));
            }

            bool          err    = false;
            StringBuilder errMsg = new StringBuilder();

            if (_socket == null || _networkStream == null)
            {
                err = true;
                errMsg.Append(GetLocalizedString("socket already closed"));
            }

            if (_socket != null)
            {
                try
                {
                    _socket.Close();
                }
                catch (IOException ioe)
                {
                    if (Log.IsErrorEnabled)
                    {
                        Log.Error(GetLocalizedString("error closing socket").Replace("$$ToString$$", ToString()).Replace("$$Host$$", Host), ioe);
                    }
                    errMsg.Append(GetLocalizedString("error closing socket").Replace("$$ToString$$", ToString()).Replace("$$Host$$", Host) + System.Environment.NewLine);
                    errMsg.Append(ioe.ToString());
                    err = true;
                }
                catch (SocketException soe)
                {
                    if (Log.IsErrorEnabled)
                    {
                        Log.Error(GetLocalizedString("error closing socket").Replace("$$ToString$$", ToString()).Replace("$$Host$$", Host), soe);
                    }
                    errMsg.Append(GetLocalizedString("error closing socket").Replace("$$ToString$$", ToString()).Replace("$$Host$$", Host) + System.Environment.NewLine);
                    errMsg.Append(soe.ToString());
                    err = true;
                }
            }

            // check in to pool
            if (_socket != null)
            {
                _pool.CheckIn(this, false);
            }

            _networkStream = null;
            _socket        = null;

            if (err)
            {
                throw new IOException(errMsg.ToString());
            }
        }