YDLIDAR SDK  V1.3.6
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ydlidar::CSimpleSocket Class Reference

#include <SimpleSocket.h>

Inheritance diagram for ydlidar::CSimpleSocket:
Inheritance graph
[legend]
Collaboration diagram for ydlidar::CSimpleSocket:
Collaboration graph
[legend]

Public Types

enum  CShutdownMode { Receives = SHUT_RD, Sends = SHUT_WR, Both = SHUT_RDWR }
 Defines the three possible states for shuting down a socket. More...
 
enum  CSocketType {
  SocketTypeInvalid = 0, SocketTypeTcp, SocketTypeUdp, SocketTypeTcp6,
  SocketTypeUdp6, SocketTypeRaw
}
 Defines the socket types defined by CSimpleSocket class. More...
 
enum  CSocketError {
  SocketError = -1, SocketSuccess = 0, SocketInvalidSocket, SocketInvalidAddress,
  SocketInvalidPort, SocketConnectionRefused, SocketTimedout, SocketEwouldblock,
  SocketNotconnected, SocketEinprogress, SocketInterrupted, SocketConnectionAborted,
  SocketProtocolError, SocketFirewallError, SocketInvalidSocketBuffer, SocketConnectionReset,
  SocketAddressInUse, SocketInvalidPointer, SocketEunknown
}
 Defines all error codes handled by the CSimpleSocket class. More...
 

Public Member Functions

 CSimpleSocket (CSocketType type=SocketTypeTcp)
 
 CSimpleSocket (CSimpleSocket &socket)
 
virtual bool Initialize (void)
 
virtual bool Close (void)
 
virtual bool Shutdown (CShutdownMode nShutdown)
 
virtual bool Select (void)
 
virtual bool Select (int32_t nTimeoutSec, int32_t nTimeoutUSec)
 
virtual int WaitForData (size_t data_count, uint32_t timeout, size_t *returned_size)
 
virtual bool IsSocketValid (void)
 
void TranslateSocketError (void)
 
const char * DescribeError ()
 
virtual int32_t Receive (int32_t nMaxBytes=1, uint8_t *pBuffer=0)
 
virtual int32_t Send (const uint8_t *pBuf, size_t bytesToSend)
 
virtual int32_t Send (const struct iovec *sendVector, int32_t nNumItems)
 
virtual int32_t SendFile (int32_t nOutFd, int32_t nInFd, off_t *pOffset, int32_t nCount)
 
bool IsNonblocking (void)
 
bool SetBlocking (void)
 
bool SetNonblocking (void)
 
uint8_t * GetData (void)
 
int32_t GetBytesReceived (void)
 
int32_t GetBytesSent (void)
 
bool SetOptionLinger (bool bEnable, uint16_t nTime)
 
bool SetOptionReuseAddr ()
 
int32_t GetConnectTimeoutSec (void)
 
int32_t GetConnectTimeoutUSec (void)
 
void SetConnectTimeout (int32_t nConnectTimeoutSec, int32_t nConnectTimeoutUsec=0)
 
int32_t GetReceiveTimeoutSec (void)
 
int32_t GetReceiveTimeoutUSec (void)
 
bool SetReceiveTimeout (int32_t nRecvTimeoutSec, int32_t nRecvTimeoutUsec=0)
 
bool SetMulticast (bool bEnable, uint8_t multicastTTL=1)
 
bool GetMulticast ()
 
bool BindInterface (const char *pInterface)
 
int32_t GetSendTimeoutSec (void)
 
int32_t GetSendTimeoutUSec (void)
 
bool SetSendTimeout (int32_t nSendTimeoutSec, int32_t nSendTimeoutUsec=0)
 
CSocketError GetSocketError (void)
 
uint32_t GetTotalTimeMs ()
 
uint32_t GetTotalTimeUsec ()
 
int GetSocketDscp (void)
 
bool SetSocketDscp (int nDscp)
 
SOCKET GetSocketDescriptor ()
 
CSocketType GetSocketType ()
 
