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)). | |
asanka
2015/08/31 19:20:01
Why is this a link to Git blame output?
Randy Smith (Not in Mondays)
2015/08/31 21:55:06
Oops; re-linked to the non-blame googlesource link
| |
9 Zero indicates either net::OK or zero bytes read (usually EOF) | |
10 depending on the context. This data union is generally specified by | |
Charlie Harrison
2015/08/31 19:10:10
"This data union" => "this pattern". The former fe
Ryan Sleevi
2015/08/31 19:38:47
Throughout this document, I note a use of two spac
mmenke
2015/08/31 19:52:01
I note that the reason I've always seen given for
Randy Smith (Not in Mondays)
2015/08/31 21:55:05
*chuckle/sigh* Yeah, the last time you brought th
Randy Smith (Not in Mondays)
2015/08/31 21:55:05
Done.
Randy Smith (Not in Mondays)
2015/08/31 21:55:05
I don't have anywhere near a strong enough feeling
mmenke
2015/08/31 21:59:20
I don't feel strongly about it at all. I just fee
| |
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 | |
23 * If the value is positive or zero, that indicates a synchronous | |
24 successful return, with a zero return value possibly indicating zero | |
25 bytes/EOF and possibly indicating net::OK, depending on context. | |
26 * If the value is negative and != net::ERR_IO_PENDING, it is an error | |
27 code specifying a synchronous failing return. | |
28 * If the return value is the special value net::ERR_IO_PENDING, it | |
asanka
2015/08/31 19:20:01
`net::ERR_IO_PENDING` ? Would the underscores be i
Randy Smith (Not in Mondays)
2015/08/31 21:55:05
I don't understand the comment? Are you asking fo
Ryan Sleevi
2015/08/31 22:17:43
Just that _FOO_ in Markdown indicates "emphasis on
Randy Smith (Not in Mondays)
2015/09/02 02:32:28
Done.
Randy Smith (Not in Mondays)
2015/09/02 02:32:28
Ah, got it. Done.
| |
29 indicates that the routine will complete asynchronously. Any buffer | |
Ryan Sleevi
2015/08/31 19:38:47
Any IOBuffer?
I think this might use some expansi
Randy Smith (Not in Mondays)
2015/08/31 21:55:05
Done.
| |
30 provided will be retained by the called entity until completion, to | |
31 be written into or read from as required. If a callback was | |
32 provided, that callback will be called upon completion with the | |
33 return value; if a callback is not provided, it usually means that | |
34 some known callback mechanism will be employed. | |
35 | |
36 ## DoLoop | |
37 | |
38 The pattern usually used to construct state machines in the Chrome | |
39 network stack is the DoLoop function. Any class that must drive a | |
40 state machine will contain an enum listing all states of that machine, | |
41 and define a function, |DoLoop|, to drive that state machine. The | |
Ryan Sleevi
2015/08/31 19:38:47
It's worth noting that sometimes there are multipl
Randy Smith (Not in Mondays)
2015/08/31 21:55:05
Done.
Can you give me a pointer to examples of
mmenke
2015/08/31 21:59:20
SpdySession has separate read and write loops. I
Ryan Sleevi
2015/08/31 22:17:43
Yeah, DoReadLoop/DoWriteLoop/DoHandshakeLoop are t
Randy Smith (Not in Mondays)
2015/09/02 02:32:28
Acknowledged.
Randy Smith (Not in Mondays)
2015/09/02 02:32:28
I shifted the example names over to using this, bu
| |
42 characteristics of this pattern are: | |
43 | |
44 * Each state has a corresponding function which is called by DoLoop | |
45 for handling when the state machine is in that state. Generally the | |
46 states are named STATE_<state name> (upper case separated by | |
47 underscores), and the routine is named Do<StateName> (CamelCase). | |
48 Those functions both take and return values that are either | |
49 net::Errors or the above combined error and byte count value. | |
asanka
2015/08/31 19:20:01
Probably also mention pairs of state that are also
Randy Smith (Not in Mondays)
2015/08/31 21:55:05
Done; take a look and see what you think.
| |
50 * Each state handling function has two basic responsibilities in | |
51 addition to state specific handling: Setting the data member | |
52 (named |next_state_| or something similar) | |
53 to specify the next state, and returning a net::Error (or combined | |
54 error and byte count, as above). | |
55 * DoLoop loops, initializes state_ to STATE_NONE, and calls the | |
56 appropriate state handling based the original value of state_. | |
Charlie Harrison
2015/08/31 19:10:10
What is "the original value of state_" if we've ju
asanka
2015/08/31 19:20:01
s/state_/next_state_/ here an elsewhere.
Randy Smith (Not in Mondays)
2015/08/31 21:55:05
Done.
Randy Smith (Not in Mondays)
2015/08/31 21:55:06
Right, wrong way to put it (it saves the original
| |
57 * If the return value from the state handling function is | |
58 net::ERR_IO_PENDING, that indicates that the function has arranged | |
59 for DoLoop() to be called at some point in the future, when further | |
60 progress can be made on the state transitions. The state_ variable | |
61 will have been set to the value proper for handling that incoming | |
62 call. In this case, DoLoop() will exit. | |
63 * If the return value from the state handling function is STATE_NONE | |
64 (indicating a failure to set the state_ variable) or STATE_DONE | |
asanka
2015/08/31 19:20:01
Is it a failure to set the next state? I've mostly
Randy Smith (Not in Mondays)
2015/08/31 21:55:05
Partially a mis-typing on my part, partially my un
| |
65 (indicating that all state transitions have completed) DoLoop() will | |
66 also return. At this point, the return value from the operation being | |
67 driven by the state machine will be returned, either synchronously or | |
68 by invoking a callback. | |
69 * When in STATE_NONE the object is generally waiting for a particular | |
Charlie Harrison
2015/08/31 19:10:10
Is this an overloading of the point of STATE_NONE?
Randy Smith (Not in Mondays)
2015/08/31 21:55:05
On 2015/08/31 19:10:10, csharrison wrote:
Splitti
| |
70 call from its consumer (e.g. a Read* call after some state machine | |
71 driven initialization) before re-entering its DoLoop. | |
72 | |
73 Public class methods should have as little processing as possible, | |
74 often simply making copies of arguments into data members, setting the | |
75 state_ variable to indicate the section of the state diagram to | |
76 process, and calling DoLoop(). | |
77 | |
78 This idiom allows synchronous and asynchronous logic to be written in | |
79 the same fashion; it's all just state transition handling. For mostly | |
80 linear state diagrams, the handling code can be very easy to | |
81 comprehend, as such code is usually written linearly (in different | |
82 handling functions) in the order it's executed. If there can be | |
83 multiple differnet events that complete outstanding IO, the framework | |
asanka
2015/08/31 19:20:01
different
Randy Smith (Not in Mondays)
2015/08/31 21:55:05
Done.
| |
84 doesn't handle that explicitly; the state handling code for the | |
85 receiving state much explicitly distinguish between those events and | |
asanka
2015/08/31 19:20:01
must
Randy Smith (Not in Mondays)
2015/08/31 21:55:06
Done.
| |
86 do the appropriate state transition. | |
davidben
2015/08/31 19:59:14
I think I might phrase this more strongly. It's no
Randy Smith (Not in Mondays)
2015/08/31 21:55:06
So a) thank you, the whole reason I personally wro
davidben
2015/08/31 22:19:35
Hrm. I'm not sure I follow what you're saying. Gra
| |
87 | |
Ryan Sleevi
2015/08/31 19:38:47
One area I've seen frequently trip people up in re
Randy Smith (Not in Mondays)
2015/08/31 21:55:06
Thank you; as I noted to David, one of my agendas
| |
88 For examples of this idiom, see | |
89 | |
90 * [HttpStreamParser::DoLoop](https://code.google.com/p/chromium/codesearch#chrom ium/src/net/http/http_stream_parser.cc&q=HttpStreamParser::DoLoop&sq=package:chr omium). | |
91 * [HttpNetworkTransaction::DoLoop](https://code.google.com/p/chromium/codesearch #chromium/src/net/http/http_network_transaction.cc&q=HttpNetworkTransaction::DoL oop&sq=package:chromium) | |
92 | |
OLD | NEW |