This is Webservice SDK for mbed. LPCXpresso1769/LPC1768/FRDM-K64F/LPC4088

Fork of libMiMic by Ryo Iizuka

Committer:
nyatla
Date:
Wed Oct 23 04:49:08 2013 +0000
Revision:
64:258e84040262
Parent:
60:803de2088243
Child:
109:18f12ac01097
fix issue; http://mbed.org/users/nyatla/code/libMiMic/issues/1

Who changed what in which revision?

UserRevisionLine numberNew 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 58:03b89038b21a 6
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 58:03b89038b21a 14 UdpSocket::UdpSocket(unsigned short i_port,unsigned short i_rx_buf_size)
nyatla 58:03b89038b21a 15 {
nyatla 58:03b89038b21a 16 this->_private_rx=malloc(i_rx_buf_size);
nyatla 58:03b89038b21a 17 NyLPC_cUdpSocket_initialize(&this->_inst,i_port,this->_private_rx,i_rx_buf_size);
nyatla 58:03b89038b21a 18 }
nyatla 58:03b89038b21a 19 UdpSocket::UdpSocket(unsigned short i_port,void* i_rx_buf,unsigned short i_rx_buf_size)
nyatla 58:03b89038b21a 20 {
nyatla 58:03b89038b21a 21 this->_private_rx=NULL;
nyatla 58:03b89038b21a 22 NyLPC_cUdpSocket_initialize(&this->_inst,i_port,i_rx_buf,i_rx_buf_size);
nyatla 58:03b89038b21a 23 }
nyatla 58:03b89038b21a 24
nyatla 58:03b89038b21a 25 UdpSocket::UdpSocket(unsigned short i_port,void* i_rx_handler)
nyatla 58:03b89038b21a 26 {
nyatla 58:03b89038b21a 27 }
nyatla 58:03b89038b21a 28 UdpSocket::~UdpSocket()
nyatla 58:03b89038b21a 29 {
nyatla 58:03b89038b21a 30 NyLPC_cUdpSocket_finalize(&this->_inst);
nyatla 58:03b89038b21a 31 if(this->_private_rx!=NULL){
nyatla 58:03b89038b21a 32 free(this->_private_rx);
nyatla 58:03b89038b21a 33 }
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 60:803de2088243 39 return NyLPC_cUdpSocket_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 58:03b89038b21a 45 int rs=NyLPC_cUdpSocket_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 60:803de2088243 59 int rs=NyLPC_cUdpSocket_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 58:03b89038b21a 73 NyLPC_cUdpSocket_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 58:03b89038b21a 78 int r=NyLPC_cUdpSocket_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 58:03b89038b21a 85 NyLPC_cUdpSocket_joinMulticast(&this->_inst,&i_host.addr.v4);
nyatla 58:03b89038b21a 86 }
nyatla 58:03b89038b21a 87 void UdpSocket::setBroadcast(void)
nyatla 58:03b89038b21a 88 {
nyatla 58:03b89038b21a 89 NyLPC_cUdpSocket_setBroadcast(&this->_inst);
nyatla 58:03b89038b21a 90
nyatla 58:03b89038b21a 91 }
nyatla 58:03b89038b21a 92 }