void SetSocketType (const CSocketType &type)
 set socket descriptor
 
const char * GetClientAddr ()
 
uint16_t GetClientPort ()
 
const char * GetServerAddr ()
 
uint16_t GetServerPort ()
 
uint32_t GetReceiveWindowSize ()
 
uint32_t GetSendWindowSize ()
 
uint32_t SetReceiveWindowSize (uint32_t nWindowSize)
 
uint32_t SetSendWindowSize (uint32_t nWindowSize)
 
bool DisableNagleAlgoritm ()
 
bool EnableNagleAlgoritm ()
 
virtual bool Open (const char *pAddr, uint16_t nPort)
 
virtual bool bindport (const char *, uint32_t)
 
virtual bool open ()
 
virtual bool isOpen ()
 
virtual void closePort ()
 
virtual void flush ()
 
virtual int waitfordata (size_t data_count, uint32_t timeout=-1, size_t *returned_size=NULL)
 
virtual size_t writeData (const uint8_t *data, size_t size)
 
virtual size_t readData (uint8_t *data, size_t size)
 
- Public Member Functions inherited from ChannelDevice
virtual bool setDTR (bool level=true)
 
virtual int getByteTime ()
 
virtual void ReleasePort ()
 

Static Public Member Functions

static const char * DescribeError (CSocketError err)
 

Protected Member Functions

void SetSocketError (CSimpleSocket::CSocketError error)
 
void SetSocketHandle (SOCKET socket)
 
bool Flush ()
 

Protected Attributes

SOCKET m_socket
 
CSocketError m_socketErrno
 socket handle
 
uint8_t * m_pBuffer
 number of last error
 
int32_t m_nBufferSize
 internal send/receive buffer
 
int32_t m_nSocketDomain
 size of internal send/receive buffer
 
CSocketType m_nSocketType
 socket type PF_INET, PF_INET6
 
int32_t m_nBytesReceived
 socket type - UDP, TCP or RAW
 
int32_t m_nBytesSent
 number of bytes received
 
uint32_t m_nFlags
 number of bytes sent
 
bool m_bIsBlocking
 socket flags
 
bool m_bIsMulticast
 is socket blocking
 
struct timeval m_stConnectTimeout
 is the UDP socket multicast;
 
struct timeval m_stRecvTimeout
 connection timeout
 
struct timeval m_stSendTimeout
 receive timeout
 
struct sockaddr_in m_stServerSockaddr
 send timeout
 
struct sockaddr_in m_stClientSockaddr
 server address
 
struct sockaddr_in m_stMulticastGroup
 client address
 
struct linger m_stLinger
 multicast group to bind to
 
CStatTimer m_timer
 linger flag
 
fd_set m_writeFds
 internal statistics.
 
fd_set m_readFds
 write file descriptor set
 
fd_set m_errorFds
 read file descriptor set
 
std::string m_addr
 error file descriptor set
 
uint32_t m_port
 
bool m_open
 

Detailed Description

Provides a platform independent class to for socket development. This class is designed to abstract socket communication development in a platform independent manner.

Member Enumeration Documentation

Defines the three possible states for shuting down a socket.

Enumerator
Receives 

Shutdown passive socket.

Sends 

Shutdown active socket.

Both 

Shutdown both active and passive sockets.

Defines all error codes handled by the CSimpleSocket class.

Enumerator
SocketError 

Generic socket error translates to error below.

SocketSuccess 

No socket error.

SocketInvalidSocket 

Invalid socket handle.

SocketInvalidAddress 

Invalid destination address specified.

SocketInvalidPort 

Invalid destination port specified.

SocketConnectionRefused 

No server is listening at remote address.

SocketTimedout 

Timed out while attempting operation.

SocketEwouldblock 

Operation would block if socket were blocking.

SocketNotconnected 

Currently not connected.

SocketEinprogress 

Socket is non-blocking and the connection cannot be completed immediately.

SocketInterrupted 

Call was interrupted by a signal that was caught before a valid connection arrived.

