-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.cc
More file actions
59 lines (51 loc) · 1.5 KB
/
Copy pathclient.cc
File metadata and controls
59 lines (51 loc) · 1.5 KB
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
#include "client.h"
#include <vector>
#include <cstdint>
bool Client::IsAlive()
{
return alive.load(std::memory_order_acquire);
}
void Client::QueueToTcpLoop()
{
while(alive.load(std::memory_order_acquire) && conn && conn->Valid())
{
Frame::EthFrame frame;
if (!out_queue.Pop(frame)) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
continue;
}
conn->Send(frame.data(), frame.size());
}
}
void Client::PushToOutQueue(const Frame::EthFrame& frame)
{
out_queue.Push(frame);
}
void Client::TcpToCanLoop(SocketCan* can)
{
std::vector<uint8_t> inbuf;
inbuf.reserve(4096);
uint8_t tmp[2048];
while (alive.load(std::memory_order_acquire) && conn && conn->Valid())
{
ssize_t r = conn->Recv(tmp, sizeof(tmp));
if (r == 0) break;
if (r < 0) {
if (errno == EINTR) continue;
break;
}
inbuf.insert(inbuf.end(), tmp, tmp + r);
while (inbuf.size() >= PACKED_FRAME_SIZE)
{
Frame::EthFrame pf{};
std::memcpy(pf.data(), inbuf.data(), PACKED_FRAME_SIZE);
inbuf.erase(inbuf.begin(), inbuf.begin() + PACKED_FRAME_SIZE);
Frame::CanFrame cf{};
if (!Frame::Unpack(pf, cf)) continue;
//(*n_frames)++;
if (!can->Write(cf)) { alive.store(false, std::memory_order_release); break; }
}
}
alive.store(false, std::memory_order_release);
if (conn) conn->ShutdownRD();
}