YARP
Yet Another Robot Platform
PortCorePackets.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
11 
13 
16 
17 namespace {
18 YARP_OS_LOG_COMPONENT(PORTCOREPACKETS, "yarp.os.impl.PortCorePackets")
19 } // namespace
20 
21 PortCorePackets::~PortCorePackets()
22 {
23  while (!inactive.empty()) {
24  delete inactive.back();
25  inactive.pop_back();
26  }
27  while (!active.empty()) {
28  delete active.back();
29  active.pop_back();
30  }
31 }
32 
34 {
35  return active.size();
36 }
37 
39 {
40  if (inactive.empty()) {
41  PortCorePacket* obj = nullptr;
42  obj = new PortCorePacket();
43  yCAssert(PORTCOREPACKETS, obj != nullptr);
44  inactive.push_back(obj);
45  }
46  PortCorePacket* next = inactive.front();
47  if (next == nullptr) {
48  yCError(PORTCOREPACKETS, "*** YARP consistency check failed.\n");
49  yCError(PORTCOREPACKETS, "*** There has been a low-level failure in \"PortCorePackets\".\n");
50  yCError(PORTCOREPACKETS, "*** This typically occurs when ports are accessed in a non-threadsafe way.\n");
51  yCError(PORTCOREPACKETS, "*** For help: https://github.com/robotology/yarp/issues/new\n");
52  }
53  yCAssert(PORTCOREPACKETS, next != nullptr);
54  inactive.remove(next);
55  active.push_back(next);
56  return next;
57 }
58 
60 {
61  if (packet != nullptr) {
62  if (clear) {
63  packet->reset();
64  }
65  packet->completed = true;
66  active.remove(packet);
67  inactive.push_back(packet);
68  }
69 }
70 
72 {
73  if (packet != nullptr) {
74  if (packet->getCount() <= 0) {
75  packet->complete();
76  return true;
77  }
78  }
79  return false;
80 }
81 
83 {
84  if (packet != nullptr) {
85  if (packet->getCount() <= 0) {
86  packet->complete();
87  freePacket(packet);
88  return true;
89  }
90  }
91  return false;
92 }
PortCorePackets.h
yarp::os::impl::PortCorePackets::checkPacket
bool checkPacket(PortCorePacket *packet)
Move a packet to the inactive state if it has finished being sent on all connections.
Definition: PortCorePackets.cpp:82
LogComponent.h
yarp::os::impl::PortCorePacket
A single message, potentially being transmitted on multiple connections.
Definition: PortCorePacket.h:25
yarp::os::impl::PortCorePacket::reset
void reset()
Delete anything we own and enter a clean state, as if freshly created.
Definition: PortCorePacket.h:125
yarp::os::impl::PortCorePackets::freePacket
void freePacket(PortCorePacket *packet, bool clear=true)
Force the given packet into an inactive state.
Definition: PortCorePackets.cpp:59
yarp::os::impl::PortCorePacket::completed
bool completed
has a notification of completion been sent
Definition: PortCorePacket.h:34
yarp::os::impl::PortCorePacket::getCount
int getCount()
Definition: PortCorePacket.h:64
yarp::os::impl::PortCorePackets::getFreePacket
PortCorePacket * getFreePacket()
Get a packet that we can prepare for sending.
Definition: PortCorePackets.cpp:38
yCAssert
#define yCAssert(component, x)
Definition: LogComponent.h:172
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
yarp::os::impl::PortCorePackets::completePacket
bool completePacket(PortCorePacket *packet)
Send a completion notification if a packet has finished being sent on all connections.
Definition: PortCorePackets.cpp:71
yarp::os::impl::PortCorePackets
A collection of messages being transmitted over connections.
Definition: PortCorePackets.h:29
YARP_OS_LOG_COMPONENT
#define YARP_OS_LOG_COMPONENT(name, name_string)
Definition: LogComponent.h:37
yarp::os::impl::PortCorePackets::getCount
size_t getCount()
Definition: PortCorePackets.cpp:33
yarp::os::impl::PortCorePacket::complete
void complete()
Send a completion notification if we haven't already, and there's somewhere to send it to.
Definition: PortCorePacket.h:145