Example #1
0
        void MakeRequest(
            UnsafeNativeMethods.StateProtocolVerb verb,
            String id,
            UnsafeNativeMethods.StateProtocolExclusive exclusiveAccess,
            int extraFlags,
            int timeout,
            int lockCookie,
            byte[]                                  buf,
            int cb,
            int networkTimeout,
            out UnsafeNativeMethods.SessionNDMakeRequestResults results)
        {
            int    hr;
            string uri;
            OutOfProcConnection conn = null;
            HandleRef           socketHandle;
            bool checkVersion = false;

            Debug.Assert(timeout <= SessionStateModule.MAX_CACHE_BASED_TIMEOUT_MINUTES, "item.Timeout <= SessionStateModule.MAX_CACHE_BASED_TIMEOUT_MINUTES");

            SessionIDManager.CheckIdLength(id, true /* throwOnFail */);

            if (_partitionInfo == null)
            {
                Debug.Assert(s_partitionManager != null);
                Debug.Assert(_partitionResolver != null);

                _partitionInfo = (StateServerPartitionInfo)s_partitionManager.GetPartition(_partitionResolver, id);

                // If its still null, we give up
                if (_partitionInfo == null)
                {
                    throw new HttpException(SR.GetString(SR.Bad_partition_resolver_connection_string, "PartitionManager"));
                }
            }

            // Need to make sure we dispose the connection if anything goes wrong
            try {
                conn = (OutOfProcConnection)_partitionInfo.RetrieveResource();
                if (conn != null)
                {
                    socketHandle = new HandleRef(this, conn._socketHandle.Handle);
                }
                else
                {
                    socketHandle = new HandleRef(this, INVALID_SOCKET);
                }

                if (_partitionInfo.StateServerVersion == -1)
                {
                    // We don't need locking here because it's okay to have two
                    // requests initializing s_stateServerVersion.
                    checkVersion = true;
                }

                Debug.Trace("OutOfProcSessionStateStoreMakeRequest",
                            "Calling MakeRequest, " +
                            "socket=" + (IntPtr)socketHandle.Handle +
                            "verb=" + verb +
                            " id=" + id +
                            " exclusiveAccess=" + exclusiveAccess +
                            " timeout=" + timeout +
                            " buf=" + ((buf != null) ? "non-null" : "null") +
                            " cb=" + cb +
                            " checkVersion=" + checkVersion +
                            " extraFlags=" + extraFlags);

                // Have to UrlEncode id because it may contain non-URL-safe characters
                uri = HttpUtility.UrlEncode(s_uribase + id);

                hr = UnsafeNativeMethods.SessionNDMakeRequest(
                    socketHandle, _partitionInfo.Server, _partitionInfo.Port, _partitionInfo.ServerIsIPv6NumericAddress /* forceIPv6 */, networkTimeout, verb, uri,
                    exclusiveAccess, extraFlags, timeout, lockCookie,
                    buf, cb, checkVersion, out results);

                Debug.Trace("OutOfProcSessionStateStoreMakeRequest", "MakeRequest returned: " +
                            "hr=" + hr +
                            " socket=" + (IntPtr)results.socket +
                            " httpstatus=" + results.httpStatus +
                            " timeout=" + results.timeout +
                            " contentlength=" + results.contentLength +
                            " uri=" + (IntPtr)results.content +
                            " lockCookie=" + results.lockCookie +
                            " lockDate=" + string.Format("{0:x}", results.lockDate) +
                            " lockAge=" + results.lockAge +
                            " stateServerMajVer=" + results.stateServerMajVer +
                            " actionFlags=" + results.actionFlags);

                if (conn != null)
                {
                    if (results.socket == INVALID_SOCKET)
                    {
                        conn.Detach();
                        conn = null;
                    }
                    else if (results.socket != socketHandle.Handle)
                    {
                        // The original socket is no good.  We've got a new one.
                        // Pleae note that EnsureConnected has closed the bad
                        // one already.
                        conn._socketHandle = new HandleRef(this, results.socket);
                    }
                }
                else if (results.socket != INVALID_SOCKET)
                {
                    conn = new OutOfProcConnection(results.socket);
                }

                if (conn != null)
                {
                    _partitionInfo.StoreResource(conn);
                }
            }
            catch {
                // We just need to dispose the connection if anything bad happened
                if (conn != null)
                {
                    conn.Dispose();
                }

                throw;
            }

            if (hr != 0)
            {
                HttpException e = CreateConnectionException(_partitionInfo.Server, _partitionInfo.Port, hr);

                string phase = null;

                switch (results.lastPhase)
                {
                case (int)UnsafeNativeMethods.SessionNDMakeRequestPhase.Initialization:
                    phase = SR.GetString(SR.State_Server_detailed_error_phase0);
                    break;

                case (int)UnsafeNativeMethods.SessionNDMakeRequestPhase.Connecting:
                    phase = SR.GetString(SR.State_Server_detailed_error_phase1);
                    break;

                case (int)UnsafeNativeMethods.SessionNDMakeRequestPhase.SendingRequest:
                    phase = SR.GetString(SR.State_Server_detailed_error_phase2);
                    break;

                case (int)UnsafeNativeMethods.SessionNDMakeRequestPhase.ReadingResponse:
                    phase = SR.GetString(SR.State_Server_detailed_error_phase3);
                    break;

                default:
                    Debug.Assert(false, "Unknown results.lastPhase: " + results.lastPhase);
                    break;
                }

                WebBaseEvent.RaiseSystemEvent(SR.GetString(SR.State_Server_detailed_error,
                                                           phase,
                                                           "0x" + hr.ToString("X08", CultureInfo.InvariantCulture),
                                                           cb.ToString(CultureInfo.InvariantCulture)),
                                              this, WebEventCodes.WebErrorOtherError, WebEventCodes.StateServerConnectionError, e);

                throw e;
            }

            if (results.httpStatus == 400)
            {
                if (s_usePartition)
                {
                    throw new HttpException(
                              SR.GetString(SR.Bad_state_server_request_partition_resolver,
                                           s_configPartitionResolverType, _partitionInfo.Server, _partitionInfo.Port.ToString(CultureInfo.InvariantCulture)));
                }
                else
                {
                    throw new HttpException(
                              SR.GetString(SR.Bad_state_server_request));
                }
            }

            if (checkVersion)
            {
                _partitionInfo.StateServerVersion = results.stateServerMajVer;
                if (_partitionInfo.StateServerVersion < WHIDBEY_MAJOR_VERSION)
                {
                    // We won't work with versions lower than Whidbey
                    if (s_usePartition)
                    {
                        throw new HttpException(
                                  SR.GetString(SR.Need_v2_State_Server_partition_resolver,
                                               s_configPartitionResolverType, _partitionInfo.Server, _partitionInfo.Port.ToString(CultureInfo.InvariantCulture)));
                    }
                    else
                    {
                        throw new HttpException(
                                  SR.GetString(SR.Need_v2_State_Server));
                    }
                }
            }
        }
