Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
nsServerSocket Class Reference

#include <nsServerSocket.h>

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

List of all members.

Public Member Functions

NS_DECL_ISUPPORTS virtual
NS_DECL_NSISERVERSOCKET void 
OnSocketReady (PRFileDesc *fd, PRInt16 outFlags)
virtual void OnSocketDetached (PRFileDesc *fd)
 nsServerSocket ()
void init (in long aPort, in boolean aLoopbackOnly, in long aBackLog)
 init
void initWithAddress ([const ] in PRNetAddrPtr aAddr, in long aBackLog)
 initWithAddress
void close ()
 close
void asyncListen (in nsIServerSocketListener aListener)
 asyncListen
PRNetAddr getAddress ()
 Returns the address to which this server socket is bound.

Public Attributes

nsresult mCondition
PRUint16 mPollFlags
PRUint16 mPollTimeout
readonly attribute long port
 Returns the port of this server socket.

Private Member Functions

virtual ~nsServerSocket ()
void OnMsgClose ()
void OnMsgAttach ()
nsresult TryAttach ()

Private Attributes

PRLockmLock
PRFileDescmFD
PRNetAddr mAddr
nsCOMPtr< nsIServerSocketListenermListener
PRBool mAttached

Detailed Description

Definition at line 46 of file nsServerSocket.h.


Constructor & Destructor Documentation

Definition at line 102 of file nsServerSocket.cpp.

  : mLock(nsnull)
  , mFD(nsnull)
  , mAttached(PR_FALSE)
{
  // we want to be able to access the STS directly, and it may not have been
  // constructed yet.  the STS constructor sets gSocketTransportService.
  if (!gSocketTransportService)
  {
    nsCOMPtr<nsISocketTransportService> sts =
        do_GetService(kSocketTransportServiceCID);
    NS_ASSERTION(sts, "no socket transport service");
  }
  // make sure the STS sticks around as long as we do
  NS_ADDREF(gSocketTransportService);
}

Here is the call graph for this function:

nsServerSocket::~nsServerSocket ( ) [private, virtual]

Definition at line 119 of file nsServerSocket.cpp.

{
  Close(); // just in case :)

  if (mLock)
    PR_DestroyLock(mLock);

  // release our reference to the STS
  nsSocketTransportService *serv = gSocketTransportService;
  NS_RELEASE(serv);
}

Here is the call graph for this function:


Member Function Documentation

asyncListen

This method puts the server socket in the listening state. It will asynchronously listen for and accept client connections. The listener will be notified once for each client connection that is accepted. The listener's onSocketAccepted method will be called on the same thread that called asyncListen (the calling thread must have a nsIEventTarget).

The listener will be passed a reference to an already connected socket transport (nsISocketTransport). See below for more details.

Parameters:
aListenerThe listener to be notified when client connections are accepted.
void nsIServerSocket::close ( ) [inherited]

close

This method closes a server socket. This does not affect already connected client sockets (i.e., the nsISocketTransport instances created from this server socket). This will cause the onStopListening event to asynchronously fire with a status of NS_BINDING_ABORTED.

Returns the address to which this server socket is bound.

Since a server socket may be bound to multiple network devices, this address may not necessarily be specific to a single network device. In the case of an IP socket, the IP address field would be zerod out to indicate a server socket bound to all network devices. Therefore, this method cannot be used to determine the IP address of the local system. See nsIDNSService::myHostName if this is what you need.

void nsIServerSocket::init ( in long  aPort,
in boolean  aLoopbackOnly,
in long  aBackLog 
) [inherited]

init

This method initializes a server socket.

Parameters:
aPortThe port of the server socket. Pass -1 to indicate no preference, and a port will be selected automatically.
aLoopbackOnlyIf true, the server socket will only respond to connections on the local loopback interface. Otherwise, it will accept connections from any interface. To specify a particular network interface, use initWithAddress.
aBackLogThe maximum length the queue of pending connections may grow to. This parameter may be silently limited by the operating system. Pass -1 to use the default value.
void nsIServerSocket::initWithAddress ( [const ] in PRNetAddrPtr  aAddr,
in long  aBackLog 
) [inherited]

initWithAddress

This method initializes a server socket, and binds it to a particular local address (and hence a particular local network interface).

Parameters:
aAddrThe address to which this server socket should be bound.
aBackLogThe maximum length the queue of pending connections may grow to. This parameter may be silently limited by the operating system. Pass -1 to use the default value.

Definition at line 149 of file nsServerSocket.cpp.