SocketConnectionAborted 

The connection has been aborted.

SocketProtocolError 

Invalid protocol for operation.

SocketFirewallError 

Firewall rules forbid connection.

SocketInvalidSocketBuffer 

The receive buffer point outside the process's address space.

SocketConnectionReset 

Connection was forcibly closed by the remote host.

SocketAddressInUse 

Address already in use.

SocketInvalidPointer 

Pointer type supplied as argument is invalid.

SocketEunknown 

Unknown error please report to mark@.nosp@m.carr.nosp@m.ierla.nosp@m.bs.c.nosp@m.om.

Defines the socket types defined by CSimpleSocket class.

Enumerator
SocketTypeInvalid 

Invalid socket type.

SocketTypeTcp 

Defines socket as TCP socket.

SocketTypeUdp 

Defines socket as UDP socket.

SocketTypeTcp6 

Defines socket as IPv6 TCP socket.

SocketTypeUdp6 

Defines socket as IPv6 UDP socket.

SocketTypeRaw 

Provides raw network protocol access.

Member Function Documentation

bool CSimpleSocket::BindInterface ( const char *  pInterface)

Bind socket to a specific interface when using multicast.

Returns
true if successfully bound to interface
bool CSimpleSocket::Close ( void  )
virtual

Close socket

Returns
true if successfully closed otherwise returns false.
const char * CSimpleSocket::DescribeError ( CSocketError  err)
static

Returns a human-readable description of the given error code or the last error code of a socket

bool CSimpleSocket::DisableNagleAlgoritm ( )

Disable the Nagle algorithm (Set TCP_NODELAY to true)

Returns
false if failed to set socket option otherwise return true;
bool CSimpleSocket::EnableNagleAlgoritm ( )

Enable the Nagle algorithm (Set TCP_NODELAY to false)

Returns
false if failed to set socket option otherwise return true;
bool CSimpleSocket::Flush ( )
protected

Flush the socket descriptor owned by the object.

Returns
true data was successfully sent, else return false;
int32_t ydlidar::CSimpleSocket::GetBytesReceived ( void  )
inline

Returns the number of bytes received on the last call to CSocket::Receive().

Returns
number of bytes received.
int32_t ydlidar::CSimpleSocket::GetBytesSent ( void  )
inline

Returns the number of bytes sent on the last call to CSocket::Send().

Returns
number of bytes sent.
const char* ydlidar::CSimpleSocket::GetClientAddr ( )
inline

Returns clients Internet host address as a string in standard numbers-and-dots notation.

Returns
NULL if invalid
uint16_t ydlidar::CSimpleSocket::GetClientPort ( )
inline

Returns the port number on which the client is connected.

Returns
client port number.
int32_t ydlidar::CSimpleSocket::GetConnectTimeoutSec ( void  )
inline

Gets the timeout value that specifies the maximum number of seconds a call to CSimpleSocket::Open waits until it completes.

Returns
the length of time in seconds
int32_t ydlidar::CSimpleSocket::GetConnectTimeoutUSec ( void  )
inline

Gets the timeout value that specifies the maximum number of microseconds a call to CSimpleSocket::Open waits until it completes.

Returns
the length of time in microseconds
uint8_t* ydlidar::CSimpleSocket::GetData ( void  )
inline

Get a pointer to internal receive buffer. The user MUST not free this pointer when finished. This memory is managed internally by the CSocket class.

Returns
pointer to data if valid, else returns NULL.
bool ydlidar::CSimpleSocket::GetMulticast ( )
inline

Return true if socket is multicast or false is socket is unicast

Returns
true if multicast is enabled
int32_t ydlidar::CSimpleSocket::GetReceiveTimeoutSec ( void  )
inline

Gets the timeout value that specifies the maximum number of seconds a a call to CSimpleSocket::Receive waits until it completes.

Returns
the length of time in seconds
int32_t ydlidar::CSimpleSocket::GetReceiveTimeoutUSec ( void  )
inline

