blob: 96589464b1d46e01912519a40416032977103a25 [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
[email protected]36987e92008-09-18 18:46:268#include "build/build_config.h"
[email protected]dab86192008-09-18 00:10:389
[email protected]36987e92008-09-18 18:46:2610#if defined(OS_WIN)
11#include <ws2tcpip.h>
[email protected]35aa85a2008-09-18 00:23:2412#include "base/object_watcher.h"
[email protected]36987e92008-09-18 18:46:2613#elif defined(OS_POSIX)
14struct event; // From libevent
15#define SOCKET int
16#include "base/message_pump_libevent.h"
17#endif
18
19#include "base/scoped_ptr.h"
initial.commit586acc5fe2008-07-26 22:42:5220#include "net/base/address_list.h"
21#include "net/base/client_socket.h"
[email protected]36987e92008-09-18 18:46:2622#include "net/base/completion_callback.h"
initial.commit586acc5fe2008-07-26 22:42:5223
24namespace net {
25
[email protected]836dff32008-08-05 23:15:3626// A client socket that uses TCP as the transport layer.
27//
28// NOTE: The implementation supports half duplex only. Read and Write calls
29// must not be in progress at the same time.
[email protected]3fa4bad2008-08-04 18:38:0430class TCPClientSocket : public ClientSocket,
[email protected]36987e92008-09-18 18:46:2631#if defined(OS_WIN)
32 public base::ObjectWatcher::Delegate
33#elif defined(OS_POSIX)
34 public base::MessagePumpLibevent::Watcher
35#endif
36{
initial.commit586acc5fe2008-07-26 22:42:5237 public:
38 // The IP address(es) and port number to connect to. The TCP socket will try
39 // each IP address in the list until it succeeds in establishing a
40 // connection.
[email protected]836dff32008-08-05 23:15:3641 explicit TCPClientSocket(const AddressList& addresses);
initial.commit586acc5fe2008-07-26 22:42:5242
43 ~TCPClientSocket();
44
45 // ClientSocket methods:
46 virtual int Connect(CompletionCallback* callback);
47 virtual int ReconnectIgnoringLastError(CompletionCallback* callback);
48 virtual void Disconnect();
49 virtual bool IsConnected() const;
50
51 // Socket methods:
[email protected]36987e92008-09-18 18:46:2652 // Try to transfer buf_len bytes to/from socket.
53 // If a result is available now, return it; else call back later with one.
54 // Do not call again until a result is returned!
55 // If any bytes were transferred, the result is the byte count.
56 // On error, result is a negative error code; see net/base/net_error_list.h
57 // TODO: what would a zero return value indicate?
58 // TODO: support multiple outstanding requests?
initial.commit586acc5fe2008-07-26 22:42:5259 virtual int Read(char* buf, int buf_len, CompletionCallback* callback);
60 virtual int Write(const char* buf, int buf_len, CompletionCallback* callback);
61
62 private:
initial.commit586acc5fe2008-07-26 22:42:5263 SOCKET socket_;
initial.commit586acc5fe2008-07-26 22:42:5264
[email protected]836dff32008-08-05 23:15:3665 // The list of addresses we should try in order to establish a connection.
initial.commit586acc5fe2008-07-26 22:42:5266 AddressList addresses_;
67
[email protected]36987e92008-09-18 18:46:2668 // Where we are in above list, or NULL if all addrinfos have been tried.
initial.commit586acc5fe2008-07-26 22:42:5269 const struct addrinfo* current_ai_;
70
71 enum WaitState {
72 NOT_WAITING,
73 WAITING_CONNECT,
74 WAITING_READ,
75 WAITING_WRITE
76 };
77 WaitState wait_state_;
[email protected]36987e92008-09-18 18:46:2678
79#if defined(OS_WIN)
80 // base::ObjectWatcher::Delegate methods:
81 virtual void OnObjectSignaled(HANDLE object);
82
83 OVERLAPPED overlapped_;
84 WSABUF buffer_;
85
86 base::ObjectWatcher watcher_;
87#elif defined(OS_POSIX)
88 // The socket's libevent wrapper
89 scoped_ptr<event> event_;
90
91 // Called by MessagePumpLibevent when the socket is ready to do I/O
92 void OnSocketReady(short flags);
93
94 // The buffer used by OnSocketReady to retry Read and Write requests
95 char* buf_;
96 int buf_len_;
97#endif
98
99 // External callback; called when read or write is complete.
100 CompletionCallback* callback_;
101
102 int CreateSocket(const struct addrinfo* ai);
103 void DoCallback(int rv);
104 void DidCompleteConnect();
105 void DidCompleteIO();
initial.commit586acc5fe2008-07-26 22:42:52106};
107
108} // namespace net
109
110#endif // NET_BASE_TCP_CLIENT_SOCKET_H_
license.botbf09a502008-08-24 00:55:55111