Index: net/docs/code-patterns.md |
diff --git a/net/docs/code-patterns.md b/net/docs/code-patterns.md |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b79914b974b687fc124d36c58fe9cff592534eec |
--- /dev/null |
+++ b/net/docs/code-patterns.md |
@@ -0,0 +1,92 @@ |
+# Chrome Network Stack Common Coding Patterns |
+ |
+## Combined error and byte count into a single value |
+ |
+At many places in the network stack, functions return a value that, if |
+positive, indicate a count of bytes that the the function read or |
+wrote, and if negative, indicates a network stack error code (see |
+[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
|
+Zero indicates either net::OK or zero bytes read (usually EOF) |
+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
|
+an |int| return type. |
+ |
+Many functions also have variables (often named |result|) containing |
+such value; this is especially common in the [DoLoop](#DoLoop) pattern |
+described below. |
+ |
+## Sync/Async Return |
+ |
+Many network stack routines may return synchronously or |
+asynchronously. These functions generally return an int as described |
+above. There are three cases: |
+ |
+* If the value is positive or zero, that indicates a synchronous |
+ successful return, with a zero return value possibly indicating zero |
+ bytes/EOF and possibly indicating net::OK, depending on context. |
+* If the value is negative and != net::ERR_IO_PENDING, it is an error |
+ code specifying a synchronous failing return. |
+* 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.
|
+ 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.
|
+ provided will be retained by the called entity until completion, to |
+ be written into or read from as required. If a callback was |
+ provided, that callback will be called upon completion with the |
+ return value; if a callback is not provided, it usually means that |
+ some known callback mechanism will be employed. |
+ |
+## DoLoop |
+ |
+The pattern usually used to construct state machines in the Chrome |
+network stack is the DoLoop function. Any class that must drive a |
+state machine will contain an enum listing all states of that machine, |
+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
|
+characteristics of this pattern are: |
+ |
+* Each state has a corresponding function which is called by DoLoop |
+ for handling when the state machine is in that state. Generally the |
+ states are named STATE_<state name> (upper case separated by |
+ underscores), and the routine is named Do<StateName> (CamelCase). |
+ Those functions both take and return values that are either |
+ 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.
|
+* Each state handling function has two basic responsibilities in |
+ addition to state specific handling: Setting the data member |
+ (named |next_state_| or something similar) |
+ to specify the next state, and returning a net::Error (or combined |
+ error and byte count, as above). |
+* DoLoop loops, initializes state_ to STATE_NONE, and calls the |
+ 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
|
+* If the return value from the state handling function is |
+ net::ERR_IO_PENDING, that indicates that the function has arranged |
+ for DoLoop() to be called at some point in the future, when further |
+ progress can be made on the state transitions. The state_ variable |
+ will have been set to the value proper for handling that incoming |
+ call. In this case, DoLoop() will exit. |
+* If the return value from the state handling function is STATE_NONE |
+ (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
|
+ (indicating that all state transitions have completed) DoLoop() will |
+ also return. At this point, the return value from the operation being |
+ driven by the state machine will be returned, either synchronously or |
+ by invoking a callback. |
+* 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
|
+ call from its consumer (e.g. a Read* call after some state machine |
+ driven initialization) before re-entering its DoLoop. |
+ |
+Public class methods should have as little processing as possible, |
+often simply making copies of arguments into data members, setting the |
+state_ variable to indicate the section of the state diagram to |
+process, and calling DoLoop(). |
+ |
+This idiom allows synchronous and asynchronous logic to be written in |
+the same fashion; it's all just state transition handling. For mostly |
+linear state diagrams, the handling code can be very easy to |
+comprehend, as such code is usually written linearly (in different |
+handling functions) in the order it's executed. If there can be |
+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.
|
+doesn't handle that explicitly; the state handling code for the |
+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.
|
+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
|
+ |
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
|
+For examples of this idiom, see |
+ |
+* [HttpStreamParser::DoLoop](https://code.google.com/p/chromium/codesearch#chromium/src/net/http/http_stream_parser.cc&q=HttpStreamParser::DoLoop&sq=package:chromium). |
+* [HttpNetworkTransaction::DoLoop](https://code.google.com/p/chromium/codesearch#chromium/src/net/http/http_network_transaction.cc&q=HttpNetworkTransaction::DoLoop&sq=package:chromium) |
+ |