Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / quic / port_suggester.h
blob0074f7c745975c6c6a7d7e83362e6591e7a9d5c3
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.
5 #ifndef NET_QUIC_PORT_SUGGESTER_H_
6 #define NET_QUIC_PORT_SUGGESTER_H_
8 #include "base/basictypes.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/sha1.h"
11 #include "net/base/net_export.h"
13 namespace net {
15 class HostPortPair;
17 // We provide a pseudo-random number generator that is always seeded the same
18 // way for a given destination host-port pair. The generator is used to
19 // consistently suggest (for that host-port pair) an ephemeral source port,
20 // and hence increase the likelihood that a server's load balancer will direct
21 // a repeated connection to the same server (with QUIC, further increasing the
22 // chance of connection establishment with 0-RTT).
23 class NET_EXPORT_PRIVATE PortSuggester
24 : public base::RefCounted<PortSuggester> {
25 public:
26 PortSuggester(const HostPortPair& server, uint64 seed);
28 // Generate a pseudo-random int in the inclusive range from |min| to |max|.
29 // Will (probably) return different numbers when called repeatedly.
30 int SuggestPort(int min, int max);
32 uint32 call_count() const { return call_count_; }
33 int previous_suggestion() const;
35 private:
36 friend class base::RefCounted<PortSuggester>;
38 virtual ~PortSuggester() {}
40 // We maintain the first 8 bytes of a hash as our seed_ state.
41 uint64 seed_;
42 uint32 call_count_; // Number of suggestions made.
43 int previous_suggestion_;
45 DISALLOW_COPY_AND_ASSIGN(PortSuggester);
48 } // namespace net
50 #endif // NET_QUIC_PORT_SUGGESTER_H_