Experimental HTTPClient with proxy support
api/UDPSocket.cpp@0:b56b6a05cad4, 2011-06-29 (annotated)
- Committer:
- igorsk
- Date:
- Wed Jun 29 16:01:58 2011 +0000
- Revision:
- 0:b56b6a05cad4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
igorsk | 0:b56b6a05cad4 | 1 | |
igorsk | 0:b56b6a05cad4 | 2 | /* |
igorsk | 0:b56b6a05cad4 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
igorsk | 0:b56b6a05cad4 | 4 | |
igorsk | 0:b56b6a05cad4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
igorsk | 0:b56b6a05cad4 | 6 | of this software and associated documentation files (the "Software"), to deal |
igorsk | 0:b56b6a05cad4 | 7 | in the Software without restriction, including without limitation the rights |
igorsk | 0:b56b6a05cad4 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
igorsk | 0:b56b6a05cad4 | 9 | copies of the Software, and to permit persons to whom the Software is |
igorsk | 0:b56b6a05cad4 | 10 | furnished to do so, subject to the following conditions: |
igorsk | 0:b56b6a05cad4 | 11 | |
igorsk | 0:b56b6a05cad4 | 12 | The above copyright notice and this permission notice shall be included in |
igorsk | 0:b56b6a05cad4 | 13 | all copies or substantial portions of the Software. |
igorsk | 0:b56b6a05cad4 | 14 | |
igorsk | 0:b56b6a05cad4 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
igorsk | 0:b56b6a05cad4 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
igorsk | 0:b56b6a05cad4 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
igorsk | 0:b56b6a05cad4 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
igorsk | 0:b56b6a05cad4 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
igorsk | 0:b56b6a05cad4 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
igorsk | 0:b56b6a05cad4 | 21 | THE SOFTWARE. |
igorsk | 0:b56b6a05cad4 | 22 | */ |
igorsk | 0:b56b6a05cad4 | 23 | |
igorsk | 0:b56b6a05cad4 | 24 | #include "UDPSocket.h" |
igorsk | 0:b56b6a05cad4 | 25 | #include "if/net/netudpsocket.h" |
igorsk | 0:b56b6a05cad4 | 26 | |
igorsk | 0:b56b6a05cad4 | 27 | UDPSocket::UDPSocket() : m_pNetUdpSocket(NULL), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL) |
igorsk | 0:b56b6a05cad4 | 28 | { |
igorsk | 0:b56b6a05cad4 | 29 | |
igorsk | 0:b56b6a05cad4 | 30 | } |
igorsk | 0:b56b6a05cad4 | 31 | |
igorsk | 0:b56b6a05cad4 | 32 | UDPSocket::~UDPSocket() //close() |
igorsk | 0:b56b6a05cad4 | 33 | { |
igorsk | 0:b56b6a05cad4 | 34 | close(); |
igorsk | 0:b56b6a05cad4 | 35 | } |
igorsk | 0:b56b6a05cad4 | 36 | |
igorsk | 0:b56b6a05cad4 | 37 | UDPSocketErr UDPSocket::bind(const Host& me) |
igorsk | 0:b56b6a05cad4 | 38 | { |
igorsk | 0:b56b6a05cad4 | 39 | UDPSocketErr udpSocketErr = checkInst(); |
igorsk | 0:b56b6a05cad4 | 40 | if(udpSocketErr) |
igorsk | 0:b56b6a05cad4 | 41 | return udpSocketErr; |
igorsk | 0:b56b6a05cad4 | 42 | return (UDPSocketErr) m_pNetUdpSocket->bind(me); |
igorsk | 0:b56b6a05cad4 | 43 | } |
igorsk | 0:b56b6a05cad4 | 44 | |
igorsk | 0:b56b6a05cad4 | 45 | int /*if < 0 : UDPSocketErr*/ UDPSocket::sendto(const char* buf, int len, Host* pHost) |
igorsk | 0:b56b6a05cad4 | 46 | { |
igorsk | 0:b56b6a05cad4 | 47 | UDPSocketErr udpSocketErr = checkInst(); |
igorsk | 0:b56b6a05cad4 | 48 | if(udpSocketErr) |
igorsk | 0:b56b6a05cad4 | 49 | return udpSocketErr; |
igorsk | 0:b56b6a05cad4 | 50 | return m_pNetUdpSocket->sendto(buf, len, pHost); |
igorsk | 0:b56b6a05cad4 | 51 | } |
igorsk | 0:b56b6a05cad4 | 52 | |
igorsk | 0:b56b6a05cad4 | 53 | int /*if < 0 : UDPSocketErr*/ UDPSocket::recvfrom(char* buf, int len, Host* pHost) |
igorsk | 0:b56b6a05cad4 | 54 | { |
igorsk | 0:b56b6a05cad4 | 55 | UDPSocketErr udpSocketErr = checkInst(); |
igorsk | 0:b56b6a05cad4 | 56 | if(udpSocketErr) |
igorsk | 0:b56b6a05cad4 | 57 | return udpSocketErr; |
igorsk | 0:b56b6a05cad4 | 58 | return m_pNetUdpSocket->recvfrom(buf, len, pHost); |
igorsk | 0:b56b6a05cad4 | 59 | } |
igorsk | 0:b56b6a05cad4 | 60 | |
igorsk | 0:b56b6a05cad4 | 61 | UDPSocketErr UDPSocket::close() |
igorsk | 0:b56b6a05cad4 | 62 | { |
igorsk | 0:b56b6a05cad4 | 63 | if(!m_pNetUdpSocket) |
igorsk | 0:b56b6a05cad4 | 64 | return UDPSOCKET_SETUP; |
igorsk | 0:b56b6a05cad4 | 65 | m_pNetUdpSocket->resetOnEvent(); |
igorsk | 0:b56b6a05cad4 | 66 | UDPSocketErr udpSocketErr = (UDPSocketErr) m_pNetUdpSocket->close(); //Close (can already be closed) |
igorsk | 0:b56b6a05cad4 | 67 | Net::releaseUdpSocket(m_pNetUdpSocket); //And release it so it can be freed when properly removed |
igorsk | 0:b56b6a05cad4 | 68 | m_pNetUdpSocket = NULL; |
igorsk | 0:b56b6a05cad4 | 69 | return udpSocketErr; |
igorsk | 0:b56b6a05cad4 | 70 | } |
igorsk | 0:b56b6a05cad4 | 71 | |
igorsk | 0:b56b6a05cad4 | 72 | //Callbacks |
igorsk | 0:b56b6a05cad4 | 73 | void UDPSocket::setOnEvent( void (*pMethod)(UDPSocketEvent) ) |
igorsk | 0:b56b6a05cad4 | 74 | { |
igorsk | 0:b56b6a05cad4 | 75 | m_pCb = pMethod; |
igorsk | 0:b56b6a05cad4 | 76 | } |
igorsk | 0:b56b6a05cad4 | 77 | |
igorsk | 0:b56b6a05cad4 | 78 | #if 0 //For info only |
igorsk | 0:b56b6a05cad4 | 79 | template<class T> |
igorsk | 0:b56b6a05cad4 | 80 | void UDPSocket::setOnEvent( T* pItem, void (T::*pMethod)(UDPSocketEvent) ) |
igorsk | 0:b56b6a05cad4 | 81 | { |
igorsk | 0:b56b6a05cad4 | 82 | m_pCbItem = (CDummy*) pItem; |
igorsk | 0:b56b6a05cad4 | 83 | m_pCbMeth = (void (CDummy::*)(UDPSocketEvent)) pMethod; |
igorsk | 0:b56b6a05cad4 | 84 | } |
igorsk | 0:b56b6a05cad4 | 85 | #endif |
igorsk | 0:b56b6a05cad4 | 86 | |
igorsk | 0:b56b6a05cad4 | 87 | void UDPSocket::resetOnEvent() //Disable callback |
igorsk | 0:b56b6a05cad4 | 88 | { |
igorsk | 0:b56b6a05cad4 | 89 | m_pCb = NULL; |
igorsk | 0:b56b6a05cad4 | 90 | m_pCbItem = NULL; |
igorsk | 0:b56b6a05cad4 | 91 | m_pCbMeth = NULL; |
igorsk | 0:b56b6a05cad4 | 92 | } |
igorsk | 0:b56b6a05cad4 | 93 | |
igorsk | 0:b56b6a05cad4 | 94 | void UDPSocket::onNetUdpSocketEvent(NetUdpSocketEvent e) |
igorsk | 0:b56b6a05cad4 | 95 | { |
igorsk | 0:b56b6a05cad4 | 96 | if(m_pCbItem && m_pCbMeth) |
igorsk | 0:b56b6a05cad4 | 97 | (m_pCbItem->*m_pCbMeth)((UDPSocketEvent) e); |
igorsk | 0:b56b6a05cad4 | 98 | else if(m_pCb) |
igorsk | 0:b56b6a05cad4 | 99 | m_pCb((UDPSocketEvent) e); |
igorsk | 0:b56b6a05cad4 | 100 | } |
igorsk | 0:b56b6a05cad4 | 101 | |
igorsk | 0:b56b6a05cad4 | 102 | UDPSocketErr UDPSocket::checkInst() |
igorsk | 0:b56b6a05cad4 | 103 | { |
igorsk | 0:b56b6a05cad4 | 104 | if(!m_pNetUdpSocket) |
igorsk | 0:b56b6a05cad4 | 105 | { |
igorsk | 0:b56b6a05cad4 | 106 | m_pNetUdpSocket = Net::udpSocket(); |
igorsk | 0:b56b6a05cad4 | 107 | if(!m_pNetUdpSocket) |
igorsk | 0:b56b6a05cad4 | 108 | { |
igorsk | 0:b56b6a05cad4 | 109 | return UDPSOCKET_IF; //Interface did not return a socket (usually because a default interface does not exist) |
igorsk | 0:b56b6a05cad4 | 110 | } |
igorsk | 0:b56b6a05cad4 | 111 | m_pNetUdpSocket->setOnEvent(this, &UDPSocket::onNetUdpSocketEvent); |
igorsk | 0:b56b6a05cad4 | 112 | } |
igorsk | 0:b56b6a05cad4 | 113 | return UDPSOCKET_OK; |
igorsk | 0:b56b6a05cad4 | 114 | } |