1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 import "network/public/interfaces/net_address.mojom";
8 import "network/public/interfaces/network_error.mojom";
10 // UDPSocket and UDPSocketReceiver represent a UDP socket and its client. The
11 // typical flow of using the interfaces is:
12 // - Acquire a UDPSocket interface pointer.
13 // - (optional) Set options which are allowed prior to Bind()/Connect().
14 // - Bind or connect the socket.
15 // - (optional) Bind the UDPSocketReceiver request returned by Bind()/Connect()
16 // - (optional) Set options which are allowed after Bind()/Connect().
17 // - Send / request to receive datagrams. Received datagrams will be delivered
18 // to the bound receiver's OnReceived() call.
20 // Allows the socket to share the local address to which it will be bound with
21 // other processes. Should be called before Bind().
22 // (This is equivalent to SO_REUSEADDR of the POSIX socket API.)
23 AllowAddressReuse() => (NetworkError result);
25 // Binds the socket to the given address. The socket must not be bound or
27 // |bound_addr| is non-null on success. It might not be the same as |addr|.
28 // For example, if port 0 is used in |addr|, an available port is picked and
29 // returned in |bound_addr|. The caller may provide an implementation of
30 // |receiver| to receive datagrams read from the socket. |receiver| is null
32 Bind(NetAddress addr) => (NetworkError result, NetAddress? bound_addr,
33 UDPSocketReceiver&? receiver);
35 // Connects the socket to the remote address. The socket must not be bound or
37 // |local_addr| is non-null on success.
38 // The caller may provide an implementation of |receiver| to receive datagrams
39 // read from the socket. |receiver| is null on failure.
40 Connect(NetAddress remote_addr) => (NetworkError result,
41 NetAddress? local_addr,
42 UDPSocketReceiver&? receiver);
44 // Sets the OS send buffer size (in bytes) for the socket. The socket must be
45 // bound or connected.
46 SetSendBufferSize(uint32 size) => (NetworkError result);
48 // Sets the OS receive buffer size (in bytes) for the socket. The socket must
49 // be bound or connected.
50 SetReceiveBufferSize(uint32 size) => (NetworkError result);
52 // Negotiates the maximum number of pending SendTo() requests. If
53 // |requested_size| is set to 0, this method queries the current settings.
55 // The service stores SendTo() requests in a queue while they are waiting to
56 // be executed (i.e., while they are waiting to be placed in the OS send
57 // buffer and sent out). This method negotiates how many requests (not bytes)
58 // this queue is able to store. If the queue is full, the service fails new
59 // requests directly with error code ERR_INSUFFICIENT_RESOURCES and discards
60 // those datagrams. If the client wants to avoid such failures, it needs to
61 // keep track of how many SendTo() calls are pending and make sure the number
62 // doesn't exceed the result of this method.
63 NegotiateMaxPendingSendRequests(uint32 requested_size)
64 => (uint32 actual_size);
66 // Notifies that the receiver is ready to accept |number| of datagrams.
67 // Correspondingly, OnReceived() of the UDPSocketReceiver interface will be
68 // called |number| times (errors also count), unless the connection is closed
71 // It is allowed to call this method again before the previous request is
72 // completely satisfied. For example:
73 // service->ReceiveMore(3);
75 // // OnReceived() is called.
76 // // OnReceived() is called.
78 // service->ReceiveMore(3);
79 // // The client expects 4 more calls to OnReceived().
81 // Please note that how ReceiveMore() is used will affect performance
82 // significantly. For example:
84 // service->ReceiveMore(3);
85 // // OnReceived() is called.
86 // // OnReceived() is called.
87 // // OnReceived() is called.
90 // service->ReceiveMore(1);
91 // // OnReceived() is called.
92 // service->ReceiveMore(1);
93 // // OnReceived() is called.
94 // service->ReceiveMore(1);
95 // // OnReceived() is called.
97 // It is very likely that approach 1 will perform better than approach 2,
98 // because in approach 2 getting every datagram takes at least the time of a
99 // round trip to the service side.
100 ReceiveMore(uint32 datagram_number);
102 // Sends data to the specified destination. The socket must be bound or
103 // connected. |dest_addr| is allowed to be null if the socket is connected.
104 // On success, |result.code| is a non-negative number indicating how many
105 // bytes have been written. Otherwise, it is a network error code, including
106 // (but not limited to):
107 // - ERR_INSUFFICIENT_RESOURCES (-12): The service doesn't have sufficient
108 // resource to complete the operation. One possible cause is that the client
109 // tries to send too many datagrams in a short period of time.
110 // TODO(yzshen): Formalize Mojo networking error codes.
111 SendTo(NetAddress? dest_addr, array<uint8> data) => (NetworkError result);
114 interface UDPSocketReceiver {
115 // On success, |data| is non-null, |src_addr| is non-null if the socket is
116 // not connected, |result.code| is a non-negative number indicating how many
117 // bytes have been received. On failure, |result.code| is a network error
119 OnReceived(NetworkError result, NetAddress? src_addr, array<uint8>? data);