TCPSocket.h

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2004 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_TCPSOCKET_H
00020 #define __INET_TCPSOCKET_H
00021 
00022 #include <omnetpp.h>
00023 #include "TCPCommand_m.h"
00024 #include "IPvXAddress.h"
00025 
00026 class TCPStatusInfo;
00027 
00028 
00123 class INET_API TCPSocket
00124 {
00125   public:
00134     class CallbackInterface
00135     {
00136       public:
00137         virtual ~CallbackInterface() {}
00138         virtual void socketDataArrived(int connId, void *yourPtr, cPacket *msg, bool urgent) = 0;
00139         virtual void socketEstablished(int connId, void *yourPtr) {}
00140         virtual void socketPeerClosed(int connId, void *yourPtr) {}
00141         virtual void socketClosed(int connId, void *yourPtr) {}
00142         virtual void socketFailure(int connId, void *yourPtr, int code) {}
00143         virtual void socketStatusArrived(int connId, void *yourPtr, TCPStatusInfo *status) {delete status;}
00144     };
00145 
00146     enum State {NOT_BOUND, BOUND, LISTENING, CONNECTING, CONNECTED, PEER_CLOSED, LOCALLY_CLOSED, CLOSED, SOCKERROR};
00147 
00148   protected:
00149     int connId;
00150     int sockstate;
00151 
00152     IPvXAddress localAddr;
00153     int localPrt;
00154     IPvXAddress remoteAddr;
00155     int remotePrt;
00156 
00157     CallbackInterface *cb;
00158     void *yourPtr;
00159 
00160     cGate *gateToTcp;
00161 
00162     std::string sendQueueClass;
00163     std::string receiveQueueClass;
00164     std::string tcpAlgorithmClass;
00165 
00166 
00167   protected:
00168     void sendToTCP(cMessage *msg);
00169 
00170     // internal: implementation behind listen() and listenOnce()
00171     void listen(bool fork);
00172 
00173   public:
00178     TCPSocket();
00179 
00185     TCPSocket(cMessage *msg);
00186 
00190     ~TCPSocket() {}
00191 
00197     int getConnectionId() const  {return connId;}
00198 
00204     int getState()   {return sockstate;}
00205 
00209     static const char *stateName(int state);
00210 
00213     IPvXAddress getLocalAddress() {return localAddr;}
00214     int getLocalPort() {return localPrt;}
00215     IPvXAddress getRemoteAddress() {return remoteAddr;}
00216     int getRemotePort() {return remotePrt;}
00218 
00221 
00226     void setOutputGate(cGate *toTcp)  {gateToTcp = toTcp;}
00227 
00231     void bind(int localPort);
00232 
00237     void bind(IPvXAddress localAddr, int localPort);
00238 
00242     const char *getSendQueueClass() const {return sendQueueClass.c_str();}
00243 
00247     const char *getReceiveQueueClass() const {return receiveQueueClass.c_str();}
00248 
00252     const char *getTCPAlgorithmClass() const {return tcpAlgorithmClass.c_str();}
00253 
00257     void setSendQueueClass(const char *sendQueueClass) { this->sendQueueClass = sendQueueClass; }
00258 
00262     void setReceiveQueueClass(const char *receiveQueueClass) { this->receiveQueueClass = receiveQueueClass; }
00263 
00267     void setTCPAlgorithmClass(const char *tcpAlgorithmClass) { this->tcpAlgorithmClass = tcpAlgorithmClass; }
00268 
00281     void listen()  {listen(true);}
00282 
00290     void listenOnce()  {listen(false);}
00291 
00295     void connect(IPvXAddress remoteAddr, int remotePort);
00296 
00300     void send(cMessage *msg);
00301 
00308     void close();
00309 
00313     void abort();
00314 
00322     void requestStatus();
00323 
00341     void renewSocket();
00343 
00351     bool belongsToSocket(cMessage *msg);
00352 
00358     static bool belongsToAnyTCPSocket(cMessage *msg);
00359 
00381     void setCallbackObject(CallbackInterface *cb, void *yourPtr=NULL);
00382 
00399     void processMessage(cMessage *msg);
00401 };
00402 
00403 #endif
00404 
00405