INET Framework for OMNeT++/OMNEST
inet::SocketBase Class Reference

#include <SocketBase.h>

Inheritance diagram for inet::SocketBase:
inet::ISocket inet::EthernetSocket inet::Ieee8021qSocket inet::Ieee8022LlcSocket

Public Member Functions

 SocketBase ()
 
virtual ~SocketBase ()
 
Setting up a socket
void setOutputGate (cGate *gate)
 Sets the gate on which to send messages. More...
 
int getSocketId () const override
 Returns the internal socket Id. More...
 
Opening and closing connections, sending data
virtual void send (Packet *packet) override
 Sends a data packet to the address and port specified previously in a connect() call. More...
 
virtual bool isOpen () const override
 Returns true if the socket is open. More...
 
virtual void close () override
 Unbinds the socket. More...
 
virtual void destroy () override
 Notify the protocol that the owner of ISocket has destroyed the socket. More...
 
Handling of messages
virtual bool belongsToSocket (cMessage *msg) const override
 Returns true if the message belongs to this socket instance. More...
 
User data
void * getUserData () const
 
void setUserData (void *userData)
 
- Public Member Functions inherited from inet::ISocket
virtual ~ISocket ()
 
virtual void processMessage (cMessage *msg)=0
 Examines the message, takes ownership, and updates socket state. More...
 

Protected Member Functions

virtual void sendOut (cMessage *msg)
 

Protected Attributes

cGate * outputGate = nullptr
 
int socketId = -1
 
bool isOpen_ = false
 
void * userData = nullptr
 

Constructor & Destructor Documentation

◆ SocketBase()

inet::SocketBase::SocketBase ( )
17 {
18  // don't allow user-specified socketIds because they may conflict with
19  // automatically assigned ones.
20  socketId = getEnvir()->getUniqueNumber();
21  outputGate = nullptr;
22 }

◆ ~SocketBase()

virtual inet::SocketBase::~SocketBase ( )
inlinevirtual
28 {}

Member Function Documentation

◆ belongsToSocket()

bool inet::SocketBase::belongsToSocket ( cMessage *  msg) const
overridevirtual

Returns true if the message belongs to this socket instance.

Implements inet::ISocket.

62 {
63  auto& tags = check_and_cast<ITaggedObject *>(msg)->getTags();
64  const auto& socketInd = tags.findTag<SocketInd>();
65  return socketInd != nullptr && socketInd->getSocketId() == socketId;
66 }

Referenced by inet::EthernetSocketIo::handleMessageWhenUp(), inet::Ieee8022LlcSocketIo::handleMessageWhenUp(), inet::Ieee8022LlcSocket::processMessage(), inet::EthernetSocket::processMessage(), and inet::Ieee8021qSocket::processMessage().

◆ close()

void inet::SocketBase::close ( )
overridevirtual

Unbinds the socket.

Once closed, a closed socket may be bound to another (or the same) port, and reused.

Implements inet::ISocket.

44 {
45  auto request = new Request("CLOSE", SOCKET_C_CLOSE);
46  auto *ctrl = new SocketCloseCommand();
47  request->setControlInfo(ctrl);
48  isOpen_ = true;
49  sendOut(request);
50 }

Referenced by inet::EthernetSocketIo::handleStopOperation(), inet::EtherAppServer::handleStopOperation(), inet::Ieee8022LlcSocketIo::handleStopOperation(), inet::EtherTrafGen::handleStopOperation(), and inet::EtherAppClient::handleStopOperation().

◆ destroy()

void inet::SocketBase::destroy ( )
overridevirtual

Notify the protocol that the owner of ISocket has destroyed the socket.

Typically used when the owner of ISocket has crashed.

Implements inet::ISocket.

53 {
54  auto request = new Request("destroy", SOCKET_C_DESTROY);
55  auto *ctrl = new SocketDestroyCommand();
56  request->setControlInfo(ctrl);
57  sendOut(request);
58  isOpen_ = false;
59 }

Referenced by inet::EthernetSocketIo::handleCrashOperation(), inet::EtherAppServer::handleCrashOperation(), inet::Ieee8022LlcSocketIo::handleCrashOperation(), inet::EtherTrafGen::handleCrashOperation(), and inet::EtherAppClient::handleCrashOperation().