Example #2
0
        void MakeRequest(
            UnsafeNativeMethods.StateProtocolVerb verb,
            String id,
            UnsafeNativeMethods.StateProtocolExclusive exclusiveAccess,
            int timeout,
            int lockCookie,
            byte[]                                  buf,
            int cb,
            int networkTimeout,
            out UnsafeNativeMethods.SessionNDMakeRequestResults results)
        {
            int    hr;
            string uri;
            OutOfProcConnection conn;
            HandleRef           socketHandle;

            conn = (OutOfProcConnection)s_rpool.RetrieveResource();
            if (conn != null)
            {
                socketHandle = new HandleRef(this, conn._socketHandle.Handle);
            }
            else
            {
                socketHandle = new HandleRef(this, INVALID_SOCKET);
            }

            Debug.Trace("SessionStateClientManagerMakeRequest",
                        "Calling MakeRequest, " +
                        "socket=" + (IntPtr)socketHandle.Handle +
                        "verb=" + verb +
                        " id=" + id +
                        " exclusiveAccess=" + exclusiveAccess +
                        " timeout=" + timeout +
                        " buf=" + ((buf != null) ? "non-null" : "null") +
                        " cb=" + cb);

            uri = s_uribase + id;
            hr  = UnsafeNativeMethods.SessionNDMakeRequest(
                socketHandle, s_server, s_port, networkTimeout, verb, uri,
                exclusiveAccess, timeout, lockCookie,
                buf, cb, out results);

            Debug.Trace("SessionStateClientManagerMakeRequest", "MakeRequest returned: " +
                        "hr=" + hr +
                        " socket=" + (IntPtr)results.socket +
                        " httpstatus=" + results.httpStatus +
                        " timeout=" + results.timeout +
                        " contentlength=" + results.contentLength +
                        " uri=" + (IntPtr)results.content +
                        " lockCookie=" + results.lockCookie +
                        " lockDate=" + string.Format("{0:x}", results.lockDate) +
                        " lockAge=" + results.lockAge);

            if (conn != null)
            {
                if (results.socket == INVALID_SOCKET)
                {
                    conn.Detach();
                    conn = null;
                }
                else if (results.socket != socketHandle.Handle)
                {
                    // The original socket is no good.  We've got a new one.
                    // Pleae note that EnsureConnected has closed the bad
                    // one already.
                    conn._socketHandle = new HandleRef(this, results.socket);
                }
            }
            else if (results.socket != INVALID_SOCKET)
            {
                conn = new OutOfProcConnection(results.socket);
            }

            if (conn != null)
            {
                s_rpool.StoreResource(conn);
            }

            if (hr != 0)
            {
                throw new HttpException(
                          HttpRuntime.FormatResourceString(SR.Cant_make_session_request),
                          hr);
            }
        }