@@ -34,16 +34,36 @@ namespace node {
34
34
35
35
class UDPWrapBase ;
36
36
37
+ // A listener that can be attached to an `UDPWrapBase` object and generally
38
+ // manages its I/O activity. This is similar to `StreamListener`.
37
39
class UDPListener {
38
40
public:
39
41
virtual ~UDPListener ();
42
+
43
+ // Called right before data is received from the socket. Must return a
44
+ // buffer suitable for reading data into, that is then passed to OnRecv.
40
45
virtual uv_buf_t OnAlloc (size_t suggested_size) = 0;
46
+
47
+ // Called right after data is received from the socket, and includes
48
+ // information about the source address. If `nread` is negative, an error
49
+ // has occurred, and it represents a libuv error code.
41
50
virtual void OnRecv (ssize_t nread,
42
51
const uv_buf_t & buf,
43
52
const sockaddr* addr,
44
53
unsigned int flags) = 0;
54
+
55
+ // Called when an asynchronous request for writing data is created.
56
+ // The `msg_size` value contains the total size of the data to be sent,
57
+ // but may be ignored by the implementation of this Method.
58
+ // The return value is later passed to OnSendDone.
45
59
virtual ReqWrap<uv_udp_send_t >* CreateSendWrap (size_t msg_size) = 0;
60
+
61
+ // Called when an asynchronous request for writing data has finished.
62
+ // If status is negative, an error has occurred, and it represents a libuv
63
+ // error code.
46
64
virtual void OnSendDone (ReqWrap<uv_udp_send_t >* wrap, int status) = 0;
65
+
66
+ // Optional callback that is called after the socket has been bound.
47
67
virtual void OnAfterBind () {}
48
68
49
69
inline UDPWrapBase* udp () const { return wrap_; }
@@ -59,13 +79,26 @@ class UDPWrapBase {
59
79
static constexpr int kUDPWrapBaseField = 1 ;
60
80
61
81
virtual ~UDPWrapBase ();
82
+
83
+ // Start emitting OnAlloc() + OnRecv() events on the listener.
62
84
virtual int RecvStart () = 0;
85
+
86
+ // Stop emitting OnAlloc() + OnRecv() events on the listener.
63
87
virtual int RecvStop () = 0;
88
+
89
+ // Send a chunk of data over this socket. This may call CreateSendWrap()
90
+ // on the listener if an async transmission is necessary.
64
91
virtual ssize_t Send (uv_buf_t * bufs,
65
92
size_t nbufs,
66
93
const sockaddr* addr) = 0;
94
+
95
+ // Stores the sockaddr for the peer in `name`.
67
96
virtual int GetPeerName (sockaddr* name, int * namelen) = 0;
97
+
98
+ // Stores the sockaddr for the local socket in `name`.
68
99
virtual int GetSockName (sockaddr* name, int * namelen) = 0;
100
+
101
+ // Returns an AsyncWrap object with the same lifetime as this object.
69
102
virtual AsyncWrap* GetAsyncWrap () = 0;
70
103
71
104
void set_listener (UDPListener* listener);
0 commit comments