Darran Shepherd
/
AutoIpNetStack
Net stack with AutoIP enabled
Embed:
(wiki syntax)
Show/hide line numbers
UDPSocket.h
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 #ifndef UDPSOCKET_H 00025 #define UDPSOCKET_H 00026 00027 #include "if/net/net.h" 00028 //Essentially it is a safe interface to NetUdpSocket 00029 00030 enum UDPSocketErr 00031 { 00032 __UDPSOCKET_MIN = -0xFFFF, 00033 UDPSOCKET_SETUP, //NetUdpSocket not properly configured 00034 UDPSOCKET_IF, //If has problems, does not exist or is not initialized 00035 UDPSOCKET_MEM, //Not enough mem 00036 UDPSOCKET_INUSE, //If/Port is in use 00037 //... 00038 UDPSOCKET_OK = 0 00039 }; 00040 00041 enum UDPSocketEvent //Only one lonely event here... but who knows, maybe some day there'll be another one! 00042 { 00043 UDPSOCKET_READABLE, //Data in buf 00044 }; 00045 00046 00047 class UDPSocket 00048 { 00049 public: 00050 UDPSocket(); 00051 ~UDPSocket(); //close() 00052 00053 UDPSocketErr bind(const Host& me); 00054 00055 int /*if < 0 : UDPSocketErr*/ sendto(const char* buf, int len, Host* pHost); 00056 int /*if < 0 : UDPSocketErr*/ recvfrom(char* buf, int len, Host* pHost); 00057 00058 /* TODO NTH : printf / scanf helpers that call send/recv */ 00059 00060 UDPSocketErr close(); 00061 00062 class CDummy; 00063 //Callbacks 00064 void setOnEvent( void (*pMethod)(UDPSocketEvent) ); 00065 template<class T> 00066 void setOnEvent( T* pItem, void (T::*pMethod)(UDPSocketEvent) ) 00067 { 00068 m_pCbItem = (CDummy*) pItem; 00069 m_pCbMeth = (void (CDummy::*)(UDPSocketEvent)) pMethod; 00070 } 00071 00072 void resetOnEvent(); //Disable callback 00073 00074 protected: 00075 void onNetUdpSocketEvent(NetUdpSocketEvent e); 00076 UDPSocketErr checkInst(); 00077 00078 private: 00079 NetUdpSocket* m_pNetUdpSocket; 00080 00081 CDummy* m_pCbItem; 00082 void (CDummy::*m_pCbMeth)(UDPSocketEvent); 00083 00084 void (*m_pCb)(UDPSocketEvent); 00085 00086 }; 00087 00088 #endif
Generated on Tue Jul 12 2022 15:37:05 by 1.7.2