Back to index

lightning-sunbird  0.9+nobinonly
Classes | Public Member Functions | Public Attributes | Protected Member Functions | Private Member Functions | Private Attributes
nsSocketTransportService Class Reference

#include <nsSocketTransportService2.h>

Inheritance diagram for nsSocketTransportService:
Inheritance graph
[legend]
Collaboration diagram for nsSocketTransportService:
Collaboration graph
[legend]

List of all members.

Classes

struct  SocketContext

Public Member Functions

NS_DECL_ISUPPORTS
NS_DECL_NSPISOCKETTRANSPORTSERVICE
NS_DECL_NSISOCKETTRANSPORTSERVICE
NS_DECL_NSIEVENTTARGET
NS_DECL_NSIRUNNABLE 
nsSocketTransportService ()
PRBool CanAttachSocket ()
nsresult NotifyWhenCanAttachSocket (PLEvent *)
nsresult AttachSocket (PRFileDesc *fd, nsASocketHandler *)
void init ()
 init/shutdown routines.
void shutdown ()
nsISocketTransport createTransport ([array, size_is(aTypeCount)] in string aSocketTypes, in unsigned long aTypeCount, in AUTF8String aHost, in long aPort, in nsIProxyInfo aProxyInfo)
 Creates a transport for a specified host and port.
void postEvent (in PLEventPtr aEvent)
 Method for posting an asynchronous event to the event target.
boolean isOnCurrentThread ()
 This method returns true if the event target is the current thread.
NS_IMETHOD Run ()=0
 Defines an entry point for a newly created thread.
void run ()

Public Attributes

attribute boolean autodialEnabled
 controls whether or not the socket transport service should poke the autodialer on connection failure.

Protected Member Functions

virtual ~nsSocketTransportService ()

Private Member Functions

nsresult DetachSocket (SocketContext *)
nsresult AddToIdleList (SocketContext *)
nsresult AddToPollList (SocketContext *)
void RemoveFromIdleList (SocketContext *)
void RemoveFromPollList (SocketContext *)
void MoveToIdleList (SocketContext *sock)
void MoveToPollList (SocketContext *sock)
PRBool ServiceEventQ ()
PRIntervalTime PollTimeout ()
PRInt32 Poll (PRUint32 *interval)

Private Attributes

PRBool mInitialized
nsIThreadmThread
PRFileDescmThreadEvent
PRBool mAutodialEnabled
PRBool mShuttingDown
PRCList mEventQ
PRLockmEventQLock
SocketContext mActiveList [NS_SOCKET_MAX_COUNT]
SocketContext mIdleList [NS_SOCKET_MAX_COUNT]
PRUint32 mActiveCount
PRUint32 mIdleCount
PRPollDesc mPollList [NS_SOCKET_MAX_COUNT+1]
PRCList mPendingSocketQ

Detailed Description

Definition at line 126 of file nsSocketTransportService2.h.


Class Documentation

struct nsSocketTransportService::SocketContext

Definition at line 214 of file nsSocketTransportService2.h.

Collaboration diagram for nsSocketTransportService::SocketContext:
Class Members
PRUint16 mElapsedTime
PRFileDesc * mFD
nsASocketHandler * mHandler

Constructor & Destructor Documentation

Definition at line 78 of file nsSocketTransportService2.cpp.

    : mInitialized(PR_FALSE)
    , mThread(nsnull)
    , mThreadEvent(nsnull)
    , mAutodialEnabled(PR_FALSE)
    , mShuttingDown(PR_FALSE)
    , mEventQLock(PR_NewLock())
    , mActiveCount(0)
    , mIdleCount(0)
{
#if defined(PR_LOGGING)
    gSocketTransportLog = PR_NewLogModule("nsSocketTransport");
#endif

    NS_ASSERTION(nsIThread::IsMainThread(), "wrong thread");

    PR_INIT_CLIST(&mEventQ);
    PR_INIT_CLIST(&mPendingSocketQ);

    NS_ASSERTION(!gSocketTransportService, "must not instantiate twice");
    gSocketTransportService = this;
}

Here is the call graph for this function:

Definition at line 101 of file nsSocketTransportService2.cpp.

{
    NS_ASSERTION(nsIThread::IsMainThread(), "wrong thread");
    NS_ASSERTION(!mInitialized, "not shutdown properly");

    PR_DestroyLock(mEventQLock);
    
    if (mThreadEvent)
        PR_DestroyPollableEvent(mThreadEvent);

    gSocketTransportService = nsnull;
}

Here is the call graph for this function:


Member Function Documentation

Definition at line 261 of file nsSocketTransportService2.cpp.

{
    LOG(("nsSocketTransportService::AddToIdleList [handler=%x]\n", sock->mHandler));

    if (mIdleCount == NS_SOCKET_MAX_COUNT) {
        NS_ERROR("too many idle sockets");
        return NS_ERROR_UNEXPECTED;
    }

    mIdleList[mIdleCount] = *sock;
    mIdleCount++;

    LOG(("  active=%u idle=%u\n", mActiveCount, mIdleCount));
    return NS_OK;
}

