Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(436)

Unified Diff: samples/chat/http_impl.dart

Issue 8776001: Fix HTTP sample implementation to actually wait for connections before attempting to write. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « samples/chat/http.dart ('k') | samples/tests/samples/src/chat/ChatServerTest.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: samples/chat/http_impl.dart
diff --git a/samples/chat/http_impl.dart b/samples/chat/http_impl.dart
index ec4aa9389e563b3539c1f8a8583e02945f6899a8..bf41112d358b25a1c9261a9b97abcfd1ac871046 100644
--- a/samples/chat/http_impl.dart
+++ b/samples/chat/http_impl.dart
@@ -1386,17 +1386,12 @@ class HTTPClientImplementation implements HTTPClient{
HTTPClientImplementation() : _openSockets = new Map(), _shutdown = false;
- HTTPClientRequest open(String method,
- String host,
- int port,
- String path) {
- // TODO(sgjesse): Throw exception.
- if (_shutdown) return null;
- SocketConnection socketConn = _getSocketConnection(host, port);
- HTTPClientConnection connection =
- new HTTPClientConnection(this, socketConn);
- HTTPClientRequest request = connection.open(method, path);
- return request;
+ void open(String method, String host, int port, String path) {
+ if (_shutdown && _openHandler) {
+ _openHandler(null);
+ return;
+ }
+ _getSocketConnection(host, port, method, path);
}
void shutdown() {
@@ -1417,17 +1412,29 @@ class HTTPClientImplementation implements HTTPClient{
return "$host:$port";
}
- SocketConnection _getSocketConnection(String host, int port) {
- SocketConnection entry;
+ void _getSocketConnection(String host, int port, String method, String path) {
+
+ void _connectionOpened(SocketConnection socketConn) {
+ HTTPClientConnection connection =
+ new HTTPClientConnection(this, socketConn);
+ HTTPClientRequest request = connection.open(method, path);
+ if (_openHandler != null) {
+ _openHandler(request);
+ }
+ }
// If there are active connections for this key get the first one
// otherwise create a new one.
Queue socketConnections = _openSockets[_connectionKey(host, port)];
if (socketConnections == null || socketConnections.isEmpty()) {
Socket socket = new Socket(host, port);
- entry = new SocketConnection(host, port, socket);
+ socket.connectHandler = () {
+ SocketConnection socketConn = new SocketConnection(host, port, socket);
+ _connectionOpened(socketConn);
+ };
} else {
- entry = socketConnections.removeFirst();
+ SocketConnection socketConn = socketConnections.removeFirst();
+ _connectionOpened(socketConn);
// Get rid of eviction timer if there are no more active connections.
if (socketConnections.isEmpty()) {
@@ -1435,8 +1442,6 @@ class HTTPClientImplementation implements HTTPClient{
_evictionTimer = null;
}
}
-
- return entry;
}
void _returnSocketConnection(SocketConnection socketConn) {
@@ -1482,6 +1487,11 @@ class HTTPClientImplementation implements HTTPClient{
socketConn._markReturned();
}
+ void set openHandler(void callback(HTTPClientRequest request)) {
+ _openHandler = callback;
+ }
+
+ var _openHandler;
Map<String, Queue<SocketConnection>> _openSockets;
Timer _evictionTimer;
bool _shutdown; // Has this HTTP client been shutdown?
« no previous file with comments | « samples/chat/http.dart ('k') | samples/tests/samples/src/chat/ChatServerTest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698