Gets the timeout value that specifies the maximum number of microseconds a call to CSimpleSocket::Receive waits until it completes.

Returns
the length of time in microseconds
uint32_t ydlidar::CSimpleSocket::GetReceiveWindowSize ( )
inline

Get the TCP receive buffer window size for the current socket object.

NOTE: Linux will set the receive buffer to twice the value passed.

Returns
zero on failure else the number of bytes of the TCP receive buffer window size if successful.
int32_t ydlidar::CSimpleSocket::GetSendTimeoutSec ( void  )
inline

Gets the timeout value that specifies the maximum number of seconds a a call to CSimpleSocket::Send waits until it completes.

Returns
the length of time in seconds
int32_t ydlidar::CSimpleSocket::GetSendTimeoutUSec ( void  )
inline

Gets the timeout value that specifies the maximum number of microseconds a call to CSimpleSocket::Send waits until it completes.

Returns
the length of time in microseconds
uint32_t ydlidar::CSimpleSocket::GetSendWindowSize ( )
inline

Get the TCP send buffer window size for the current socket object.

NOTE: Linux will set the send buffer to twice the value passed.

Returns
zero on failure else the number of bytes of the TCP receive buffer window size if successful.
const char* ydlidar::CSimpleSocket::GetServerAddr ( )
inline

Returns server Internet host address as a string in standard numbers-and-dots notation.

Returns
NULL if invalid
uint16_t ydlidar::CSimpleSocket::GetServerPort ( )
inline

Returns the port number on which the server is connected.

Returns
server port number.
SOCKET ydlidar::CSimpleSocket::GetSocketDescriptor ( )
inline

Return socket descriptor

Returns
socket descriptor which is a signed 32 bit integer.
int32_t CSimpleSocket::GetSocketDscp ( void  )

Return Differentiated Services Code Point (DSCP) value currently set on the socket object.

Returns
DSCP for current socket object.

NOTE: Windows special notes http://support.microsoft.com/kb/248611.
CSocketError ydlidar::CSimpleSocket::GetSocketError ( void  )
inline

Returns the last error that occured for the instace of the CSimpleSocket instance. This method should be called immediately to retrieve the error code for the failing mehtod call.

Returns
last error that occured.
CSocketType ydlidar::CSimpleSocket::GetSocketType ( )
inline

Return socket descriptor

Returns
socket descriptor which is a signed 32 bit integer.
uint32_t ydlidar::CSimpleSocket::GetTotalTimeMs ( )
inline

Get the total time the of the last operation in milliseconds.

Returns
number of milliseconds of last operation.
uint32_t ydlidar::CSimpleSocket::GetTotalTimeUsec ( )
inline

Get the total time the of the last operation in microseconds.

Returns
number of microseconds or last operation.
bool CSimpleSocket::Initialize ( void  )
virtual

Initialize instance of CSocket. This method MUST be called before an object can be used. Errors : CSocket::SocketProtocolError, CSocket::SocketInvalidSocket,

Returns
true if properly initialized.
bool ydlidar::CSimpleSocket::IsNonblocking ( void  )
inline

Returns blocking/non-blocking state of socket.

Returns
true if the socket is non-blocking, else return false.
virtual bool ydlidar::CSimpleSocket::IsSocketValid ( void  )
inlinevirtual

Does the current instance of the socket object contain a valid socket descriptor.

Returns
true if the socket object contains a valid socket descriptor.
int32_t CSimpleSocket::Receive ( int32_t  nMaxBytes = 1,
uint8_t *  pBuffer = 0 
)
virtual

Attempts to receive a block of data on an established connection.

Parameters
nMaxBytesmaximum number of bytes to receive.
pBuffer,memorywhere to receive the data, NULL receives to internal buffer returned with GetData() Non-NULL receives directly there, but GetData() will return WRONG ptr!
Returns
number of bytes actually received.
of zero means the connection has been shutdown on the other side.
of -1 means that an error has occurred.
virtual bool ydlidar::CSimpleSocket::Select ( void  )
inlinevirtual

