-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathUDPController.cpp
97 lines (79 loc) · 2.49 KB
/
UDPController.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
/*
* Copyright (C) 2021 by Jonathan Naylor G4KLX
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "UDPController.h"
#include "Log.h"
#include <cstring>
#include <cassert>
const unsigned int BUFFER_LENGTH = 600U;
CUDPController::CUDPController(const std::string& modemAddress, unsigned int modemPort, const std::string& localAddress, unsigned int localPort) :
m_socket(localAddress, localPort),
m_addr(),
m_addrLen(0U),
m_buffer(2000U, "UDP Controller Ring Buffer")
{
assert(!modemAddress.empty());
assert(modemPort > 0U);
assert(localPort > 0U);
if (CUDPSocket::lookup(modemAddress, modemPort, m_addr, m_addrLen) != 0)
m_addrLen = 0U;
}
CUDPController::~CUDPController()
{
}
bool CUDPController::open()
{
if (m_addrLen == 0U) {
LogError("Unable to resolve the address of the modem");
return false;
}
return m_socket.open(m_addr);
}
int CUDPController::read(unsigned char* buffer, unsigned int length)
{
assert(buffer != NULL);
assert(length > 0U);
unsigned char data[BUFFER_LENGTH];
sockaddr_storage addr;
unsigned int addrLen;
int ret = m_socket.read(data, BUFFER_LENGTH, addr, addrLen);
// An error occurred on the socket
if (ret < 0)
return ret;
// Add new data to the ring buffer
if (ret > 0) {
if (CUDPSocket::match(addr, m_addr))
m_buffer.addData(data, ret);
}
// Get required data from the ring buffer
unsigned int avail = m_buffer.dataSize();
if (avail < length)
length = avail;
if (length > 0U)
m_buffer.getData(buffer, length);
return int(length);
}
int CUDPController::write(const unsigned char* buffer, unsigned int length)
{
assert(buffer != NULL);
assert(length > 0U);
return m_socket.write(buffer, length, m_addr, m_addrLen) ? int(length) : -1;
}
void CUDPController::close()
{
m_socket.close();
}