Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of libMiMic by
UdpSocket.cpp
00001 //////////////////////////////////////////////////////////////////////////////// 00002 // UdpSocket.cpp 00003 //////////////////////////////////////////////////////////////////////////////// 00004 00005 #include "UdpSocket.h" 00006 #include "mbed.h" 00007 00008 00009 00010 namespace MiMic 00011 { 00012 #define TIMEOUT_IN_MSEC (2*1000) 00013 00014 void UdpSocket::rxhandler(NyLPC_TiUdpSocket_t* i_inst,const void* i_buf,const struct NyLPC_TIPv4RxInfo* i_info) 00015 { 00016 UdpSocket* sock=(UdpSocket*)i_inst->_tag; 00017 sock->onRxHandler(i_buf,i_info); 00018 } 00019 00020 UdpSocket::UdpSocket(unsigned short i_port,bool i_nobuffer) 00021 { 00022 if(i_nobuffer){ 00023 this->_inst=NyLPC_cNet_createUdpSocketEx(i_port,NyLPC_TSocketType_UDP_NOBUF); 00024 }else{ 00025 this->_inst=NyLPC_cNet_createUdpSocketEx(i_port,NyLPC_TSocketType_UDP_NORMAL); 00026 } 00027 if(this->_inst==NULL){ 00028 mbed_die(); 00029 } 00030 } 00031 UdpSocket::~UdpSocket() 00032 { 00033 NyLPC_iUdpSocket_finalize(this->_inst); 00034 } 00035 bool UdpSocket::canRecv() 00036 { 00037 const void* rx; 00038 const struct NyLPC_TIPv4RxInfo* info; 00039 return NyLPC_iUdpSocket_precv(this->_inst,&rx,&info,TIMEOUT_IN_MSEC)>0; 00040 } 00041 00042 int UdpSocket::precvFrom(const void* &i_rx,IpAddr* i_peer_host,unsigned short* i_port) 00043 { 00044 const struct NyLPC_TIPv4RxInfo* info; 00045 int rs=NyLPC_iUdpSocket_precv(this->_inst,&i_rx,&info,TIMEOUT_IN_MSEC); 00046 if(rs>1){ 00047 if(i_peer_host!=NULL){ 00048 i_peer_host->setIPv4(info->peer_ip); 00049 } 00050 if(i_port!=NULL){ 00051 *i_port=info->peer_port; 00052 } 00053 } 00054 return rs; 00055 } 00056 int UdpSocket::precvFrom(const char* &i_rx,IpAddr* i_peer_host,unsigned short* i_port) 00057 { 00058 const struct NyLPC_TIPv4RxInfo* info; 00059 int rs=NyLPC_iUdpSocket_precv(this->_inst,(const void**)&i_rx,&info,TIMEOUT_IN_MSEC); 00060 if(rs>1){ 00061 if(i_peer_host!=NULL){ 00062 i_peer_host->setIPv4(info->peer_ip); 00063 } 00064 if(i_port!=NULL){ 00065 *i_port=info->peer_port; 00066 } 00067 } 00068 return rs; 00069 } 00070 00071 void UdpSocket::precvNext(void) 00072 { 00073 NyLPC_iUdpSocket_pseek(this->_inst); 00074 } 00075 00076 bool UdpSocket::sendTo(const IpAddr& i_host,unsigned short i_port,const void* i_tx,unsigned short i_tx_size) 00077 { 00078 int r=NyLPC_iUdpSocket_send(this->_inst,&i_host.addr.v4,i_port,i_tx,i_tx_size,TIMEOUT_IN_MSEC); 00079 return (r==i_tx_size); 00080 00081 } 00082 00083 void UdpSocket::joinMulticast(const IpAddr& i_host) 00084 { 00085 NyLPC_iUdpSocket_joinMulticast(this->_inst,&i_host.addr.v4); 00086 } 00087 void UdpSocket::setBroadcast(void) 00088 { 00089 NyLPC_iUdpSocket_setBroadcast(this->_inst); 00090 00091 } 00092 }
Generated on Tue Jul 12 2022 16:22:59 by