Examine the socket descriptor sets currently owned by the instance of the socket class (the readfds, writefds, and errorfds parameters) to see whether some of their descriptors are ready for reading, are ready for writing, or have an exceptional condition pending, respectively. Block until an event happens on the specified file descriptors.

Returns
true if socket has data ready, or false if not ready or timed out.
bool CSimpleSocket::Select ( int32_t  nTimeoutSec,
int32_t  nTimeoutUSec 
)
virtual

Examine the socket descriptor sets currently owned by the instance of the socket class (the readfds, writefds, and errorfds parameters) to see whether some of their descriptors are ready for reading, are ready for writing, or have an exceptional condition pending, respectively.

Parameters
nTimeoutSectimeout in seconds for select.
nTimeoutUSectimeout in micro seconds for select.
Returns
true if socket has data ready, or false if not ready or timed out.
int32_t CSimpleSocket::Send ( const uint8_t *  pBuf,
size_t  bytesToSend 
)
virtual

Attempts to send a block of data on an established connection.

Parameters
pBufblock of data to be sent.
bytesToSendsize of data block to be sent.
Returns
number of bytes actually sent.
of zero means the connection has been shutdown on the other side.
of -1 means that an error has occurred.

Reimplemented in ydlidar::CPassiveSocket.

int32_t CSimpleSocket::Send ( const struct iovec *  sendVector,
int32_t  nNumItems 
)
virtual

Attempts to send at most nNumItem blocks described by sendVector to the socket descriptor associated with the socket object.

Parameters
sendVectorpointer to an array of iovec structures
nNumItemsnumber of items in the vector to process
NOTE: Buffers are processed in the order specified.
Returns
number of bytes actually sent, return of zero means the connection has been shutdown on the other side, and a return of -1 means that an error has occurred.
int32_t CSimpleSocket::SendFile ( int32_t  nOutFd,
int32_t  nInFd,
off_t *  pOffset,
int32_t  nCount 
)
virtual

Copies data between one file descriptor and another. On some systems this copying is done within the kernel, and thus is more efficient than the combination of CSimpleSocket::Send and CSimpleSocket::Receive, which would require transferring data to and from user space.
Note: This is available on all implementations, but the kernel implementation is only available on Unix type systems.

Parameters
nOutFddescriptor opened for writing.
nInFddescriptor opened for reading.
pOffsetfrom which to start reading data from input file.
nCountnumber of bytes to copy between file descriptors.
Returns
number of bytes written to the out socket descriptor.
bool CSimpleSocket::SetBlocking ( void  )

Set the socket to blocking.

Returns
true if successful set to blocking, else return false;
void ydlidar::CSimpleSocket::SetConnectTimeout ( int32_t  nConnectTimeoutSec,
int32_t  nConnectTimeoutUsec = 0 
)
inline

Sets the timeout value that specifies the maximum amount of time a call to CSimpleSocket::Receive waits until it completes. Use the method CSimpleSocket::SetReceiveTimeout to specify the number of seconds to wait. If a call to CSimpleSocket::Receive has blocked for the specified length of time without receiving additional data, it returns with a partial count or CSimpleSocket::GetSocketError set to CSimpleSocket::SocketEwouldblock if no data were received.

Parameters
nConnectTimeoutSecof timeout in seconds.
nConnectTimeoutUsecof timeout in microseconds.
Returns
true if socket connection timeout was successfully set.
bool CSimpleSocket::SetMulticast ( bool  bEnable,
uint8_t  multicastTTL = 1 
)

Enable/disable multicast for a socket. This options is only valid for socket descriptors of type CSimpleSocket::SocketTypeUdp.

Returns
true if multicast was enabled or false if socket type is not CSimpleSocket::SocketTypeUdp and the error will be set to CSimpleSocket::SocketProtocolError
bool CSimpleSocket::SetNonblocking ( void  )

Set the socket as non-blocking.

