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