-
Notifications
You must be signed in to change notification settings - Fork 167
/
udp_receiver.cpp
106 lines (88 loc) 路 2.89 KB
/
udp_receiver.cpp
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
97
98
99
100
101
102
103
104
105
106
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2019 Continental Corporation
*
* 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.
*
* ========================= eCAL LICENSE =================================
*/
/**
* @brief UDP receiver class
**/
#include <iostream>
#include "udp_receiver.h"
#include <ecal/ecal_config.h>
#include "io/udp_receiver_base.h"
#include "io/udp_receiver_asio.h"
#ifdef ECAL_NPCAP_SUPPORT
#include "io/udp_receiver_npcap.h"
#endif
namespace eCAL
{
////////////////////////////////////////////////////////
// udp receiver class
////////////////////////////////////////////////////////
CUDPReceiver::CUDPReceiver()
: CReceiver(CReceiver::SType_ReceiverUDP)
, m_use_npcap(false)
{
#ifdef ECAL_NPCAP_SUPPORT
if (Config::IsNpcapEnabled())
{
m_use_npcap = Udpcap::Initialize(); // Only use NPCAP if we can initialize it (or it has already been initialized successfully)
if (!m_use_npcap)
{
std::cerr << "Npcap is enabled, but cannot be initialized. Using socket fallback mode." << std::endl;
}
}
#endif //ECAL_NPCAP_SUPPORT
}
bool CUDPReceiver::Create(const SReceiverAttr& attr_)
{
if (m_socket_impl) return false;
#ifdef ECAL_NPCAP_SUPPORT
if (m_use_npcap)
{
m_socket_impl = std::make_shared<CUDPReceiverPcap>(attr_);
return true;
}
#endif // ECAL_NPCAP_SUPPORT
m_socket_impl = std::make_shared<CUDPReceiverAsio>(attr_);
return(true);
}
bool CUDPReceiver::Destroy()
{
if (!m_socket_impl) return(false);
const std::lock_guard<std::mutex> lock(m_socket_mtx);
m_socket_impl.reset();
return(true);
}
bool CUDPReceiver::AddMultiCastGroup(const char* ipaddr_)
{
if (!m_socket_impl) return(false);
const std::lock_guard<std::mutex> lock(m_socket_mtx);
return(m_socket_impl->AddMultiCastGroup(ipaddr_));
}
bool CUDPReceiver::RemMultiCastGroup(const char* ipaddr_)
{
if (!m_socket_impl) return(false);
const std::lock_guard<std::mutex> lock(m_socket_mtx);
return(m_socket_impl->RemMultiCastGroup(ipaddr_));
}
size_t CUDPReceiver::Receive(char* buf_, size_t len_, int timeout_, ::sockaddr_in* address_ /* = nullptr */)
{
if (!m_socket_impl) return(0);
const std::lock_guard<std::mutex> lock(m_socket_mtx);
return(m_socket_impl->Receive(buf_, len_, timeout_, address_));
}
}