Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / net / socket / tcp_socket_win.h
1 // Copyright 2013 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_SOCKET_TCP_SOCKET_WIN_H_
6 #define NET_SOCKET_TCP_SOCKET_WIN_H_
7
8 #include <winsock2.h>
9
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "base/win/object_watcher.h"
16 #include "net/base/address_family.h"
17 #include "net/base/completion_callback.h"
18 #include "net/base/net_export.h"
19 #include "net/base/net_log.h"
20
21 namespace net {
22
23 class AddressList;
24 class IOBuffer;
25 class IPEndPoint;
26
27 class NET_EXPORT TCPSocketWin : NON_EXPORTED_BASE(public base::NonThreadSafe),
28                                 public base::win::ObjectWatcher::Delegate  {
29  public:
30   TCPSocketWin(NetLog* net_log, const NetLog::Source& source);
31   virtual ~TCPSocketWin();
32
33   int Open(AddressFamily family);
34
35   // Both AdoptConnectedSocket and AdoptListenSocket take ownership of an
36   // existing socket. AdoptConnectedSocket takes an already connected
37   // socket. AdoptListenSocket takes a socket that is intended to accept
38   // connection. In some sense, AdoptListenSocket is more similar to Open.
39   int AdoptConnectedSocket(SOCKET socket, const IPEndPoint& peer_address);
40   int AdoptListenSocket(SOCKET socket);
41
42   int Bind(const IPEndPoint& address);
43
44   int Listen(int backlog);
45   int Accept(scoped_ptr<TCPSocketWin>* socket,
46              IPEndPoint* address,
47              const CompletionCallback& callback);
48
49   int Connect(const IPEndPoint& address, const CompletionCallback& callback);
50   bool IsConnected() const;
51   bool IsConnectedAndIdle() const;
52
53   // Multiple outstanding requests are not supported.
54   // Full duplex mode (reading and writing at the same time) is supported.
55   int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
56   int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
57
58   int GetLocalAddress(IPEndPoint* address) const;
59   int GetPeerAddress(IPEndPoint* address) const;
60
61   // Sets various socket options.
62   // The commonly used options for server listening sockets:
63   // - SetExclusiveAddrUse().
64   int SetDefaultOptionsForServer();
65   // The commonly used options for client sockets and accepted sockets:
66   // - Increase the socket buffer sizes for WinXP;
67   // - SetNoDelay(true);
68   // - SetKeepAlive(true, 45).
69   void SetDefaultOptionsForClient();
70   int SetExclusiveAddrUse();
71   int SetReceiveBufferSize(int32 size);
72   int SetSendBufferSize(int32 size);
73   bool SetKeepAlive(bool enable, int delay);
74   bool SetNoDelay(bool no_delay);
75
76   void Close();
77
78   // Setter/Getter methods for TCP FastOpen socket option.
79   // NOOPs since TCP FastOpen is not implemented in Windows.
80   bool UsingTCPFastOpen() const { return false; }
81   void EnableTCPFastOpenIfSupported() {}
82
83   bool IsValid() const { return socket_ != INVALID_SOCKET; }
84
85   // Marks the start/end of a series of connect attempts for logging purpose.
86   //
87   // TCPClientSocket may attempt to connect to multiple addresses until it
88   // succeeds in establishing a connection. The corresponding log will have
89   // multiple NetLog::TYPE_TCP_CONNECT_ATTEMPT entries nested within a
90   // NetLog::TYPE_TCP_CONNECT. These methods set the start/end of
91   // NetLog::TYPE_TCP_CONNECT.
92   //
93   // TODO(yzshen): Change logging format and let TCPClientSocket log the
94   // start/end of a series of connect attempts itself.
95   void StartLoggingMultipleConnectAttempts(const AddressList& addresses);
96   void EndLoggingMultipleConnectAttempts(int net_error);
97
98   const BoundNetLog& net_log() const { return net_log_; }
99
100  private:
101   class Core;
102
103   // base::ObjectWatcher::Delegate implementation.
104   virtual void OnObjectSignaled(HANDLE object) OVERRIDE;
105
106   int AcceptInternal(scoped_ptr<TCPSocketWin>* socket,
107                      IPEndPoint* address);
108
109   int DoConnect();
110   void DoConnectComplete(int result);
111
112   void LogConnectBegin(const AddressList& addresses);
113   void LogConnectEnd(int net_error);
114
115   int DoRead(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
116   void DidCompleteConnect();
117   void DidCompleteWrite();
118   void DidSignalRead();
119
120   SOCKET socket_;
121
122   HANDLE accept_event_;
123   base::win::ObjectWatcher accept_watcher_;
124
125   scoped_ptr<TCPSocketWin>* accept_socket_;
126   IPEndPoint* accept_address_;
127   CompletionCallback accept_callback_;
128
129   // The various states that the socket could be in.
130   bool waiting_connect_;
131   bool waiting_read_;
132   bool waiting_write_;
133
134   // The core of the socket that can live longer than the socket itself. We pass
135   // resources to the Windows async IO functions and we have to make sure that
136   // they are not destroyed while the OS still references them.
137   scoped_refptr<Core> core_;
138
139   // External callback; called when connect or read is complete.
140   CompletionCallback read_callback_;
141
142   // External callback; called when write is complete.
143   CompletionCallback write_callback_;
144
145   scoped_ptr<IPEndPoint> peer_address_;
146   // The OS error that a connect attempt last completed with.
147   int connect_os_error_;
148
149   bool logging_multiple_connect_attempts_;
150
151   BoundNetLog net_log_;
152
153   DISALLOW_COPY_AND_ASSIGN(TCPSocketWin);
154 };
155
156 }  // namespace net
157
158 #endif  // NET_SOCKET_TCP_SOCKET_WIN_H_