-
Notifications
You must be signed in to change notification settings - Fork 107
/
message_pump_io_starboard.h
177 lines (140 loc) · 6.02 KB
/
message_pump_io_starboard.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2018 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef BASE_MESSAGE_PUMP_IO_STARBOARD_H_
#define BASE_MESSAGE_PUMP_IO_STARBOARD_H_
#include "base/compiler_specific.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_pump.h"
#include "base/observer_list.h"
#include "base/threading/thread_checker.h"
#include "base/time/time.h"
#include "starboard/common/socket.h"
#include "starboard/socket_waiter.h"
namespace base {
// Class to monitor sockets and issue callbacks when sockets are ready for I/O.
class BASE_EXPORT MessagePumpIOStarboard : public MessagePump {
public:
class IOObserver : public CheckedObserver {
public:
IOObserver() {}
virtual ~IOObserver() {}
// An IOObserver is an object that receives IO notifications from the
// MessagePump.
//
// NOTE: An IOObserver should not do much work, it should return extremely
// quickly!
virtual void WillProcessIOEvent() = 0;
virtual void DidProcessIOEvent() = 0;
};
// Used with WatchFileDescriptor to asynchronously monitor the I/O readiness
// of a file descriptor.
class Watcher {
public:
// These methods are called from MessageLoop::Run when a socket can be
// interacted with without blocking.
virtual void OnSocketReadyToRead(SbSocket socket) {}
virtual void OnSocketReadyToWrite(SbSocket socket) {}
protected:
virtual ~Watcher() {}
};
// Object returned by WatchSocket to manage further watching.
class SocketWatcher {
public:
SocketWatcher(const Location& from_here);
~SocketWatcher(); // Implicitly calls StopWatchingSocket.
SocketWatcher(const SocketWatcher&) = delete;
SocketWatcher& operator=(const SocketWatcher&) = delete;
// NOTE: These methods aren't called StartWatching()/StopWatching() to avoid
// confusion with the win32 ObjectWatcher class.
// Stops watching the socket, always safe to call. No-op if there's nothing
// to do.
bool StopWatchingSocket();
bool persistent() const { return persistent_; }
private:
friend class MessagePumpIOStarboard;
friend class MessagePumpIOStarboardTest;
// Called by MessagePumpIOStarboard.
void Init(SbSocket socket, bool persistent);
SbSocket Release();
int interests() const { return interests_; }
void set_interests(int interests) { interests_ = interests; }
void set_pump(MessagePumpIOStarboard* pump) { pump_ = pump; }
MessagePumpIOStarboard* pump() const { return pump_; }
void set_watcher(Watcher* watcher) { watcher_ = watcher; }
void OnSocketReadyToRead(SbSocket socket, MessagePumpIOStarboard* pump);
void OnSocketReadyToWrite(SbSocket socket, MessagePumpIOStarboard* pump);
const Location created_from_location_;
int interests_;
SbSocket socket_;
bool persistent_;
MessagePumpIOStarboard* pump_;
Watcher* watcher_;
base::WeakPtrFactory<SocketWatcher> weak_factory_;
};
enum Mode {
WATCH_READ = 1 << 0,
WATCH_WRITE = 1 << 1,
WATCH_READ_WRITE = WATCH_READ | WATCH_WRITE
};
MessagePumpIOStarboard();
virtual ~MessagePumpIOStarboard();
MessagePumpIOStarboard(const MessagePumpIOStarboard&) = delete;
MessagePumpIOStarboard& operator=(const MessagePumpIOStarboard&) = delete;
// Have the current thread's message loop watch for a a situation in which
// reading/writing to the socket can be performed without blocking. Callers
// must provide a preallocated SocketWatcher object which can later be used to
// manage the lifetime of this event. If a SocketWatcher is passed in which
// is already attached to a socket, then the effect is cumulative i.e. after
// the call |controller| will watch both the previous event and the new one.
// If an error occurs while calling this method in a cumulative fashion, the
// event previously attached to |controller| is aborted. Returns true on
// success. Must be called on the same thread the message_pump is running on.
bool Watch(SbSocket socket,
bool persistent,
int mode,
SocketWatcher* controller,
Watcher* delegate);
// Stops watching the socket.
bool StopWatching(SbSocket socket);
void AddIOObserver(IOObserver* obs);
void RemoveIOObserver(IOObserver* obs);
// MessagePump methods:
virtual void Run(Delegate* delegate) override;
virtual void Quit() override;
virtual void ScheduleWork() override;
virtual void ScheduleDelayedWork(const Delegate::NextWorkInfo& next_work_info) override;
private:
friend class MessagePumpIOStarboardTest;
void WillProcessIOEvent();
void DidProcessIOEvent();
// Called by SbSocketWaiter to tell us a registered socket can be read and/or
// written to.
static void OnSocketWaiterNotification(SbSocketWaiter waiter,
SbSocket socket,
void* context,
int ready_interests);
bool should_quit() const { return !keep_running_; }
// This flag is set to false when Run should return.
bool keep_running_;
// This flag is set if the Socket Waiter has processed I/O events.
bool processed_io_events_;
// Starboard socket waiter dispatcher. Waits for all sockets registered with
// it, and sends readiness callbacks when a socket is ready for I/O.
SbSocketWaiter waiter_;
ObserverList<IOObserver> io_observers_;
THREAD_CHECKER(watch_socket_caller_checker_);
};
using MessagePumpForIO = MessagePumpIOStarboard;
} // namespace base
#endif // BASE_MESSAGE_PUMP_IO_STARBOARD_H_