Returns
true if successful set to non-blocking, else return false;
bool CSimpleSocket::SetOptionLinger ( bool  bEnable,
uint16_t  nTime 
)

Controls the actions taken when CSimpleSocket::Close is executed on a socket object that has unsent data. The default value for this option is off.

  • Following are the three possible scenarios.

    1. bEnable is false, CSimpleSocket::Close returns immediately, but any unset data is transmitted (after CSimpleSocket::Close returns)
    2. bEnable is true and nTime is zero, CSimpleSocket::Close return immediately and any unsent data is discarded.
    3. bEnable is true and nTime is nonzero, CSimpleSocket::Close does not return until all unsent data is transmitted (or the connection is Closed by the remote system).
    Parameters
    bEnabletrue to enable option false to disable option.
    nTimetime in seconds to linger.
    Returns
    true if option successfully set
bool CSimpleSocket::SetOptionReuseAddr ( )

Tells the kernel that even if this port is busy (in the TIME_WAIT state), go ahead and reuse it anyway. If it is busy, but with another state, you will still get an address already in use error.

Returns
true if option successfully set
bool CSimpleSocket::SetReceiveTimeout ( int32_t  nRecvTimeoutSec,
int32_t  nRecvTimeoutUsec = 0 
)

Sets the timeout value that specifies the maximum amount of time a call to CSimpleSocket::Receive waits until it completes. Use the method CSimpleSocket::SetReceiveTimeout to specify the number of seconds to wait. If a call to CSimpleSocket::Receive has blocked for the specified length of time without receiving additional data, it returns with a partial count or CSimpleSocket::GetSocketError set to CSimpleSocket::SocketEwouldblock if no data were received.

Parameters
nRecvTimeoutSecof timeout in seconds.
nRecvTimeoutUsecof timeout in microseconds.
Returns
true if socket timeout was successfully set.
uint32_t ydlidar::CSimpleSocket::SetReceiveWindowSize ( uint32_t  nWindowSize)
inline

Set the TCP receive buffer window size for the current socket object.

NOTE: Linux will set the receive buffer to twice the value passed.

Returns
zero on failure else the number of bytes of the TCP send buffer window size if successful.
bool CSimpleSocket::SetSendTimeout ( int32_t  nSendTimeoutSec,
int32_t  nSendTimeoutUsec = 0 
)

Gets the timeout value that specifies the maximum amount of time a call to CSimpleSocket::Send waits until it completes.

Returns
the length of time in seconds
uint32_t ydlidar::CSimpleSocket::SetSendWindowSize ( uint32_t  nWindowSize)
inline

Set the TCP send buffer window size for the current socket object.

NOTE: Linux will set the send buffer to twice the value passed.

Returns
zero on failure else the number of bytes of the TCP send buffer window size if successful.
bool CSimpleSocket::SetSocketDscp ( int  nDscp)

Set Differentiated Services Code Point (DSCP) for socket object.

Parameters
nDscpvalue of TOS setting which will be converted to DSCP
Returns
true if DSCP value was properly set

NOTE: Windows special notes http://support.microsoft.com/kb/248611.
void ydlidar::CSimpleSocket::SetSocketError ( CSimpleSocket::CSocketError  error)
inlineprotected

Set internal socket error to that specified error

Parameters
errortype of error
void ydlidar::CSimpleSocket::SetSocketHandle ( SOCKET  socket)
inlineprotected

Set object socket handle to that specified as parameter

Parameters
socketvalue of socket descriptor
bool CSimpleSocket::Shutdown ( CShutdownMode  nShutdown)
virtual

Shutdown shut down socket send and receive operations CShutdownMode::Receives - Disables further receive operations. CShutdownMode::Sends - Disables further send operations. CShutdownBoth:: - Disables further send and receive operations.

Parameters
nShutdownspecifies the type of shutdown.
Returns
true if successfully shutdown otherwise returns false.
void CSimpleSocket::TranslateSocketError ( void  )

Provides a standard error code for cross platform development by mapping the operating system error to an error defined by the CSocket class.


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