-
Notifications
You must be signed in to change notification settings - Fork 4
/
Connection.inl
96 lines (84 loc) · 3.37 KB
/
Connection.inl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2021 Rainer Schoenberger
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
namespace PjonHL
{
template<class Strategy>
Connection<Strategy>::Connection(Address f_remoteAddress, Address f_remoteMask, Address f_localAddress, Address f_localMask, Bus<Strategy> & f_pjonHL) :
m_remoteAddress(f_remoteAddress),
m_remoteMask(f_remoteMask),
m_localAddress(f_localAddress),
m_localMask(f_localMask),
m_pjonHL(f_pjonHL),
m_active(true)
{
}
template<class Strategy>
std::future<Result> Connection<Strategy>::send(const std::vector<uint8_t> && f_payload, uint32_t f_timeout_milliseconds, bool f_enableRetransmit)
{
std::lock_guard<std::mutex> guard(m_activityMutex);
if(not m_active)
{
std::promise<Result> promise;
promise.set_value(Result(std::string("Connection not active (is Bus instance still alive?)")));
return promise.get_future();
}
// TODO: I hope m_localAddress means to PJON what I think it means?
return m_pjonHL.send(m_localAddress, m_remoteAddress, f_payload, f_timeout_milliseconds, f_enableRetransmit);
}
template<class Strategy>
Expect< ReceivedPacket > Connection<Strategy>::receive(uint32_t f_timeout_milliseconds)
{
std::unique_lock<std::mutex> guardActivity(m_activityMutex);
if(not m_active)
{
return Expect< ReceivedPacket >();
}
std::unique_lock<std::mutex> guardRxQueue(m_rxQueueMutex);
bool dataAvailable = m_rxQueueCondition.wait_for(
guardRxQueue,
std::chrono::milliseconds(f_timeout_milliseconds),
[this]{return m_rxQueue.size()>0;}
);
if(dataAvailable)
{
auto rxPacket = std::move(m_rxQueue.front());
m_rxQueue.pop();
return Expect< ReceivedPacket >{std::move(rxPacket)};
}
return Expect<ReceivedPacket>{};
}
template<class Strategy>
void Connection<Strategy>::addReceivedPacket(std::vector<uint8_t> && f_payload, Address f_remoteAddress, Address f_targetAddress)
{
// NOTE: not locking m_activityMutex here
// - to avoid problems with condition variable.
// - To allow safe destruction if connection (if we would lock
// activity here we can deadlock if destructor of Handle runs and
// Bus is trying to deliver packet to this connection at same time)
// This function is hence not thread safe with respect to destruction
// of Bus.
// This works however, as only Busis calling it and it makes sure to
// de-register before.
// TODO: handle remote address here
std::unique_lock<std::mutex> guardRxQueue(m_rxQueueMutex);
m_rxQueue.push(ReceivedPacket(std::move(f_payload), f_remoteAddress, f_targetAddress));
m_rxQueueCondition.notify_all();
}
template<class Strategy>
void Connection<Strategy>::setInactive()
{
std::lock_guard<std::mutex> guard(m_activityMutex);
m_active = false;
}
}