Here is the caller graph for this function:

Definition at line 221 of file nsSocketTransportService2.cpp.

{
    LOG(("nsSocketTransportService::AddToPollList [handler=%x]\n", sock->mHandler));

    if (mActiveCount == NS_SOCKET_MAX_COUNT) {
        NS_ERROR("too many active sockets");
        return NS_ERROR_UNEXPECTED;
    }

    mActiveList[mActiveCount] = *sock;
    mActiveCount++;

    mPollList[mActiveCount].fd = sock->mFD;
    mPollList[mActiveCount].in_flags = sock->mHandler->mPollFlags;
    mPollList[mActiveCount].out_flags = 0;

    LOG(("  active=%u idle=%u\n", mActiveCount, mIdleCount));
    return NS_OK;
}

Here is the caller graph for this function:

Definition at line 170 of file nsSocketTransportService2.cpp.

{
    LOG(("nsSocketTransportService::AttachSocket [handler=%x]\n", handler));

    NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");

    SocketContext sock;
    sock.mFD = fd;
    sock.mHandler = handler;
    sock.mElapsedTime = 0;

    nsresult rv = AddToIdleList(&sock);
    if (NS_SUCCEEDED(rv))
        NS_ADDREF(handler);
    return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 146 of file nsSocketTransportService2.h.

Here is the caller graph for this function:

nsISocketTransport nsISocketTransportService::createTransport ( [array, size_is(aTypeCount)] in string  aSocketTypes,
in unsigned long  aTypeCount,
in AUTF8String  aHost,
in long  aPort,
in nsIProxyInfo  aProxyInfo 
) [inherited]

Creates a transport for a specified host and port.

Parameters:
aSocketTypesarray of socket type strings. null if using default socket type.
aTypeCountspecifies length of aSocketTypes.
aHostspecifies the target hostname or IP address literal of the peer for this socket.
aPortspecifies the target port of the peer for this socket.
aProxyInfospecifies the transport-layer proxy type to use. null if no proxy. used for communicating information about proxies like SOCKS (which are transparent to upper protocols).
See also:
nsIProxiedProtocolHandler
nsIProtocolProxyService::GetProxyInfo

Definition at line 188 of file nsSocketTransportService2.cpp.

{
    LOG(("nsSocketTransportService::DetachSocket [handler=%x]\n", sock->mHandler));

    // inform the handler that this socket is going away
    sock->mHandler->OnSocketDetached(sock->mFD);

    // cleanup
    sock->mFD = nsnull;
    NS_RELEASE(sock->mHandler);

    // find out what list this is on.
    PRUint32 index = sock - mActiveList;
    if (index < NS_SOCKET_MAX_COUNT)
        RemoveFromPollList(sock);
    else
        RemoveFromIdleList(sock);

    // NOTE: sock is now an invalid pointer
    
    //
    // notify the first element on the pending socket queue...
    //
    if (!PR_CLIST_IS_EMPTY(&mPendingSocketQ)) {
        // move event from pending queue to event queue
        PLEvent *event = PLEVENT_FROM_LINK(PR_LIST_HEAD(&mPendingSocketQ));
        PR_REMOVE_AND_INIT_LINK(&event->link);
        PostEvent(event);
    }
    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

init/shutdown routines.

This method returns true if the event target is the current thread.

Definition at line 293 of file nsSocketTransportService2.cpp.

{
    nsresult rv = AddToIdleList(sock);
    if (NS_FAILED(rv))
        DetachSocket(sock);
    else
        RemoveFromPollList(sock);
}

Here is the call graph for this function:

Definition at line 303 of file nsSocketTransportService2.cpp.

{
    nsresult rv = AddToPollList(sock);
    if (NS_FAILED(rv))
        DetachSocket(sock);
    else
        RemoveFromIdleList(sock);
}

Here is the call graph for this function:

Definition at line 154 of file nsSocketTransportService2.cpp.

{
    LOG(("nsSocketTransportService::NotifyWhenCanAttachSocket\n"));

    NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");

    if (CanAttachSocket()) {
        NS_WARNING("should have called CanAttachSocket");
        return PostEvent(event);
    }

    PR_APPEND_LINK(&event->link, &mPendingSocketQ);
    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PRInt32 nsSocketTransportService::Poll ( PRUint32 interval) [private]

Definition at line 335 of file nsSocketTransportService2.cpp.

{
    PRPollDesc *pollList;
    PRUint32 pollCount;
    PRIntervalTime pollTimeout;

    if (mPollList[0].fd) {
        mPollList[0].out_flags = 0;
        pollList = mPollList;
        pollCount = mActiveCount + 1;
        pollTimeout = PollTimeout();
    }
    else {
        // no pollable event, so busy wait...
        pollCount = mActiveCount;
        if (pollCount)
            pollList = &mPollList[1];
        else
            pollList = nsnull;
        pollTimeout = PR_MillisecondsToInterval(25);
    }

    PRIntervalTime ts = PR_IntervalNow();

    LOG(("    timeout = %i milliseconds\n",
         PR_IntervalToMilliseconds(pollTimeout)));
    PRInt32 rv = PR_Poll(pollList, pollCount, pollTimeout);

    PRIntervalTime passedInterval = PR_IntervalNow() - ts;

    LOG(("    ...returned after %i milliseconds\n",
         PR_IntervalToMilliseconds(passedInterval))); 

    *interval = PR_IntervalToSeconds(passedInterval);
    return rv;
}

Here is the call graph for this function:

Definition at line 313 of file nsSocketTransportService2.cpp.

{
    if (mActiveCount == 0)
        return NS_SOCKET_POLL_TIMEOUT;

    // compute minimum time before any socket timeout expires.
    PRUint32 minR = PR_UINT16_MAX;
    for (PRUint32 i=0; i<mActiveCount; ++i) {
        const SocketContext &s = mActiveList[i];
        // mPollTimeout could be less than mElapsedTime if setTimeout
        // was called with a value smaller than mElapsedTime.
        PRUint32 r = (s.mElapsedTime < s.mHandler->mPollTimeout)
          ? s.mHandler->mPollTimeout - s.mElapsedTime
          : 0;
        if (r < minR)
            minR = r;
    }
    LOG(("poll timeout: %lu\n", minR));
    return PR_SecondsToInterval(minR);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void nsIEventTarget::postEvent ( in PLEventPtr  aEvent) [inherited]

Method for posting an asynchronous event to the event target.

If this method succeeds, then the event will be dispatched on the target thread.

Parameters:
aEventThe event to dispatched.

Definition at line 278 of file nsSocketTransportService2.cpp.

{
    LOG(("nsSocketTransportService::RemoveFromIdleList [handler=%x]\n", sock->mHandler));

    PRUint32 index = sock - &mIdleList[0];
    NS_ASSERTION(index < NS_SOCKET_MAX_COUNT, "invalid index");

    if (index != mIdleCount-1)
        mIdleList[index] = mIdleList[mIdleCount-1];
    mIdleCount--;

    LOG(("  active=%u idle=%u\n", mActiveCount, mIdleCount));
}

Here is the caller graph for this function:

Definition at line 242 of file nsSocketTransportService2.cpp.

{
    LOG(("nsSocketTransportService::RemoveFromPollList [handler=%x]\n", sock->mHandler));

    PRUint32 index = sock - mActiveList;
    NS_ASSERTION(index < NS_SOCKET_MAX_COUNT, "invalid index");

    LOG(("  index=%u mActiveCount=%u\n", index, mActiveCount));

    if (index != mActiveCount-1) {
        mActiveList[index] = mActiveList[mActiveCount-1];
        mPollList[index+1] = mPollList[mActiveCount];
    }
    mActiveCount--;

    LOG(("  active=%u idle=%u\n", mActiveCount, mIdleCount));
}

Here is the caller graph for this function:

void nsIRunnable::run ( ) [inherited]
NS_IMETHOD nsIRunnable::Run ( ) [pure virtual, inherited]

Definition at line 373 of file nsSocketTransportService2.cpp.

{
    PRBool keepGoing;

    // grab the event queue
    PRCList eq;
    PR_INIT_CLIST(&eq);
    {
        nsAutoLock lock(mEventQLock);

        MoveCList(mEventQ, eq);

        // check to see if we're supposed to shutdown
        keepGoing = mInitialized;
    }
    // service the event queue
    PLEvent *event;
    while (!PR_CLIST_IS_EMPTY(&eq)) {
        event = PLEVENT_FROM_LINK(PR_LIST_HEAD(&eq));
        PR_REMOVE_AND_INIT_LINK(&event->link);

        PL_HandleEvent(event);
    }
    return keepGoing;
}

Here is the call graph for this function:


Member Data Documentation

controls whether or not the socket transport service should poke the autodialer on connection failure.

Definition at line 58 of file nsPISocketTransportService.idl.

Definition at line 224 of file nsSocketTransportService2.h.

Definition at line 221 of file nsSocketTransportService2.h.

Definition at line 188 of file nsSocketTransportService2.h.

Definition at line 200 of file nsSocketTransportService2.h.

Definition at line 201 of file nsSocketTransportService2.h.

Definition at line 225 of file nsSocketTransportService2.h.

Definition at line 222 of file nsSocketTransportService2.h.

Definition at line 176 of file nsSocketTransportService2.h.

Definition at line 256 of file nsSocketTransportService2.h.

Definition at line 245 of file nsSocketTransportService2.h.

Definition at line 194 of file nsSocketTransportService2.h.

Definition at line 177 of file nsSocketTransportService2.h.

Definition at line 178 of file nsSocketTransportService2.h.


The documentation for this class was generated from the following files: