Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
fixup! fixup! dgram: make UDPWrap more reusable
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax committed Oct 11, 2019
1 parent a915058 commit 53d166c
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/udp_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,36 @@ namespace node {

class UDPWrapBase;

// A listener that can be attached to an `UDPWrapBase` object and generally
// manages its I/O activity. This is similar to `StreamListener`.
class UDPListener {
public:
virtual ~UDPListener();

// Called right before data is received from the socket. Must return a
// buffer suitable for reading data into, that is then passed to OnRecv.
virtual uv_buf_t OnAlloc(size_t suggested_size) = 0;

// Called right after data is received from the socket, and includes
// information about the source address. If `nread` is negative, an error
// has occurred, and it represents a libuv error code.
virtual void OnRecv(ssize_t nread,
const uv_buf_t& buf,
const sockaddr* addr,
unsigned int flags) = 0;

// Called when an asynchronous request for writing data is created.
// The `msg_size` value contains the total size of the data to be sent,
// but may be ignored by the implementation of this Method.
// The return value is later passed to OnSendDone.
virtual ReqWrap<uv_udp_send_t>* CreateSendWrap(size_t msg_size) = 0;

// Called when an asynchronous request for writing data has finished.
// If status is negative, an error has occurred, and it represents a libuv
// error code.
virtual void OnSendDone(ReqWrap<uv_udp_send_t>* wrap, int status) = 0;

// Optional callback that is called after the socket has been bound.
virtual void OnAfterBind() {}

inline UDPWrapBase* udp() const { return wrap_; }
Expand All @@ -59,13 +79,26 @@ class UDPWrapBase {
static constexpr int kUDPWrapBaseField = 1;

virtual ~UDPWrapBase();

// Start emitting OnAlloc() + OnRecv() events on the listener.
virtual int RecvStart() = 0;

// Stop emitting OnAlloc() + OnRecv() events on the listener.
virtual int RecvStop() = 0;

// Send a chunk of data over this socket. This may call CreateSendWrap()
// on the listener if an async transmission is necessary.
virtual ssize_t Send(uv_buf_t* bufs,
size_t nbufs,
const sockaddr* addr) = 0;

// Stores the sockaddr for the peer in `name`.
virtual int GetPeerName(sockaddr* name, int* namelen) = 0;

// Stores the sockaddr for the local socket in `name`.
virtual int GetSockName(sockaddr* name, int* namelen) = 0;

// Returns an AsyncWrap object with the same lifetime as this object.
virtual AsyncWrap* GetAsyncWrap() = 0;

void set_listener(UDPListener* listener);
Expand Down

0 comments on commit 53d166c

Please sign in to comment.