OLD | NEW |
---|---|
(Empty) | |
1 # Chrome Network Stack Common Coding Patterns | |
2 | |
3 ## Combined error and byte count into a single value | |
4 | |
5 At many places in the network stack, functions return a value that, if | |
6 positive, indicate a count of bytes that the the function read or | |
7 wrote, and if negative, indicates a network stack error code (see | |
8 [net_error_list.h](https://chromium.googlesource.com/chromium/src/+blame/master/ net/base/net_error_list.h#1)). | |
xunjieli
2015/08/31 19:29:32
I find it easier to read if the reference links ar
Randy Smith (Not in Mondays)
2015/09/18 22:06:32
Ah, nice, I didn't know about that feature of mark
| |
9 Zero indicates either net::OK or zero bytes read (usually EOF) | |
10 depending on the context. This data union is generally specified by | |
11 an |int| return type. | |
12 | |
13 Many functions also have variables (often named |result|) containing | |
14 such value; this is especially common in the [DoLoop](#DoLoop) pattern | |
15 described below. | |
16 | |
17 ## Sync/Async Return | |
18 | |
19 Many network stack routines may return synchronously or | |
20 asynchronously. These functions generally return an int as described | |
21 above. There are three cases: | |
22 * If the value is positive or zero, that indicates a synchronous | |
23 successful return, with a zero return value usually indicating EOF. | |
mmenke
2015/08/28 16:24:57
I'd go with usually -> possibly.
Basically if we'
Randy Smith (Not in Mondays)
2015/08/31 18:23:22
Done.
| |
24 * If the value is negative and != net::ERR_IO_PENDING, it is an error | |
25 code specifying a synchronous failing return. | |
26 * If the return value is the special value net::ERR_IO_PENDING, it | |
27 indicates that the routine will complete asynchronously. Any buffer | |
28 provided will be retained by the called entity until completion, to | |
29 be written into or read from as required. If a callback was | |
30 provided, that callback will be called upon completion with the | |
31 return value; if a callback is not provided, it usually means that | |
32 some known callback mechanism will be employed (e.g. an asynchronous | |
33 return from | |
34 [URLRequestJob::ReadRawData](https://code.google.com/p/chromium/codesearch#chr omium/src/net/url_request/url_request_job.h&q=ReadRawData&sq=package:chromium&l= 309) | |
mmenke
2015/08/28 16:24:57
I'd suggest avoiding URLRequestJob::ReadRawData -
Randy Smith (Not in Mondays)
2015/08/31 18:23:22
Hmmm. I think based on this feedback I'll just re
| |
35 will be signalled by calling [URLRequestJob::NotifyReadComplete](https://code. google.com/p/chromium/codesearch#chromium/src/net/url_request/url_request_job.h& q=NotifyReadComplete&sq=package:chromium). | |
36 | |
37 ## DoLoop | |
38 | |
39 The pattern usually used to construct state machines in the Chrome | |
xunjieli
2015/08/31 19:29:32
usually [is] used?
Randy Smith (Not in Mondays)
2015/09/18 22:06:32
I was using a shorthand language construct (You ca
| |
40 network stack is the DoLoop function. Any class that must drive a | |
41 state machine will contain an enum listing all states of that machine, | |
42 and define a function, |DoLoop|, to drive that state machine. The | |
43 characteristics of this pattern are: | |
44 | |
45 * Each state has a corresponding function which is called by DoLoop | |
46 for handling when the state machine is in that state. Generally the | |
47 states are named STATE_<state name> (upper case separated by | |
48 underscores), and the routine is named Do<StateName> (CamelCase). | |
49 Those functions both take and return values that are either | |
50 net::Errors or the above combined error and byte count value. | |
51 * Each state handling function has two basic responsibilities in | |
52 addition to state specific handling: Setting the data member | |
53 (named |state_| or something similar) | |
mmenke
2015/08/28 16:24:57
I'd say next_state_ is more common - when ERR_IO_P
Randy Smith (Not in Mondays)
2015/08/31 18:23:22
Done.
| |
54 to specify the next state, and returning a net::Error (or combined | |
55 error and byte count, as above). | |
56 * DoLoop loops, initializes state_ to STATE_NONE, and calls the | |
57 appropriate state handling based the original value of state_. | |
58 * If the return value from the state handling function is | |
59 net::ERR_IO_PENDING, that indicates that the function has arranged | |
60 for DoLoop() to be called at some point in the future, when further | |
61 progress can be made on the state transitions. The state_ variable | |
62 will have been set to the value proper for handling that incoming | |
63 call. In this case, DoLoop() will return. | |
mmenke
2015/08/28 16:24:57
will exit?
Randy Smith (Not in Mondays)
2015/08/31 18:23:22
Done.
| |
64 * If the return value from the state handling function is STATE_NONE | |
65 (indicating a failure to set the state_ variable) or STATE_DONE | |
66 (indicating that all state transitions have completed) DoLoop() will | |
67 also return. | |
mmenke
2015/08/28 16:24:57
Suggest adding:
At this point, the return value w
Randy Smith (Not in Mondays)
2015/08/31 18:23:22
Done, though enough text is added that you should
| |
68 | |
69 Public class methods should have as little processing as possible, | |
70 often simply making copies of arguments into data members, setting the | |
71 state_ variable to indicate the section of the state diagram to | |
72 process, and calling DoLoop(). | |
73 | |
74 This idiom allows synchronous and asynchronous logic to be written in | |
75 the same fashion; it's all just state transition handling. For mostly | |
76 linear state diagrams, the handling code can be very easy to | |
77 comprehend, as such code is usually written linearly (in different | |
78 handling functions) in the order it's executed. If there can be | |
79 multiple differnet events that complete outstanding IO, the framework | |
xunjieli
2015/08/31 19:29:32
nit: s/differnet/different.
Randy Smith (Not in Mondays)
2015/09/18 22:06:32
I think this one disappeared in one of the editing
| |
80 doesn't handle that explicitly; the state handling code for the | |
81 receiving state much explicitly distinguish between those events and | |
xunjieli
2015/08/31 19:29:32
s/much/must
Randy Smith (Not in Mondays)
2015/09/18 22:06:32
Ditto.
| |
82 do the appropriate state transition. | |
83 | |
84 For an example of this idiom, see [HttpStreamParser::DoLoop](https://code.google .com/p/chromium/codesearch#chromium/src/net/http/http_stream_parser.cc&q=HttpStr eamParser::DoLoop&sq=package:chromium). | |
mmenke
2015/08/28 16:24:57
Maybe add HttpNetworkTransaction as well? I think
Randy Smith (Not in Mondays)
2015/09/18 22:06:32
Done.
| |
85 | |
86 | |
OLD | NEW |