UDPSocket.h

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2005 Andras Varga
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU Lesser General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with this program; if not, see <http://www.gnu.org/licenses/>.
00016 //
00017 
00018 
00019 #ifndef __INET_UDPSOCKET_H
00020 #define __INET_UDPSOCKET_H
00021 
00022 #include <omnetpp.h>
00023 #include "UDPControlInfo_m.h"
00024 
00025 
00098 class INET_API UDPSocket
00099 {
00100   public:
00109     class CallbackInterface
00110     {
00111       public:
00112         virtual ~CallbackInterface() {}
00113         virtual void socketDatagramArrived(int sockId, void *yourPtr, cMessage *msg, UDPControlInfo *ctrl) = 0;
00114         virtual void socketPeerClosed(int sockId, void *yourPtr) {}
00115     };
00116 
00117     enum State {NOT_BOUND, BOUND};  // FIXME needed?
00118 
00119   protected:
00120     int sockId;
00121     int usrId;
00122     int sockstate;
00123 
00124     IPvXAddress localAddr; // needed for sendTo() as local address
00125     int localPrt;
00126     IPvXAddress remoteAddr; // needed to enable send(msg) call
00127     int remotePrt; // needed to enable send(msg) call
00128     int mcastIfaceId;
00129 
00130     CallbackInterface *cb;
00131     void *yourPtr;
00132 
00133     cGate *gateToUdp;
00134 
00135   protected:
00136     void sendToUDP(cMessage *msg);
00137 
00138   public:
00143     UDPSocket();
00144 
00148     ~UDPSocket() {}
00149 
00153     int getSocketId() const  {return sockId;}
00154 
00159     void setUserId(int userId);
00160 
00164     int getUserId() const  {return usrId;}
00165 
00171     int getState()   {return sockstate;}
00172 
00176     static const char *stateName(int state);
00177 
00181     static int generateSocketId();
00182 
00185     IPvXAddress getLocalAddress() {return localAddr;}
00186     int getLocalPort() {return localPrt;}
00188 
00191 
00196     void setOutputGate(cGate *toUdp)  {gateToUdp = toUdp;}
00197 
00201     void bind(int localPort);
00202 
00207     void bind(IPvXAddress localAddr, int localPort);
00208 
00214     void connect(IPvXAddress remoteAddr, int remotePort);
00215 
00221     void setMulticastInterfaceId(int interfaceId) {mcastIfaceId = interfaceId;}
00222 
00226     int getMulticastInterfaceId() const {return mcastIfaceId;}
00227 
00231     void sendTo(cMessage *msg, IPvXAddress destAddr, int destPort);
00232 
00237     void send(cMessage *msg);
00238 
00242     void close();
00244 
00252     bool belongsToSocket(cMessage *msg);
00253 
00259     static bool belongsToAnyUDPSocket(cMessage *msg);
00260 
00282     void setCallbackObject(CallbackInterface *cb, void *yourPtr=NULL);
00283 
00294     void processMessage(cMessage *msg);
00296 };
00297 
00298 #endif
00299 
00300