blob: 06a57fe1065a0b0fc605a4eb85de91184af085c5 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
initial.commit586acc5fe2008-07-26 22:42:524
5#ifndef NET_BASE_TCP_CLIENT_SOCKET_H_
6#define NET_BASE_TCP_CLIENT_SOCKET_H_
7
8#include <ws2tcpip.h>
9
[email protected]3fa4bad2008-08-04 18:38:0410#include "base/object_watcher.h"
initial.commit586acc5fe2008-07-26 22:42:5211#include "net/base/address_list.h"
12#include "net/base/client_socket.h"
13
14namespace net {
15
[email protected]836dff32008-08-05 23:15:3616// A client socket that uses TCP as the transport layer.
17//
18// NOTE: The implementation supports half duplex only. Read and Write calls
19// must not be in progress at the same time.
[email protected]3fa4bad2008-08-04 18:38:0420class TCPClientSocket : public ClientSocket,
21 public base::ObjectWatcher::Delegate {
initial.commit586acc5fe2008-07-26 22:42:5222 public:
23 // The IP address(es) and port number to connect to. The TCP socket will try
24 // each IP address in the list until it succeeds in establishing a
25 // connection.
[email protected]836dff32008-08-05 23:15:3626 explicit TCPClientSocket(const AddressList& addresses);
initial.commit586acc5fe2008-07-26 22:42:5227
28 ~TCPClientSocket();
29
30 // ClientSocket methods:
31 virtual int Connect(CompletionCallback* callback);
32 virtual int ReconnectIgnoringLastError(CompletionCallback* callback);
33 virtual void Disconnect();
34 virtual bool IsConnected() const;
35
36 // Socket methods:
37 virtual int Read(char* buf, int buf_len, CompletionCallback* callback);
38 virtual int Write(const char* buf, int buf_len, CompletionCallback* callback);
39
40 private:
41 int CreateSocket(const struct addrinfo* ai);
42 void DoCallback(int rv);
43 void DidCompleteConnect();
44 void DidCompleteIO();
45
[email protected]5656af62008-08-14 22:55:0446 // base::ObjectWatcher::Delegate methods:
initial.commit586acc5fe2008-07-26 22:42:5247 virtual void OnObjectSignaled(HANDLE object);
48
49 SOCKET socket_;
50 OVERLAPPED overlapped_;
51 WSABUF buffer_;
52
[email protected]3fa4bad2008-08-04 18:38:0453 base::ObjectWatcher watcher_;
54
initial.commit586acc5fe2008-07-26 22:42:5255 CompletionCallback* callback_;
56
[email protected]836dff32008-08-05 23:15:3657 // The list of addresses we should try in order to establish a connection.
initial.commit586acc5fe2008-07-26 22:42:5258 AddressList addresses_;
59
[email protected]836dff32008-08-05 23:15:3660 // The addrinfo that we are attempting to use or NULL if all addrinfos have
61 // been tried.
initial.commit586acc5fe2008-07-26 22:42:5262 const struct addrinfo* current_ai_;
63
64 enum WaitState {
65 NOT_WAITING,
66 WAITING_CONNECT,
67 WAITING_READ,
68 WAITING_WRITE
69 };
70 WaitState wait_state_;
71};
72
73} // namespace net
74
75#endif // NET_BASE_TCP_CLIENT_SOCKET_H_
license.botbf09a502008-08-24 00:55:5576