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 Bonjour by
UDPSocket.cpp
00001 00002 /* 00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy 00006 of this software and associated documentation files (the "Software"), to deal 00007 in the Software without restriction, including without limitation the rights 00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00009 copies of the Software, and to permit persons to whom the Software is 00010 furnished to do so, subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included in 00013 all copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00021 THE SOFTWARE. 00022 */ 00023 00024 #include "UDPSocket.h" 00025 00026 UDPSocket::UDPSocket() : m_pNetUdpSocket(NULL), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL) 00027 { 00028 00029 } 00030 00031 UDPSocket::~UDPSocket() //close() 00032 { 00033 close(); 00034 } 00035 00036 UDPSocketErr UDPSocket::bind(const Host& me) 00037 { 00038 UDPSocketErr udpSocketErr = checkInst(); 00039 if(udpSocketErr) 00040 return udpSocketErr; 00041 return (UDPSocketErr) m_pNetUdpSocket->bind(me); 00042 } 00043 00044 int /*if < 0 : UDPSocketErr*/ UDPSocket::sendto(const char* buf, int len, Host* pHost) 00045 { 00046 UDPSocketErr udpSocketErr = checkInst(); 00047 if(udpSocketErr) 00048 return udpSocketErr; 00049 return m_pNetUdpSocket->sendto(buf, len, pHost); 00050 } 00051 00052 int /*if < 0 : UDPSocketErr*/ UDPSocket::recvfrom(char* buf, int len, Host* pHost) 00053 { 00054 UDPSocketErr udpSocketErr = checkInst(); 00055 if(udpSocketErr) 00056 return udpSocketErr; 00057 return m_pNetUdpSocket->recvfrom(buf, len, pHost); 00058 } 00059 00060 UDPSocketErr UDPSocket::close() 00061 { 00062 if(!m_pNetUdpSocket) 00063 return UDPSOCKET_SETUP; 00064 m_pNetUdpSocket->resetOnEvent(); 00065 UDPSocketErr udpSocketErr = (UDPSocketErr) m_pNetUdpSocket->close(); //Close (can already be closed) 00066 Net::releaseUdpSocket(m_pNetUdpSocket); //And release it so it can be freed when properly removed 00067 m_pNetUdpSocket = NULL; 00068 return udpSocketErr; 00069 } 00070 00071 //Callbacks 00072 void UDPSocket::setOnEvent( void (*pMethod)(UDPSocketEvent) ) 00073 { 00074 m_pCb = pMethod; 00075 } 00076 00077 #if 0 //For info only 00078 template<class T> 00079 void UDPSocket::setOnEvent( T* pItem, void (T::*pMethod)(UDPSocketEvent) ) 00080 { 00081 m_pCbItem = (CDummy*) pItem; 00082 m_pCbMeth = (void (CDummy::*)(UDPSocketEvent)) pMethod; 00083 } 00084 #endif 00085 00086 void UDPSocket::resetOnEvent() //Disable callback 00087 { 00088 m_pCb = NULL; 00089 m_pCbItem = NULL; 00090 m_pCbMeth = NULL; 00091 } 00092 00093 void UDPSocket::onNetUdpSocketEvent(NetUdpSocketEvent e) 00094 { 00095 if(m_pCbItem && m_pCbMeth) 00096 (m_pCbItem->*m_pCbMeth)((UDPSocketEvent) e); 00097 else if(m_pCb) 00098 m_pCb((UDPSocketEvent) e); 00099 } 00100 00101 UDPSocketErr UDPSocket::checkInst() 00102 { 00103 if(!m_pNetUdpSocket) 00104 { 00105 m_pNetUdpSocket = Net::udpSocket(); 00106 if(!m_pNetUdpSocket) 00107 { 00108 return UDPSOCKET_IF; //Interface did not return a socket (usually because a default interface does not exist) 00109 } 00110 m_pNetUdpSocket->setOnEvent(this, &UDPSocket::onNetUdpSocketEvent); 00111 } 00112 return UDPSOCKET_OK; 00113 }
Generated on Tue Jul 12 2022 18:11:31 by