{
  LOG(("nsServerSocket::OnMsgAttach [this=%p]\n", this));

  if (NS_FAILED(mCondition))
    return;

  mCondition = TryAttach();
  
  // if we hit an error while trying to attach then bail...
  if (NS_FAILED(mCondition))
  {
    NS_ASSERTION(!mAttached, "should not be attached already");
    OnSocketDetached(mFD);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 132 of file nsServerSocket.cpp.

{
  LOG(("nsServerSocket::OnMsgClose [this=%p]\n", this));

  if (NS_FAILED(mCondition))
    return;

  // tear down socket.  this signals the STS to detach our socket handler.
  mCondition = NS_BINDING_ABORTED;

  // if we are attached, then we'll close the socket in our OnSocketDetached.
  // otherwise, call OnSocketDetached from here.
  if (!mAttached)
    OnSocketDetached(mFD);
}

Here is the call graph for this function:

Implements nsASocketHandler.

Definition at line 257 of file nsServerSocket.cpp.

{
  // force a failure condition if none set; maybe the STS is shutting down :-/
  if (NS_SUCCEEDED(mCondition))
    mCondition = NS_ERROR_ABORT;

  if (mFD)
  {
    NS_ASSERTION(mFD == fd, "wrong file descriptor");
    PR_Close(mFD);
    mFD = nsnull;
  }

  if (mListener)
  {
    mListener->OnStopListening(this, mCondition);

    // need to atomically clear mListener.  see our Close() method.
    nsAutoLock lock(mLock);
    mListener = nsnull;
  }
}

Here is the caller graph for this function:

void nsServerSocket::OnSocketReady ( PRFileDesc fd,
PRInt16  outFlags 
) [virtual]

Implements nsASocketHandler.

Definition at line 218 of file nsServerSocket.cpp.

{
  NS_ASSERTION(NS_SUCCEEDED(mCondition), "oops");
  NS_ASSERTION(mFD == fd, "wrong file descriptor");
  NS_ASSERTION(outFlags != -1, "unexpected timeout condition reached");

  if (outFlags & (PR_POLL_ERR | PR_POLL_HUP | PR_POLL_NVAL))
  {
    NS_WARNING("error polling on listening socket");
    mCondition = NS_ERROR_UNEXPECTED;
    return;
  }

  PRFileDesc *clientFD;
  PRNetAddr clientAddr;

  clientFD = PR_Accept(mFD, &clientAddr, PR_INTERVAL_NO_WAIT);
  if (!clientFD)
  {
    NS_WARNING("PR_Accept failed");
    mCondition = NS_ERROR_UNEXPECTED;
  }
  else
  {
    nsRefPtr<nsSocketTransport> trans = new nsSocketTransport;
    if (!trans)
      mCondition = NS_ERROR_OUT_OF_MEMORY;
    else
    {
      nsresult rv = trans->InitWithConnectedSocket(clientFD, &clientAddr);
      if (NS_FAILED(rv))
        mCondition = rv;
      else
        mListener->OnSocketAccepted(this, trans);
    }
  }
}

Definition at line 167 of file nsServerSocket.cpp.

{
  nsresult rv;

  //
  // find out if it is going to be ok to attach another socket to the STS.
  // if not then we have to wait for the STS to tell us that it is ok.
  // the notification is asynchronous, which means that when we could be
  // in a race to call AttachSocket once notified.  for this reason, when
  // we get notified, we just re-enter this function.  as a result, we are
  // sure to ask again before calling AttachSocket.  in this way we deal
  // with the race condition.  though it isn't the most elegant solution,
  // it is far simpler than trying to build a system that would guarantee
  // FIFO ordering (which wouldn't even be that valuable IMO).  see bug
  // 194402 for more info.
  //
  if (!gSocketTransportService->CanAttachSocket())
  {
    PLEvent *event = new nsServerSocketEvent(this, &nsServerSocket::OnMsgAttach);
    if (!event)
      return NS_ERROR_OUT_OF_MEMORY;

    nsresult rv = gSocketTransportService->NotifyWhenCanAttachSocket(event);
    if (NS_FAILED(rv))
    {
      PL_DestroyEvent(event);
      return rv;
    }
  }

  //
  // ok, we can now attach our socket to the STS for polling
  //
  rv = gSocketTransportService->AttachSocket(mFD, this);
  if (NS_FAILED(rv))
    return rv;

  mAttached = PR_TRUE;

  //
  // now, configure our poll flags for listening...
  //
  mPollFlags = (PR_POLL_READ | PR_POLL_EXCEPT);
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 71 of file nsServerSocket.h.

Definition at line 73 of file nsServerSocket.h.

Definition at line 84 of file nsSocketTransportService2.h.

Definition at line 70 of file nsServerSocket.h.

Definition at line 72 of file nsServerSocket.h.

Definition at line 69 of file nsServerSocket.h.

Definition at line 91 of file nsSocketTransportService2.h.

Definition at line 102 of file nsSocketTransportService2.h.

readonly attribute long nsIServerSocket::port [inherited]

Returns the port of this server socket.

Definition at line 119 of file nsIServerSocket.idl.


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