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