Overview   Project   Class   Tree   Deprecated   Index 
0 A.D.
FRAMES    NO FRAMES
SUMMARY:  INNER | FIELD | CONSTR | METHOD

network
Class CSocketBase

   in SocketBase.h
   in SocketBase.cpp
Direct Known Subclasses:
CServerSocket, CStreamSocket

class CSocketBase

Contains the basic socket I/O abstraction and event callback methods. A CSocketBase can only be instantiated as a subclass, none of the functions are meant to exist as anything other than helper functions for socket classes Any CSocket subclass that can be Accept:ed by a CServerSocket should provide a constructor that takes a CSocketInternal pointer, and hands it to the base class constructor.


Inner Classes, Typedefs, and Enums
enum CSocketBase::Ops
          These values are bitwise or-ed to produce op masks
 
Field Summary
private PS_RESULT m_Error
          
private bool m_NonBlocking
          
private CSocketInternal* m_pInternal
          
private ESocketProtocol m_Proto
          
private ESocketState m_State
          
 
Constructor Summary
protected  CSocketBase( CSocketInternal* pInt )
          Initialize a CSocketBase from a CSocketInternal pointer.
  CSocketBase()
          Constructs a CSocketBase.
protected  virtual ~CSocketBase()
          
 
Method Summary
private static void AbortWaitLoop()
          Abort the call to RunWaitLoop(), if one is currently running.
 CSocketInternal* Accept()
          Accept the next incoming connection.
 PS_RESULT Bind( const CSocketAddress& address )
          Bind the socket to the specified address and start listening for incoming connections.
 void Close()
          Close the socket.
 PS_RESULT Connect( const CSocketAddress& addr )
          Connect the socket to the specified address.
private bool ConnectError( CSocketBase* pSocket )
          
 void Destroy()
          Destroy the OS socket.
 inline PS_RESULT GetErrorState() const
          Return the error state of the socket.
protected uint GetOpMask()
          Get the op mask for the socket.
 inline ESocketProtocol GetProtocol() const
          Returns the protocol set by Initialize.
 const CSocketAddress& GetRemoteAddress()
          Get the address of the remote end to which the socket is connected.
 inline ESocketState GetState() const
          Return the connection state of the socket.
 PS_RESULT Initialize( ESocketProtocol proto = IPv4 )
          Create the OS socket for the specified protocol type.
 inline bool IsNonBlocking() const
          Return the current non-blocking state of the socket.
 virtual void OnClose( PS_RESULT errorCode )= 0
          The socket has been closed.
 virtual void OnRead()= 0
          Called by the Network Thread when data is available for reading.
 virtual void OnWrite()= 0
          Called by the Network Thread when data can be written to the socket.
 PS_RESULT PreAccept( CSocketAddress& addr )
          Store the address of the next incoming connection in the SocketAddress pointed to by addr.
 PS_RESULT Read( void* buf, uint len, uint* bytesRead )
          Attempt to read data from the socket.
 void Reject()
          Reject the next incoming connection.
private static void RunWaitLoop()
          Loop forever, waiting for events and calling the callbacks on sockets, according to their Op mask.
private static void SendWaitLoopAbort()
          Tell the running wait loop to abort.
private void SendWaitLoopUpdate()
          
 void SetNonBlocking( bool nonBlocking = true )
          Set or reset non-blocking operation.
protected void SetOpMask( uint ops )
          Set the op mask for the socket, specifying which callbacks should be called by the WaitLoop.
 void SetTcpNoDelay( bool tcpNoDelay = true )
          Disable Nagle's algorithm (enable no-delay working mode)
 static void Shutdown()
          Forcibly shuts down the network wait loop.
private static void SocketReadable( CSocketBase* )
          
private static void SocketWritable( CSocketBase* )
          
 PS_RESULT Write( void* buf, uint len, uint* bytesWritten )
          Attempt to write data to the socket.
 

Field Detail

m_Error

private PS_RESULT m_Error;

m_NonBlocking

private bool m_NonBlocking;

m_pInternal

private CSocketInternal* m_pInternal;

m_Proto

private ESocketProtocol m_Proto;

m_State

private ESocketState m_State;


Constructor Detail

CSocketBase

protected CSocketBase( CSocketInternal* pInt );
Initialize a CSocketBase from a CSocketInternal pointer. Use in OnAccept callbacks to create an object of your subclass. This constructor should be overloaded by any subclass that may be Accept:ed.

CSocketBase

public CSocketBase();
Constructs a CSocketBase. The OS socket object is not created by the constructor, but by the protected Initialize method, which is called by Connect and Bind.
See Also:
Connect, Bind

~CSocketBase

protected virtual ~CSocketBase();


Method Detail

AbortWaitLoop

private static void AbortWaitLoop();
Abort the call to RunWaitLoop(), if one is currently running.

Accept

public CSocketInternal* Accept();
Accept the next incoming connection. You must construct a suitable CSocketBase subclass using the passed CSocketInternal. May only be called after a successful PreAccept call

Bind

public PS_RESULT Bind( const CSocketAddress& address );
Bind the socket to the specified address and start listening for incoming connections. You must initialize the socket for the correct SocketProtocol before calling Bind.
Parameters:
addr - The address to bind to
See Also:
SocketAddress::SocketAddress(int,SocketProtocol)

Close