◆ getSocketId()

int inet::SocketBase::getSocketId ( ) const
inlineoverridevirtual

Returns the internal socket Id.

Implements inet::ISocket.

41 { return socketId; }

◆ getUserData()

void* inet::SocketBase::getUserData ( ) const
inline
80 { return userData; }

◆ isOpen()

virtual bool inet::SocketBase::isOpen ( ) const
inlineoverridevirtual

Returns true if the socket is open.

Implements inet::ISocket.

55 { return isOpen_; }

Referenced by inet::EtherAppServer::socketClosed(), and inet::EtherAppClient::socketClosed().

◆ send()

void inet::SocketBase::send ( Packet packet)
overridevirtual

Sends a data packet to the address and port specified previously in a connect() call.

Implements inet::ISocket.

37 {
38  packet->setKind(SOCKET_C_DATA);
39  isOpen_ = true;
40  sendOut(packet);
41 }

Referenced by inet::EthernetSocketIo::handleMessageWhenUp(), inet::Ieee8022LlcSocketIo::handleMessageWhenUp(), inet::EtherAppServer::sendPacket(), and inet::EtherAppClient::sendPacket().

◆ sendOut()

void inet::SocketBase::sendOut ( cMessage *  msg)
protectedvirtual

Reimplemented in inet::Ieee8021qSocket, inet::EthernetSocket, and inet::Ieee8022LlcSocket.

25 {
26  if (!isOpen_)
27  throw cRuntimeError("Socket is closed");
28  if (!outputGate)
29  throw cRuntimeError("Socket output gate is not set");
30  EV_DEBUG << "Sending message" << EV_FIELD(msg) << EV_ENDL;
31  auto& tags = check_and_cast<ITaggedObject *>(msg)->getTags();
32  tags.addTagIfAbsent<SocketReq>()->setSocketId(socketId);
33  check_and_cast<cSimpleModule *>(outputGate->getOwnerModule())->send(msg, outputGate);
34 }

Referenced by close(), destroy(), send(), inet::Ieee8022LlcSocket::sendOut(), inet::EthernetSocket::sendOut(), and inet::Ieee8021qSocket::sendOut().

◆ setOutputGate()

void inet::SocketBase::setOutputGate ( cGate *  gate)
inline

Sets the gate on which to send messages.

Must be invoked before socket can be used. Example: socket.setOutputGate(gate("out"));

36 { this->outputGate = gate; }

Referenced by inet::EthernetSocketIo::handleStartOperation(), inet::Ieee8022LlcSocketIo::handleStartOperation(), inet::EtherAppServer::initialize(), inet::EtherAppClient::initialize(), and inet::EtherTrafGen::initialize().

◆ setUserData()

void inet::SocketBase::setUserData ( void *  userData)
inline
81 { this->userData = userData; }

Member Data Documentation

◆ isOpen_

◆ outputGate

cGate* inet::SocketBase::outputGate = nullptr
protected

Referenced by sendOut(), and SocketBase().

◆ socketId

int inet::SocketBase::socketId = -1
protected

◆ userData

void* inet::SocketBase::userData = nullptr
protected

The documentation for this class was generated from the following files:
inet::SocketBase::socketId
int socketId
Definition: SocketBase.h:19
inet::SOCKET_C_CLOSE
@ SOCKET_C_CLOSE
Definition: SocketCommand_m.h:67
inet::SocketBase::userData
void * userData
Definition: SocketBase.h:21
ctrl
removed ctrl
Definition: IUdp-gates.txt:7
EV_FIELD
#define EV_FIELD(...)
Definition: INETDefs.h:112
inet::SocketBase::isOpen_
bool isOpen_
Definition: SocketBase.h:20
inet::SocketBase::outputGate
cGate * outputGate
Definition: SocketBase.h:18
inet::SOCKET_C_DATA
@ SOCKET_C_DATA
Definition: SocketCommand_m.h:64
tags
* tags
Definition: IUdp-gates.txt:3
inet::SOCKET_C_DESTROY
@ SOCKET_C_DESTROY
Definition: SocketCommand_m.h:68
inet::SocketBase::sendOut
virtual void sendOut(cMessage *msg)
Definition: SocketBase.cc:24
EV_ENDL
#define EV_ENDL
Definition: INETDefs.h:114