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

Side by Side Diff: net/base/tcp_pinger.h

Issue 16207: Make ssl_client_socket_unittest use a local server (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 10 months 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/base/ssl_test_util.cc ('k') | net/base/tcp_pinger_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef NET_BASE_TCP_PINGER_H_
6 #define NET_BASE_TCP_PINGER_H_
7
8 #include "base/compiler_specific.h"
9 #include "base/ref_counted.h"
10 #include "base/scoped_ptr.h"
11 #include "base/task.h"
12 #include "base/thread.h"
13 #include "base/time.h"
14 #include "base/waitable_event.h"
15 #include "net/base/address_list.h"
16 #include "net/base/completion_callback.h"
17 #include "net/base/net_errors.h"
18 #include "net/base/tcp_client_socket.h"
19
20 namespace net {
21
22 // Simple class to wait until a TCP server is accepting connections.
23 class TCPPinger {
24 public:
25 explicit TCPPinger(const net::AddressList& addr)
26 : io_thread_("TCPPinger"),
27 worker_(new Worker(addr)) {
28 worker_->AddRef();
29 // Start up a throwaway IO thread just for this.
30 // TODO(dkegel): use some existing thread pool instead?
31 base::Thread::Options options;
32 options.message_loop_type = MessageLoop::TYPE_IO;
33 io_thread_.StartWithOptions(options);
34 }
35
36 ~TCPPinger() {
37 io_thread_.message_loop()->ReleaseSoon(FROM_HERE, worker_);
38 }
39
40 int Ping() {
41 // Default is 100 tries, each with a timeout of 100ms,
42 // for a total max timeout of 10 seconds.
43 return Ping(base::TimeDelta::FromMilliseconds(100), 100);
44 }
45
46 int Ping(base::TimeDelta tryTimeout, int nTries) {
47 int err = ERR_IO_PENDING;
48 // Post a request to do the connect on that thread.
49 for (int i = 0; i < nTries; i++) {
50 io_thread_.message_loop()->PostTask(FROM_HERE,
51 NewRunnableMethod(worker_,
52 &net::TCPPinger::Worker::DoConnect));
53 // Timeout here in case remote host offline
54 err = worker_->TimedWaitForResult(tryTimeout);
55 if (err == net::OK)
56 break;
57 PlatformThread::Sleep(static_cast<int>(tryTimeout.InMilliseconds()));
58 if (err == net::OK)
59 break;
60 // Cancel leftover activity, if any
61 io_thread_.message_loop()->PostTask(FROM_HERE,
62 NewRunnableMethod(worker_,
63 &net::TCPPinger::Worker::DoDisconnect));
64 worker_->WaitForResult();
65 }
66 return err;
67 }
68
69 private:
70
71 // Inner class to handle all actual socket calls.
72 // This makes the outer interface simpler,
73 // and helps us obey the "all socket calls
74 // must be on same thread" restriction.
75 class Worker : public base::RefCountedThreadSafe<Worker> {
76 public:
77 explicit Worker(const net::AddressList& addr)
78 : event_(false, false),
79 net_error_(ERR_IO_PENDING),
80 addr_(addr),
81 ALLOW_THIS_IN_INITIALIZER_LIST(connect_callback_(this,
82 &net::TCPPinger::Worker::ConnectDone)) {
83 }
84
85 void DoConnect() {
86 sock_.reset(new TCPClientSocket(addr_));
87 int rv = sock_->Connect(&connect_callback_);
88 // Regardless of success or failure, if we're done now,
89 // signal the customer.
90 if (rv != ERR_IO_PENDING)
91 ConnectDone(rv);
92 }
93
94 void DoDisconnect() {
95 sock_.reset();
96 event_.Signal();
97 }
98
99 void ConnectDone(int rv) {
100 sock_.reset();
101 net_error_ = rv;
102 event_.Signal();
103 }
104
105 int TimedWaitForResult(base::TimeDelta tryTimeout) {
106 event_.TimedWait(tryTimeout);
107 return net_error_;
108 }
109
110 int WaitForResult() {
111 event_.Wait();
112 return net_error_;
113 }
114
115 private:
116 base::WaitableEvent event_;
117 int net_error_;
118 net::AddressList addr_;
119 scoped_ptr<TCPClientSocket> sock_;
120 net::CompletionCallbackImpl<Worker> connect_callback_;
121 };
122
123 base::Thread io_thread_;
124 Worker* worker_;
125 DISALLOW_COPY_AND_ASSIGN(TCPPinger);
126 };
127
128 } // namespace net
129
130 #endif // NET_BASE_TCP_PINGER_H_
131
OLDNEW
« no previous file with comments | « net/base/ssl_test_util.cc ('k') | net/base/tcp_pinger_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698