public void Close();
Close the socket. No more data can be sent over the socket, but any data pending from the remote host will still be received, and the OnRead callback called (if the socket's op mask has the READ bit set). Note that the socket isn't actually closed until the remote end calls Close on the corresponding remote socket, upon which the OnClose callback is called with a status code of PS_OK.

Connect

public PS_RESULT Connect( const CSocketAddress& addr );
Connect the socket to the specified address. The socket must be initialized for the protocol of the address.
Parameters:
addr - The address to connect to
See Also:
SocketAddress::Resolve

ConnectError

private bool ConnectError( CSocketBase* pSocket );

Destroy

public void Destroy();
Destroy the OS socket. If the socket is not cleanly closed before, it will be forcefully closed by calling this method.

GetErrorState

public inline PS_RESULT GetErrorState() const;
Return the error state of the socket. This will be the same value that was returned by the IO function that failed.
See Also:
GetState()

GetOpMask

protected uint GetOpMask();
Get the op mask for the socket.

GetProtocol

public inline ESocketProtocol GetProtocol() const;
Returns the protocol set by Initialize. All SocketAddresses used with the socket must have the same SocketProtocol

GetRemoteAddress

public const CSocketAddress& GetRemoteAddress();
Get the address of the remote end to which the socket is connected.
Returns:
A reference to the socket address

GetState

public inline ESocketState GetState() const;
Return the connection state of the socket. If the connection status is "unconnected", use GetError() to see if it was disconnected due to an error, or cleanly closed.
See Also:
SocketState, GetError()

Initialize

public PS_RESULT Initialize( ESocketProtocol proto = IPv4 );
Create the OS socket for the specified protocol type.

IsNonBlocking

public inline bool IsNonBlocking() const;
Return the current non-blocking state of the socket.
See Also:
SetNonBlocking(bool)

OnClose

public virtual void OnClose( PS_RESULT errorCode )= 0;
The socket has been closed. It is not certain that the error code provides meaningful diagnostics. CONNECTION_BROKEN is the generic catch- all for erroneous closures, PS_OK for clean closures.
Parameters:
errorCode - A result code describing the reason why the socket was closed

OnRead

public virtual void OnRead()= 0;
Called by the Network Thread when data is available for reading. Use SetOpMask with the READ bit set to enable calling of this function. For server sockets, "data is available for reading" means "incoming connections are pending".

OnWrite

public virtual void OnWrite()= 0;
Called by the Network Thread when data can be written to the socket. Will only be called when the WRITE bit is set in the Op Mask of the socket.

PreAccept

public PS_RESULT PreAccept( CSocketAddress& addr );
Store the address of the next incoming connection in the SocketAddress pointed to by addr. You must then choose whether to accept or reject the connection by calling Accept or Reject
Parameters:
addr - A pointer to a SocketAddress
Returns:
PS_OK or an error code
See Also:
Accept(SocketAddress&), Reject()

Read

public PS_RESULT Read( void* buf, uint len, uint* bytesRead );
Attempt to read data from the socket. Any data available without blocking will be returned. Note that a successful return does not mean that the whole buffer was filled.
Parameters:
buf - A pointer to the buffer where the data should be written
len - The amount of data that should be read.
bytesRead - The number of bytes read will be stored in the variable pointed to by bytesRead

Reject

public void Reject();
Reject the next incoming connection. May only be called after a successful PreAccept call

RunWaitLoop

private static void RunWaitLoop();
Loop forever, waiting for events and calling the callbacks on sockets, according to their Op mask. This loop may be aborted by calling AbortWaitLoop. The global lock must be held when calling this function, and will be held upon return from it.

SendWaitLoopAbort

private static void SendWaitLoopAbort();
Tell the running wait loop to abort. This is the platform-dependent implementation of AbortWaitLoop()

SendWaitLoopUpdate

private void SendWaitLoopUpdate();

SetNonBlocking

public void SetNonBlocking( bool nonBlocking = true );
Set or reset non-blocking operation. When non-blocking, all socket operations will return immediately, having done none or parts of the operation. The default state for a socket is non-blocking
See Also:
CSocketBase::Read, CSocketBase::Write, CSocketBase::Connect

SetOpMask

protected void SetOpMask( uint ops );
Set the op mask for the socket, specifying which callbacks should be called by the WaitLoop. The initial op mask is zero, which means that this method must be called explicitly for any callbacks to be called. Note that before the call to BeginConnect or Bind, any call to this method is a no-op. It is safe to call this function while a RunWaitLoop is running. The wait loop guarantees that the callbacks specified in ops will be called when appropriate, but does not make the opposite guarantee for unset bits; i.e. any callback may be called even with a zero op mask.

SetTcpNoDelay

public void SetTcpNoDelay( bool tcpNoDelay = true );
Disable Nagle's algorithm (enable no-delay working mode)

Shutdown

public static void Shutdown();
Forcibly shuts down the network wait loop. This should happen automatically as soon as all sockets are closed.

SocketReadable

private static void SocketReadable( CSocketBase* );

SocketWritable

private static void SocketWritable( CSocketBase* );

Write

public PS_RESULT Write( void* buf, uint len, uint* bytesWritten );
Attempt to write data to the socket. All data that can be sent without blocking will be buffered.
Parameters:
buf - A pointer to the data that should be written
len - The length of the buffer.
bytesWritten - The number of bytes written will be stored in the variable pointed to by bytesWritten

 Overview   Project   Class   Tree   Deprecated   Index 
0 A.D.
Generated on September 04, 2007 at 18:13
CppDoc v2.4.0
FRAMES    NO FRAMES
SUMMARY:  INNER | FIELD | CONSTR | METHOD