This is Webservice SDK for mbed. LPCXpresso1769/LPC1768/FRDM-K64F/LPC4088
Fork of libMiMic by
mbed/UdpSocket.cpp@115:fa79286d8ea4, 2017-02-24 (annotated)
- Committer:
- furutani
- Date:
- Fri Feb 24 04:43:41 2017 +0000
- Revision:
- 115:fa79286d8ea4
- Parent:
- 110:257739f9b31e
Delete missing include line.; Add parameter "timeout" to TCPSocket::connect(), precv().; Fix to send ARP request to default gateway when connecting to IP address of different segment.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nyatla | 58:03b89038b21a | 1 | //////////////////////////////////////////////////////////////////////////////// |
nyatla | 58:03b89038b21a | 2 | // UdpSocket.cpp |
nyatla | 58:03b89038b21a | 3 | //////////////////////////////////////////////////////////////////////////////// |
nyatla | 58:03b89038b21a | 4 | |
nyatla | 58:03b89038b21a | 5 | #include "UdpSocket.h" |
nyatla | 109:18f12ac01097 | 6 | #include "mbed.h" |
nyatla | 58:03b89038b21a | 7 | |
nyatla | 58:03b89038b21a | 8 | |
nyatla | 58:03b89038b21a | 9 | |
nyatla | 58:03b89038b21a | 10 | namespace MiMic |
nyatla | 58:03b89038b21a | 11 | { |
nyatla | 58:03b89038b21a | 12 | #define TIMEOUT_IN_MSEC (2*1000) |
nyatla | 58:03b89038b21a | 13 | |
nyatla | 109:18f12ac01097 | 14 | void UdpSocket::rxhandler(NyLPC_TiUdpSocket_t* i_inst,const void* i_buf,const struct NyLPC_TIPv4RxInfo* i_info) |
nyatla | 58:03b89038b21a | 15 | { |
nyatla | 109:18f12ac01097 | 16 | UdpSocket* sock=(UdpSocket*)i_inst->_tag; |
nyatla | 109:18f12ac01097 | 17 | sock->onRxHandler(i_buf,i_info); |
nyatla | 58:03b89038b21a | 18 | } |
nyatla | 58:03b89038b21a | 19 | |
nyatla | 109:18f12ac01097 | 20 | UdpSocket::UdpSocket(unsigned short i_port,bool i_nobuffer) |
nyatla | 58:03b89038b21a | 21 | { |
nyatla | 109:18f12ac01097 | 22 | if(i_nobuffer){ |
nyatla | 110:257739f9b31e | 23 | this->_inst=NyLPC_cNet_createUdpSocketEx(i_port,NyLPC_TSocketType_UDP_NOBUF); |
nyatla | 109:18f12ac01097 | 24 | }else{ |
nyatla | 110:257739f9b31e | 25 | this->_inst=NyLPC_cNet_createUdpSocketEx(i_port,NyLPC_TSocketType_UDP_NORMAL); |
nyatla | 109:18f12ac01097 | 26 | } |
nyatla | 109:18f12ac01097 | 27 | if(this->_inst==NULL){ |
nyatla | 109:18f12ac01097 | 28 | mbed_die(); |
nyatla | 109:18f12ac01097 | 29 | } |
nyatla | 58:03b89038b21a | 30 | } |
nyatla | 58:03b89038b21a | 31 | UdpSocket::~UdpSocket() |
nyatla | 58:03b89038b21a | 32 | { |
nyatla | 109:18f12ac01097 | 33 | NyLPC_iUdpSocket_finalize(this->_inst); |
nyatla | 58:03b89038b21a | 34 | } |
nyatla | 60:803de2088243 | 35 | bool UdpSocket::canRecv() |
nyatla | 60:803de2088243 | 36 | { |
nyatla | 60:803de2088243 | 37 | const void* rx; |
nyatla | 60:803de2088243 | 38 | const struct NyLPC_TIPv4RxInfo* info; |
nyatla | 109:18f12ac01097 | 39 | return NyLPC_iUdpSocket_precv(this->_inst,&rx,&info,TIMEOUT_IN_MSEC)>0; |
nyatla | 60:803de2088243 | 40 | } |
nyatla | 58:03b89038b21a | 41 | |
nyatla | 64:258e84040262 | 42 | int UdpSocket::precvFrom(const void* &i_rx,IpAddr* i_peer_host,unsigned short* i_port) |
nyatla | 58:03b89038b21a | 43 | { |
nyatla | 58:03b89038b21a | 44 | const struct NyLPC_TIPv4RxInfo* info; |
nyatla | 109:18f12ac01097 | 45 | int rs=NyLPC_iUdpSocket_precv(this->_inst,&i_rx,&info,TIMEOUT_IN_MSEC); |
nyatla | 58:03b89038b21a | 46 | if(rs>1){ |
nyatla | 58:03b89038b21a | 47 | if(i_peer_host!=NULL){ |
nyatla | 58:03b89038b21a | 48 | i_peer_host->setIPv4(info->peer_ip); |
nyatla | 58:03b89038b21a | 49 | } |
nyatla | 58:03b89038b21a | 50 | if(i_port!=NULL){ |
nyatla | 58:03b89038b21a | 51 | *i_port=info->peer_port; |
nyatla | 58:03b89038b21a | 52 | } |
nyatla | 58:03b89038b21a | 53 | } |
nyatla | 58:03b89038b21a | 54 | return rs; |
nyatla | 58:03b89038b21a | 55 | } |
nyatla | 64:258e84040262 | 56 | int UdpSocket::precvFrom(const char* &i_rx,IpAddr* i_peer_host,unsigned short* i_port) |
nyatla | 60:803de2088243 | 57 | { |
nyatla | 60:803de2088243 | 58 | const struct NyLPC_TIPv4RxInfo* info; |
nyatla | 109:18f12ac01097 | 59 | int rs=NyLPC_iUdpSocket_precv(this->_inst,(const void**)&i_rx,&info,TIMEOUT_IN_MSEC); |
nyatla | 60:803de2088243 | 60 | if(rs>1){ |
nyatla | 60:803de2088243 | 61 | if(i_peer_host!=NULL){ |
nyatla | 60:803de2088243 | 62 | i_peer_host->setIPv4(info->peer_ip); |
nyatla | 60:803de2088243 | 63 | } |
nyatla | 60:803de2088243 | 64 | if(i_port!=NULL){ |
nyatla | 60:803de2088243 | 65 | *i_port=info->peer_port; |
nyatla | 60:803de2088243 | 66 | } |
nyatla | 60:803de2088243 | 67 | } |
nyatla | 60:803de2088243 | 68 | return rs; |
nyatla | 60:803de2088243 | 69 | } |
nyatla | 58:03b89038b21a | 70 | |
nyatla | 64:258e84040262 | 71 | void UdpSocket::precvNext(void) |
nyatla | 58:03b89038b21a | 72 | { |
nyatla | 109:18f12ac01097 | 73 | NyLPC_iUdpSocket_pseek(this->_inst); |
nyatla | 58:03b89038b21a | 74 | } |
nyatla | 58:03b89038b21a | 75 | |
nyatla | 58:03b89038b21a | 76 | bool UdpSocket::sendTo(const IpAddr& i_host,unsigned short i_port,const void* i_tx,unsigned short i_tx_size) |
nyatla | 58:03b89038b21a | 77 | { |
nyatla | 109:18f12ac01097 | 78 | int r=NyLPC_iUdpSocket_send(this->_inst,&i_host.addr.v4,i_port,i_tx,i_tx_size,TIMEOUT_IN_MSEC); |
nyatla | 60:803de2088243 | 79 | return (r==i_tx_size); |
nyatla | 58:03b89038b21a | 80 | |
nyatla | 58:03b89038b21a | 81 | } |
nyatla | 58:03b89038b21a | 82 | |
nyatla | 58:03b89038b21a | 83 | void UdpSocket::joinMulticast(const IpAddr& i_host) |
nyatla | 58:03b89038b21a | 84 | { |
nyatla | 109:18f12ac01097 | 85 | NyLPC_iUdpSocket_joinMulticast(this->_inst,&i_host.addr.v4); |
nyatla | 58:03b89038b21a | 86 | } |
nyatla | 58:03b89038b21a | 87 | void UdpSocket::setBroadcast(void) |
nyatla | 58:03b89038b21a | 88 | { |
nyatla | 109:18f12ac01097 | 89 | NyLPC_iUdpSocket_setBroadcast(this->_inst); |
nyatla | 58:03b89038b21a | 90 | |
nyatla | 58:03b89038b21a | 91 | } |
nyatla | 109:18f12ac01097 | 92 | } |