Fork of NetServicesMin with some warnings removed

Dependencies:   lwip-sys lwip

Fork of NetServicesMin by Hendrik Lipka

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UDPSocket.cpp Source File

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 #include "if/net/netudpsocket.h"
00026 
00027 UDPSocket::UDPSocket() : m_pNetUdpSocket(NULL), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL)
00028 {
00029 
00030 }
00031 
00032 UDPSocket::~UDPSocket() //close()
00033 {
00034   close();
00035 }
00036   
00037 UDPSocketErr UDPSocket::bind(const Host& me)
00038 {
00039   UDPSocketErr udpSocketErr = checkInst();
00040   if(udpSocketErr)
00041     return udpSocketErr;
00042   return (UDPSocketErr) m_pNetUdpSocket->bind(me); 
00043 }
00044   
00045 int /*if < 0 : UDPSocketErr*/ UDPSocket::sendto(const char* buf, int len, Host* pHost)
00046 {
00047   UDPSocketErr udpSocketErr = checkInst();
00048   if(udpSocketErr)
00049     return udpSocketErr;
00050   return m_pNetUdpSocket->sendto(buf, len, pHost);  
00051 }
00052 
00053 int /*if < 0 : UDPSocketErr*/ UDPSocket::recvfrom(char* buf, int len, Host* pHost)
00054 {
00055   UDPSocketErr udpSocketErr = checkInst();
00056   if(udpSocketErr)
00057     return udpSocketErr;
00058   return m_pNetUdpSocket->recvfrom(buf, len, pHost);
00059 }
00060 
00061 UDPSocketErr UDPSocket::close()
00062 {
00063   if(!m_pNetUdpSocket)
00064     return UDPSOCKET_SETUP;
00065   m_pNetUdpSocket->resetOnEvent();
00066   UDPSocketErr udpSocketErr = (UDPSocketErr) m_pNetUdpSocket->close(); //Close (can already be closed)
00067   Net::releaseUdpSocket(m_pNetUdpSocket); //And release it so it can be freed when properly removed
00068   m_pNetUdpSocket = NULL;
00069   return udpSocketErr;
00070 }
00071 
00072 //Callbacks
00073 void UDPSocket::setOnEvent( void (*pMethod)(UDPSocketEvent) )
00074 {
00075   m_pCb = pMethod;
00076 }
00077 
00078 #if 0 //For info only
00079 template<class T> 
00080 void UDPSocket::setOnEvent( T* pItem, void (T::*pMethod)(UDPSocketEvent) )
00081 {
00082   m_pCbItem = (CDummy*) pItem;
00083   m_pCbMeth = (void (CDummy::*)(UDPSocketEvent)) pMethod;
00084 }
00085 #endif
00086   
00087 void UDPSocket::resetOnEvent() //Disable callback
00088 {
00089   m_pCb = NULL;
00090   m_pCbItem = NULL;
00091   m_pCbMeth = NULL;
00092 }
00093 
00094 void UDPSocket::onNetUdpSocketEvent(NetUdpSocketEvent e)
00095 {
00096   if(m_pCbItem && m_pCbMeth)
00097     (m_pCbItem->*m_pCbMeth)((UDPSocketEvent) e);
00098   else if(m_pCb)
00099     m_pCb((UDPSocketEvent) e);
00100 }
00101 
00102 UDPSocketErr UDPSocket::checkInst()
00103 {
00104   if(!m_pNetUdpSocket)
00105   {
00106     m_pNetUdpSocket = Net::udpSocket();
00107     if(!m_pNetUdpSocket)
00108     {
00109       return UDPSOCKET_IF; //Interface did not return a socket (usually because a default interface does not exist)
00110     }
00111     m_pNetUdpSocket->setOnEvent(this, &UDPSocket::onNetUdpSocketEvent);
00112   }
00113   return UDPSOCKET_OK;
00114 }