blob: 38850267e220cdb2206edcc8a4db2b955563dc93 [file] [log] [blame]
initial.commit586acc5fe2008-07-26 22:42:521// Copyright 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#ifndef NET_BASE_TCP_CLIENT_SOCKET_H_
31#define NET_BASE_TCP_CLIENT_SOCKET_H_
32
33#include <ws2tcpip.h>
34
[email protected]3fa4bad2008-08-04 18:38:0435#include "base/object_watcher.h"
initial.commit586acc5fe2008-07-26 22:42:5236#include "net/base/address_list.h"
37#include "net/base/client_socket.h"
38
39namespace net {
40
[email protected]836dff32008-08-05 23:15:3641// A client socket that uses TCP as the transport layer.
42//
43// NOTE: The implementation supports half duplex only. Read and Write calls
44// must not be in progress at the same time.
[email protected]3fa4bad2008-08-04 18:38:0445class TCPClientSocket : public ClientSocket,
46 public base::ObjectWatcher::Delegate {
initial.commit586acc5fe2008-07-26 22:42:5247 public:
48 // The IP address(es) and port number to connect to. The TCP socket will try
49 // each IP address in the list until it succeeds in establishing a
50 // connection.
[email protected]836dff32008-08-05 23:15:3651 explicit TCPClientSocket(const AddressList& addresses);
initial.commit586acc5fe2008-07-26 22:42:5252
53 ~TCPClientSocket();
54
55 // ClientSocket methods:
56 virtual int Connect(CompletionCallback* callback);
57 virtual int ReconnectIgnoringLastError(CompletionCallback* callback);
58 virtual void Disconnect();
59 virtual bool IsConnected() const;
60
61 // Socket methods:
62 virtual int Read(char* buf, int buf_len, CompletionCallback* callback);
63 virtual int Write(const char* buf, int buf_len, CompletionCallback* callback);
64
65 private:
66 int CreateSocket(const struct addrinfo* ai);
67 void DoCallback(int rv);
68 void DidCompleteConnect();
69 void DidCompleteIO();
70
[email protected]836dff32008-08-05 23:15:3671 // MessageLoop::Watcher methods:
initial.commit586acc5fe2008-07-26 22:42:5272 virtual void OnObjectSignaled(HANDLE object);
73
74 SOCKET socket_;
75 OVERLAPPED overlapped_;
76 WSABUF buffer_;
77
[email protected]3fa4bad2008-08-04 18:38:0478 base::ObjectWatcher watcher_;
79
initial.commit586acc5fe2008-07-26 22:42:5280 CompletionCallback* callback_;
81
[email protected]836dff32008-08-05 23:15:3682 // The list of addresses we should try in order to establish a connection.
initial.commit586acc5fe2008-07-26 22:42:5283 AddressList addresses_;
84
[email protected]836dff32008-08-05 23:15:3685 // The addrinfo that we are attempting to use or NULL if all addrinfos have
86 // been tried.
initial.commit586acc5fe2008-07-26 22:42:5287 const struct addrinfo* current_ai_;
88
89 enum WaitState {
90 NOT_WAITING,
91 WAITING_CONNECT,
92 WAITING_READ,
93 WAITING_WRITE
94 };
95 WaitState wait_state_;
96};
97
98} // namespace net
99
100#endif // NET_BASE_TCP_CLIENT_SOCKET_H_