blob: d430297f30b14f6514839a2f63a0e74d59083e5f [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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 QUICHE_QUIC_TEST_TOOLS_SERVER_THREAD_H_
6#define QUICHE_QUIC_TEST_TOOLS_SERVER_THREAD_H_
7
8#include <memory>
9
10#include "net/third_party/quiche/src/quic/core/quic_config.h"
11#include "net/third_party/quiche/src/quic/platform/api/quic_containers.h"
12#include "net/third_party/quiche/src/quic/platform/api/quic_mutex.h"
13#include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h"
14#include "net/third_party/quiche/src/quic/platform/api/quic_thread.h"
15#include "net/third_party/quiche/src/quic/tools/quic_server.h"
16
17namespace quic {
18namespace test {
19
20// Simple wrapper class to run QuicServer in a dedicated thread.
21class ServerThread : public QuicThread {
22 public:
23 ServerThread(QuicServer* server, const QuicSocketAddress& address);
24 ServerThread(const ServerThread&) = delete;
25 ServerThread& operator=(const ServerThread&) = delete;
26
27 ~ServerThread() override;
28
29 // Prepares the server, but does not start accepting connections. Useful for
30 // injecting mocks.
31 void Initialize();
32
33 // Runs the event loop. Will initialize if necessary.
34 void Run() override;
35
36 // Schedules the given action for execution in the event loop.
37 void Schedule(std::function<void()> action);
38
39 // Waits for the handshake to be confirmed for the first session created.
40 void WaitForCryptoHandshakeConfirmed();
41
42 // Pauses execution of the server until Resume() is called. May only be
43 // called once.
44 void Pause();
45
46 // Resumes execution of the server after Pause() has been called. May only
47 // be called once.
48 void Resume();
49
50 // Stops the server from executing and shuts it down, destroying all
51 // server objects.
52 void Quit();
53
54 // Returns the underlying server. Care must be taken to avoid data races
55 // when accessing the server. It is always safe to access the server
56 // after calling Pause() and before calling Resume().
57 QuicServer* server() { return server_.get(); }
58
59 // Returns the port that the server is listening on.
60 int GetPort();
61
62 private:
63 void MaybeNotifyOfHandshakeConfirmation();
64 void ExecuteScheduledActions();
65
66 QuicNotification
67 confirmed_; // Notified when the first handshake is confirmed.
68 QuicNotification pause_; // Notified when the server should pause.
69 QuicNotification paused_; // Notitied when the server has paused
70 QuicNotification resume_; // Notified when the server should resume.
71 QuicNotification quit_; // Notified when the server should quit.
72
73 std::unique_ptr<QuicServer> server_;
74 QuicSocketAddress address_;
75 mutable QuicMutex port_lock_;
rch52cb79f2019-08-30 13:35:57 -070076 int port_ QUIC_GUARDED_BY(port_lock_);
QUICHE teama6ef0a62019-03-07 20:34:33 -050077
78 bool initialized_;
79
80 QuicMutex scheduled_actions_lock_;
81 QuicDeque<std::function<void()>> scheduled_actions_
rch52cb79f2019-08-30 13:35:57 -070082 QUIC_GUARDED_BY(scheduled_actions_lock_);
QUICHE teama6ef0a62019-03-07 20:34:33 -050083};
84
85} // namespace test
86} // namespace quic
87
88#endif // QUICHE_QUIC_TEST_TOOLS_SERVER_THREAD_H_