Skip to content

Commit 44bfa57

Browse files
committed
UDPSocket: sendto and bcast taking both a send callback and an error callback and updates in that regard elsewhere in the OS. UDP: Socket is now key into error_callbacks_ map. Socket: Added hash function so that this can be a key into a map
1 parent a9685ba commit 44bfa57

7 files changed

Lines changed: 50 additions & 44 deletions

File tree

api/net/ip4/udp.hpp

100755100644
Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,14 @@ namespace net {
4444
using Packet_ptr = std::unique_ptr<PacketUDP, std::default_delete<net::Packet>>;
4545
using Stack = IP4::Stack;
4646

47-
using Dest_tuple = std::pair<addr_t, port_t>;
48-
49-
typedef delegate<void(Error&)> sendto_handler;
50-
51-
struct pair_hash {
52-
template<class T1, class T2>
53-
std::size_t operator () (const std::pair<T1, T2> &p) const {
54-
auto h1 = std::hash<T1>{}(p.first);
55-
auto h2 = std::hash<T2>{}(p.second);
56-
return h1 ^ h2;
57-
}
58-
};
47+
typedef delegate<void()> sendto_handler;
48+
typedef delegate<void(Error&)> error_handler;
5949

6050
// write buffer for sendq
6151
struct WriteBuffer
6252
{
6353
WriteBuffer(
64-
const uint8_t* data, size_t length, sendto_handler cb,
54+
const uint8_t* data, size_t length, sendto_handler cb, error_handler ecb,
6555
UDP& udp, addr_t LA, port_t LP, addr_t DA, port_t DP);
6656

6757
int remaining() const
@@ -78,7 +68,9 @@ namespace net {
7868
size_t len;
7969
size_t offset;
8070
// the callback for when this buffer is written
81-
sendto_handler callback;
71+
sendto_handler send_callback;
72+
// the callback for when this receives an error
73+
error_handler error_callback;
8274
// the UDP stack
8375
UDP& udp;
8476

@@ -184,22 +176,22 @@ namespace net {
184176
/** Error entries are just error callbacks and timestamps */
185177
class Error_entry {
186178
public:
187-
Error_entry(UDP::sendto_handler cb) noexcept
179+
Error_entry(UDP::error_handler cb) noexcept
188180
: callback(std::move(cb)), timestamp(RTC::time_since_boot())
189181
{}
190182

191183
bool expired() noexcept
192184
{ return timestamp + exp_t_ < RTC::time_since_boot(); }
193185

194-
UDP::sendto_handler callback;
186+
UDP::error_handler callback;
195187

196188
private:
197189
RTC::timestamp_t timestamp;
198190

199191
}; //< class Error_entry
200192

201193
/** The error callbacks that the user has sent in via the UDPSockets' sendto and bcast methods */
202-
std::unordered_map<Dest_tuple, Error_entry, pair_hash> error_callbacks_;
194+
std::unordered_map<Socket, Error_entry, Socket::pair_hash> error_callbacks_;
203195

204196
/** Timer that flushes expired error entries/callbacks (no errors have occurred) */
205197
Timer flush_timer_{{ *this, &UDP::flush_expired }};

api/net/ip4/udp_socket.hpp

100755100644
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ namespace net
3333

3434
typedef delegate<void(addr_t, port_t, const char*, size_t)> recvfrom_handler;
3535
typedef UDP::sendto_handler sendto_handler;
36+
typedef UDP::error_handler error_handler;
3637

3738
// constructors
3839
UDPSocket(UDP&, port_t port);
@@ -48,11 +49,13 @@ namespace net
4849

4950
void sendto(addr_t destIP, port_t port,
5051
const void* buffer, size_t length,
51-
sendto_handler cb = nullptr);
52+
sendto_handler cb = nullptr,
53+
error_handler ecb = nullptr);
5254

5355
void bcast(addr_t srcIP, port_t port,
5456
const void* buffer, size_t length,
55-
sendto_handler cb = nullptr);
57+
sendto_handler cb = nullptr,
58+
error_handler ecb = nullptr);
5659

5760
void close()
5861
{ udp_.close(l_port); }

api/net/socket.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ class Socket {
3232
using Address = ip4::Addr;
3333
using port_t = uint16_t;
3434

35+
struct pair_hash {
36+
std::size_t operator () (const Socket& s) const {
37+
auto h1 = std::hash<Address>{}(s.address());
38+
auto h2 = std::hash<port_t>{}(s.port());
39+
return h1 ^ h2;
40+
}
41+
};
42+
3543
/**
3644
* Constructor
3745
*

src/net/dns/client.cpp

100755100644
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace net
3636
std::forward_as_tuple(std::move(request), std::move(func)));
3737

3838
// send request to DNS server
39-
socket_.sendto(dns_server, DNS::DNS_SERVICE_PORT, buf.data(), len, [this, dns_server, key] (Error& err) {
39+
socket_.sendto(dns_server, DNS::DNS_SERVICE_PORT, buf.data(), len, nullptr, [this, dns_server, key] (Error& err) {
4040
// If an error is not received, this will never execute (Error is just erased from the map
4141
// without calling the callback)
4242

src/net/ip4/udp.cpp

100755100644
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ namespace net {
6868
// Report to application layer that got an ICMP error message of type and code (reason and subreason)
6969

7070
// Find callback with this destination address and port, and call it with the incoming err
71-
auto it = error_callbacks_.find(std::make_pair(dest.address(), dest.port()));
71+
auto it = error_callbacks_.find(Socket{dest.address(), dest.port()});
7272

7373
if (it != error_callbacks_.end()) {
7474
it->second.callback(err);
@@ -143,10 +143,8 @@ namespace net {
143143
INFO("UDP", "Flushing expired error callbacks");
144144

145145
for (auto& err : error_callbacks_) {
146-
if (err.second.expired()) {
147-
// error_callbacks_.second.callback(ICMP_error{});
146+
if (err.second.expired())
148147
error_callbacks_.erase(err.first);
149-
}
150148
}
151149

152150
if (not error_callbacks_.empty())
@@ -164,10 +162,13 @@ namespace net {
164162
num--;
165163

166164
if (buffer.done()) {
167-
if (buffer.callback != nullptr) {
165+
if (buffer.send_callback != nullptr)
166+
buffer.send_callback();
167+
168+
if (buffer.error_callback != nullptr) {
168169
error_callbacks_.emplace(std::piecewise_construct,
169-
std::forward_as_tuple(std::make_pair(buffer.d_addr, buffer.d_port)),
170-
std::forward_as_tuple(Error_entry{buffer.callback}));
170+
std::forward_as_tuple(Socket{buffer.d_addr, buffer.d_port}),
171+
std::forward_as_tuple(Error_entry{buffer.error_callback}));
171172

172173
if (UNLIKELY(not flush_timer_.is_running()))
173174
flush_timer_.start(flush_interval_);
@@ -194,9 +195,9 @@ namespace net {
194195
return P;
195196
}
196197

197-
UDP::WriteBuffer::WriteBuffer(const uint8_t* data, size_t length, sendto_handler cb,
198+
UDP::WriteBuffer::WriteBuffer(const uint8_t* data, size_t length, sendto_handler cb, error_handler ecb,
198199
UDP& stack, addr_t LA, port_t LP, addr_t DA, port_t DP)
199-
: len(length), offset(0), callback(cb), udp(stack),
200+
: len(length), offset(0), send_callback(cb), error_callback(ecb), udp(stack),
200201
l_addr(LA), l_port(LP), d_port(DP), d_addr(DA)
201202
{
202203
// create a copy of the data,

src/net/ip4/udp_socket.cpp

100755100644
Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,37 @@ namespace net
4343
void UDPSocket::internal_read(UDP::Packet_ptr udp)
4444
{ on_read_handler(udp->ip_src(), udp->src_port(), (const char*) udp->data(), udp->data_length()); }
4545

46-
void UDPSocket::sendto (
46+
void UDPSocket::sendto(
4747
addr_t destIP,
4848
port_t port,
4949
const void* buffer,
50-
size_t len,
51-
sendto_handler cb)
50+
size_t length,
51+
sendto_handler cb,
52+
error_handler ecb)
5253
{
53-
if (UNLIKELY(len == 0)) return;
54+
if (UNLIKELY(length == 0)) return;
5455
udp_.sendq.emplace_back(
55-
(const uint8_t*) buffer, len, cb, this->udp_,
56+
(const uint8_t*) buffer, length, cb, ecb, this->udp_,
5657
local_addr(), this->l_port, destIP, port);
5758

5859
// UDP packets are meant to be sent immediately, so try flushing
5960
udp_.flush();
6061
}
6162

62-
void UDPSocket::bcast (
63-
addr_t srcIP,
64-
port_t port,
65-
const void* buffer,
66-
size_t len,
67-
sendto_handler cb)
63+
void UDPSocket::bcast(
64+
addr_t srcIP,
65+
port_t port,
66+
const void* buffer,
67+
size_t length,
68+
sendto_handler cb,
69+
error_handler ecb)
6870
{
69-
if (UNLIKELY(len == 0)) return;
71+
if (UNLIKELY(length == 0)) return;
7072
udp_.sendq.emplace_back(
71-
(const uint8_t*) buffer, len, cb, this->udp_,
73+
(const uint8_t*) buffer, length, cb, ecb, this->udp_,
7274
srcIP, this->l_port, IP4::ADDR_BCAST, port);
7375

7476
// UDP packets are meant to be sent immediately, so try flushing
7577
udp_.flush();
7678
}
77-
}
79+
} // < namespace net

src/posix/udp_fd.cpp

100755100644
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ ssize_t UDP_FD::sendto(const void* message, size_t len, int,
178178
// Sending
179179
bool written = false;
180180
this->sock->sendto(ntohl(dest.sin_addr.s_addr), ntohs(dest.sin_port), message, len,
181-
[&written](net::Error) { written = true; });
181+
[&written]() { written = true; });
182182

183183
while(!written)
184184
OS::block();

0 commit comments

Comments
 (0)