示例#1
0
        private void OnReceive(IAsyncResult res)
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Policy connection receiving request");
                }

                _received += _connection.EndReceive(res);
                if (_received < _policyRequestString.Length)
                {
                    if (log.IsDebugEnabled)
                    {
                        log.Debug(string.Format("Policy connection received partial request: {0} bytes", _received));
                    }

                    _connection.BeginReceive(_buffer, _received, _policyRequestString.Length - _received, SocketFlags.None, new AsyncCallback(OnReceive), null);
                    return;
                }
                string request = System.Text.Encoding.UTF8.GetString(_buffer, 0, _received);
#if !(NET_1_1)
                if (StringComparer.InvariantCultureIgnoreCase.Compare(request, _policyRequestString) != 0)
#else
                if (request != _policyRequestString)
#endif
                {
                    if (log.IsDebugEnabled)
                    {
                        log.Debug(string.Format("Policy connection could not handle request: {0}", request));
                    }

                    _policyServer.RaiseDisconnect(_endpoint);
                    _connection.Close();
                    return;
                }
                // Sending the policy
                if (log.IsDebugEnabled)
                {
                    log.Debug("Policy connection sending policy stream");
                }
                _connection.BeginSend(_policy, 0, _policy.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
            }
            catch (ObjectDisposedException)
            {
                //The underlying socket may be closed
                _policyServer.RaiseDisconnect(_endpoint);
            }
            catch (SocketException ex)
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Socket exception", ex);
                }

                _policyServer.RaiseDisconnect(_endpoint);
                _connection.Close();
            }
            catch (Exception ex)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("Policy connection failed", ex);
                }
            }
        }