UdpSendReceive Class
Class for sending and receiving generic udp packets using one socket. More...
Header: | #include <OSAPI/Network/UdpSendReceive.h> |
Inherited By: |
Public Types
enum | Status { OK, ERR } |
Public Functions
UdpSendReceive() | |
UdpSendReceive(IpAddress &ipRemote, IpAddress &ipLocal, bool nonBlocking = true, const int bufferSize = -1, const bool messageTransportPackets = false) | |
virtual | ~UdpSendReceive() |
Status | Close() |
virtual void | EnableBroadcast() |
void | EnableReuse(bool bReuse) |
double | GetBlockingTimer() |
virtual bool | GetBroadcastSocket() |
virtual unsigned int | GetInterfaceNo() |
int | GetLastErrorCode() |
double | GetMaxBlockingTimer() |
virtual bool | GetMessageTransportPackets() |
bool | GetNumberOfBytesPending(unsigned int *numberOfBytesPending) |
IpAddress & | GetRemoteIpReceivedFrom() |
SOCKET | GetSocket() |
Status | Init(IpAddress &ipRemote, IpAddress &ipLocal, const bool nonBlocking = true, const int bufferSize = -1, const bool messageTransportPackets = false) |
bool | IsOpen() const |
bool | IsSocketError() |
bool | IsSocketReadReady(int nTimeoutMs) |
bool | IsSocketWriteReady(int nTimeoutMs) |
virtual bool | MulticastJoin(IpAddress &multicastGroup, IpAddress &localInterface) |
virtual bool | MulticastLeave(IpAddress &multicastGroup, IpAddress &localInterface) |
void | PeekPacket(int *numberOfBytesReceived, int maxNumberOfBytes, void *data) |
virtual void | ReceivePacket(int *numberOfBytesReceived, int maxNumberOfBytes, void *data) |
void | ResetMaxBlockingTimer() |
Status | SendPacket(const void *buf, const int size) |
Status | SendPacketTo(const void *buf, const int size, IpAddress &destination) |
virtual bool | SetMulticastTTL(int timeToLive) |
Status | SetTargetIpAddress(IpAddress &ipAddress) |
virtual bool | SetTypeOfServiceField(int tos) |
Status | Shutdown() |
Protected Functions
Status | Open() |
bool | ReceivingUnicast(const void *data) |
bool | SendingUnicast(const void *buf, const IpAddress &dest) |
Protected Variables
bool | m_bBroadcastSocket |
bool | m_bMessageTransportPackets |
bool | m_bSocketBound |
int | m_bufferSize |
unsigned int | m_dInterfaceNo |
IpAddress | m_ipLocal |
IpAddress | m_ipRemote |
IpAddress | m_ipRemoteReceivedFrom |
int | m_lastErrorCode |
int | m_nReuse |
bool | nonBlocking |
SOCKET | sd |
Detailed Description
Class for sending and receiving generic udp packets using one socket.
This class must be used if you need to receive on the same port as is used as the source port when sending. If separate UdpSend and UdpReceive sockets are used in this case, the UdpSend socket will "steal" the arriving packets which should have arrived at the UdpReceive socket.
The following example demonstrates how the UdpSendReceive class can be used to wait for a message and then send a message in return:
IpAddress ipLocal = EthernetManager::GetInstance()->GetNetworkInterface(0); IpAddress ipRemote = ipLocal; ipLocal.SetPort(5555); ipRemote.SetPort(5556); UdpSendReceive udpSendReceive; if(UdpReceive::ERR == udpSendReceive.Init(ipRemote, ipLocal)) CDPMessage("Failed to initialize!\n"); while(!Stopped()) { if(udpSendReceive.IsSocketReadReady(0)) { int bytesReceived = 0; int bufferSize = 100; char buffer[bufferSize]; udpSendReceive.ReceivePacket(&bytesReceived, bufferSize-1, buffer); if(strcmp(buffer, "SendResponse")==0) { if(udpSendReceive.IsSocketWriteReady(100)) { char data[] = "MessageReceived"; udpSendReceive.SendPacket(data, sizeof(data)); } else CDPMessage("Failed to send response!\n"); } } OSAPISleep(100); }
If the communication is not working as expected, you may increase the debug- level in your Application. Set the Debug property to 1 or 2 in the Properties- section, and you may get printouts in the Application Output console window giving indication of what might be wrong.
See also UdpSend and UdpReceive.
Member Type Documentation
enum UdpSendReceive::Status
This enum type specifies operation status:
Constant | Value | Description |
---|---|---|
UdpSendReceive::OK | 0 | Operation succeded |
UdpSendReceive::ERR | -1 | Operation failed |
Member Function Documentation
UdpSendReceive::UdpSendReceive()
Constructor.
UdpSendReceive::UdpSendReceive(IpAddress &ipRemote, IpAddress &ipLocal, bool nonBlocking = true, const int bufferSize = -1, const bool messageTransportPackets = false)
Constructor that makes it unnecessary to execute Init().
The local and remote ports must be specified in the IpAddress arguments.
Use the nonBlocking argument to specify whether or not the receive functions should block until a message is received. When configured with the blocking flag set to true
, the user has to be careful not to use the object in code that is executed by the CDPEngine, like state machines and message handlers.
The messageTransportPackets argument should be set to true
if the packets to be sent are only of type MessageTransportPackets. This type of packet are used internally by the CDP protocol and rarely in user code.
[virtual]
UdpSendReceive::~UdpSendReceive()
Virtual destructor.
Status UdpSendReceive::Close()
Closes the connection (socket) for sending data.
[virtual]
void UdpSendReceive::EnableBroadcast()
Enables broadcast acceptance.
void UdpSendReceive::EnableReuse(bool bReuse)
Enable reuse of socket, causes SO_REUSEADDR flag to be set in next Init-call.
double UdpSendReceive::GetBlockingTimer()
Gets the last measured BlockingTimeout (only updated if ::debug is at least DEBUGLEVEL_NORMAL).
[virtual]
bool UdpSendReceive::GetBroadcastSocket()
Returns true
if the socket only receives/sends broadcast-messages.
[virtual]
unsigned int UdpSendReceive::GetInterfaceNo()
Returns which interface number this socket receives/sends packets from.
int UdpSendReceive::GetLastErrorCode()
Returns the last error code.
If an UdpSendReceive-function indicates error, this function can be called immediately after to get the error code. The error code is reset to 0 when executing any class function that may get error.
double UdpSendReceive::GetMaxBlockingTimer()
Gets the max BlockingTimeout in seconds (only updated if ::debug is at least DEBUGLEVEL_NORMAL).
The max blocking value shows the amount of time spent blocking. When the value increases it is not filtered, but when decreasing it is reduced with 1% of the value difference.
[virtual]
bool UdpSendReceive::GetMessageTransportPackets()
Returns true
if the packets to be received/sent are only of type MessageTransportPacket.
This bool value is specified in constructor or Init().
bool UdpSendReceive::GetNumberOfBytesPending(unsigned int *numberOfBytesPending)
Updates numberOfBytesPending with the total number of bytes in the first UDP packet queued on the socket.
Returns false if an error occured and true when numberOfBytesPending was updated.
IpAddress &UdpSendReceive::GetRemoteIpReceivedFrom()
Returns the remote ipaddress that we received data from last time.
SOCKET UdpSendReceive::GetSocket()
Returns the socket descriptor.
Status UdpSendReceive::Init(IpAddress &ipRemote, IpAddress &ipLocal, const bool nonBlocking = true, const int bufferSize = -1, const bool messageTransportPackets = false)
Initializes the socket with IP address/port.
The local and remote ports must be specified in the IpAddress arguments.
May be called several times to re-initialize.
Use the nonBlocking argument to specify whether or not the receive functions should block until a message is received. When configured with the blocking flag set to true
, the user have to be careful not to use the object in code that is executed by the CDPEngine, like state machines and message handlers.
The messageTransportPackets argument should be set to true
if the packets to be sent are only of type MessageTransportPackets. This type of packet are used internally by the CDP protocol and rarely in user code.
bool UdpSendReceive::IsOpen() const
Determines if the connection is open and ready to send.
bool UdpSendReceive::IsSocketError()
Returns true if the socket is in error.
bool UdpSendReceive::IsSocketReadReady(int nTimeoutMs)
Returns true if the socket has data ready for reading.
Use nTimeoutMs to specify the maximum time to wait for the socket to get ready.
bool UdpSendReceive::IsSocketWriteReady(int nTimeoutMs)
Returns true if the socket can be written to.
Use nTimeoutMs to specify the maximum time to wait for the socket to get ready.
[virtual]
bool UdpSendReceive::MulticastJoin(IpAddress &multicastGroup, IpAddress &localInterface)
Join a multicast group.
[virtual]
bool UdpSendReceive::MulticastLeave(IpAddress &multicastGroup, IpAddress &localInterface)
Leave a multicast group.
[protected]
Status UdpSendReceive::Open()
Creates a new socket or reuses open socket for communication.
The function also performs some socket configuration.
void UdpSendReceive::PeekPacket(int *numberOfBytesReceived, int maxNumberOfBytes, void *data)
Peeks into packetdata without removing it. Call ReceivePacket() to remove.
The variable provided in numberOfBytesReceived will get updated with the number of bytes received or 0 if an error occurred.
Use maxNumberOfBytes to specify a maximum number of bytes to receive.
[virtual]
void UdpSendReceive::ReceivePacket(int *numberOfBytesReceived, int maxNumberOfBytes, void *data)
Receives a packet with max number of bytes and stores the result in data.
The variable provided in numberOfBytesReceived will get updated with the number of bytes received or 0 if an error occurred.
[protected]
bool UdpSendReceive::ReceivingUnicast(const void *data)
Returns true if we are receiving a unicast messages.
void UdpSendReceive::ResetMaxBlockingTimer()
Resets the max BlockingTimeout.
Status UdpSendReceive::SendPacket(const void *buf, const int size)
Send a packet.
Status UdpSendReceive::SendPacketTo(const void *buf, const int size, IpAddress &destination)
Send a packet to a specified destination ipaddress.
[protected]
bool UdpSendReceive::SendingUnicast(const void *buf, const IpAddress &dest)
Returns true if we are sending a unicast message.
[virtual]
bool UdpSendReceive::SetMulticastTTL(int timeToLive)
Set TimeToLive for multicast (i.e. number of network hops).
Status UdpSendReceive::SetTargetIpAddress(IpAddress &ipAddress)
Set/change the IP-address to which the packet is sent.
[virtual]
bool UdpSendReceive::SetTypeOfServiceField(int tos)
Set the Type Of Service field (8bits) for outgoing packets.
For more info, see RFC 1349. Returns false
if failure.
- 0x04 (IPTOS_RELIABILITY): QoS level 2
- 0x08 (IPTOS_THROUGHPUT) : QoS level 2
- 0x10 (IPTOS_LOWDELAY): QoS level 2
- 0xF1: QoS level 1 (lowest priority level)
- 0xF2: QoS level 2
- 0xF3: QoS level 3
- 0xF4: QoS level 4 (highest priority level)
Status UdpSendReceive::Shutdown()
Calls shutdown() on the socket.
Useful when another thread is blocked waiting on a socket (e.g. calling ReceivePacket()). Shutdown() will unblock the thread. Use Close() shutdown and free resources.
Member Variable Documentation
bool UdpSendReceive::m_bBroadcastSocket
This variable holds true if the socket only receives/sends broadcast-messages (e.g. if local ip equals "10.0.2.255" when subnetmask is 255.255.255.0).
bool UdpSendReceive::m_bMessageTransportPackets
This variable holds true if the packets to be received/sent are only of type MessageTransportPacket.
bool UdpSendReceive::m_bSocketBound
This variable holds true if socket is bound.
int UdpSendReceive::m_bufferSize
This variable holds the size of the socket output buffer.
unsigned int UdpSendReceive::m_dInterfaceNo
This variable holds which interface no this sockets receives/sends packets from (specified m_ipLocal is compared to ipaddresses found in NetworkInterfaceList).
IpAddress UdpSendReceive::m_ipLocal
This variable holds the local ip-address to use.
IpAddress UdpSendReceive::m_ipRemote
This variable holds the target IP-address.
IpAddress UdpSendReceive::m_ipRemoteReceivedFrom
This variable holds the remote ipaddress that we received data from last time.
int UdpSendReceive::m_lastErrorCode
This variable contains the last error code returned by GetLastErrorMessage().
int UdpSendReceive::m_nReuse
This variable holds true if we should reuse socket.
bool UdpSendReceive::nonBlocking
This variable specifies if the socket connection should be non-blocking (set by Init).
SOCKET UdpSendReceive::sd
This variable holds socket descriptor (handle) for the socket used.
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.