Changeset 249158 in webkit
- Timestamp:
- Aug 27, 2019 11:59:29 AM (5 years ago)
- Location:
- trunk/Source
- Files:
-
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/JavaScriptCore/ChangeLog
r249132 r249158 1 2019-08-27 Basuke Suzuki <Basuke.Suzuki@sony.com> 2 3 [RemoteInspector][Socket] Restructuring the components of Socket implementation 4 https://bugs.webkit.org/show_bug.cgi?id=201079 5 6 Reviewed by Ross Kirsling. 7 8 Since the change for WeakPtr on r248386, our port start assertion failure on the usage of 9 RemoteInspectorSocketEndpoint. We have to send a message to connection client, but if that 10 has to be done in the same thread which weakPtr generated, it's a little bit stronger 11 restriction for us to handle. In this restructure, we are stopping to use weakPtr to 12 resolve circular dependency, but using a reference with invalidation method because 13 everything is under our control. 14 15 - Make SocketEndpoint a singleton. This class represents a central place to handle socket 16 connections and there's no need to instantiate more than one in a process. Once every 17 connection goes away, it just start sleeping until next connection is created. Very low 18 resource usage when it is idle. 19 - Move Socket::Connection structure from global definition to SocketEndpoint local 20 structure. It is directly used in SocketEndpoint privately. 21 - Move responsibility to handle message encoding/decoding task from SocketEndpoint to 22 ConnectionClient. Make SocketEndpoint as plain socket handling as possible to keep it 23 simple to exist long span. 24 - Extract an interface from ConnectionClient as SocketEndpoint::Client which is required 25 to work with SocketEndpoint. Now SocketEndpoint is very independent from others. 26 SocketEndpoint::Client is the required parameter to create a connection. 27 28 Many responsibilities are moved into ConnectionClient which was a thin interface for 29 communication between RemoteInspector, RemoteInspectorServer and RemoteInspectorClient. 30 It now handles followings: 31 - life cycle of connection: create, listen and close or invalidation 32 - sending and receiving data packed in a message. 33 34 RemoteInspector and RemoteInspectorServer are now free from creation of SocketEndpoint. 35 All communication to SocketEndpoint id now the duty of super class. 36 37 * inspector/remote/RemoteInspector.h: 38 * inspector/remote/socket/RemoteInspectorConnectionClient.cpp: 39 (Inspector::RemoteInspectorConnectionClient::~RemoteInspectorConnectionClient): Make all connection invalidated. 40 (Inspector::RemoteInspectorConnectionClient::connectInet): Add itself as a listener of socket. 41 (Inspector::RemoteInspectorConnectionClient::listenInet): Ditto. 42 (Inspector::RemoteInspectorConnectionClient::createClient): Ditto. 43 (Inspector::RemoteInspectorConnectionClient::send): Add message processing. 44 (Inspector::RemoteInspectorConnectionClient::didReceive): Ditto. 45 (Inspector::RemoteInspectorConnectionClient::extractEvent): Extracted from send. 46 * inspector/remote/socket/RemoteInspectorConnectionClient.h: 47 * inspector/remote/socket/RemoteInspectorMessageParser.cpp: 48 (Inspector::MessageParser::MessageParser): 49 (Inspector::MessageParser::pushReceivedData): 50 (Inspector::MessageParser::parse): 51 * inspector/remote/socket/RemoteInspectorMessageParser.h: 52 (Inspector::MessageParser::MessageParser): 53 (Inspector::MessageParser::Function<void): 54 * inspector/remote/socket/RemoteInspectorServer.cpp: 55 (Inspector::RemoteInspectorServer::connect): Remove direct communication to Socket Endpoint. 56 (Inspector::RemoteInspectorServer::listenForTargets): Ditto. 57 (Inspector::RemoteInspectorServer::sendWebInspectorEvent): Ditto. 58 (Inspector::RemoteInspectorServer::start): Ditto. 59 * inspector/remote/socket/RemoteInspectorServer.h: 60 * inspector/remote/socket/RemoteInspectorSocket.cpp: 61 (Inspector::RemoteInspector::sendWebInspectorEvent): Remove direct communication to Socket Endpoint. 62 (Inspector::RemoteInspector::start): Ditto. 63 (Inspector::RemoteInspector::stopInternal): Ditto. 64 (Inspector::RemoteInspector::pushListingsNow): Change the target of validity check to ID. 65 (Inspector::RemoteInspector::pushListingsSoon): Ditto. 66 (Inspector::RemoteInspector::sendMessageToRemote): Ditto. 67 * inspector/remote/socket/RemoteInspectorSocket.h: Move Connection structure to RemoteInspectorSocketEndpoint. 68 * inspector/remote/socket/RemoteInspectorSocketEndpoint.cpp: 69 (Inspector::RemoteInspectorSocketEndpoint::singleton): Added. 70 (Inspector::RemoteInspectorSocketEndpoint::RemoteInspectorSocketEndpoint): Use hard-coded thread name. 71 (Inspector::RemoteInspectorSocketEndpoint::connectInet): Accept RemoteInspectorSocketEndpoint::Client as listener. 72 (Inspector::RemoteInspectorSocketEndpoint::listenInet): Ditto. 73 (Inspector::RemoteInspectorSocketEndpoint::createClient): Ditto. 74 (Inspector::RemoteInspectorSocketEndpoint::invalidateClient): Added. Invalidate all connection from the client. 75 (Inspector::RemoteInspectorSocketEndpoint::recvIfEnabled): Remove message parser handling. 76 (Inspector::RemoteInspectorSocketEndpoint::send): Remove message packing. 77 (Inspector::RemoteInspectorSocketEndpoint::acceptInetSocketIfEnabled): 78 * inspector/remote/socket/RemoteInspectorSocketEndpoint.h: 79 (Inspector::RemoteInspectorSocketEndpoint::Connection::Connection): 80 1 81 2019-08-26 Devin Rousso <drousso@apple.com> 2 82 -
trunk/Source/JavaScriptCore/inspector/remote/RemoteInspector.h
r244919 r249158 57 57 #include "RemoteConnectionToTarget.h" 58 58 #include "RemoteInspectorConnectionClient.h" 59 #include "RemoteInspectorSocketEndpoint.h"60 59 #include <wtf/JSONValues.h> 61 60 #include <wtf/RefCounted.h> … … 242 241 243 242 #if USE(INSPECTOR_SOCKET_SERVER) 244 std::unique_ptr<RemoteInspectorSocketEndpoint> m_socketConnection;245 243 static PlatformSocketType s_connectionIdentifier; 246 244 static uint16_t s_serverPort; -
trunk/Source/JavaScriptCore/inspector/remote/socket/RemoteInspectorConnectionClient.cpp
r245536 r249158 35 35 namespace Inspector { 36 36 37 void RemoteInspectorConnectionClient::didReceiveWebInspectorEvent(ConnectionID clientID, Vector<uint8_t>&& data) 37 RemoteInspectorConnectionClient::~RemoteInspectorConnectionClient() 38 { 39 auto& endpoint = Inspector::RemoteInspectorSocketEndpoint::singleton(); 40 endpoint.invalidateClient(*this); 41 } 42 43 Optional<ConnectionID> RemoteInspectorConnectionClient::connectInet(const char* serverAddr, uint16_t serverPort) 44 { 45 auto& endpoint = Inspector::RemoteInspectorSocketEndpoint::singleton(); 46 return endpoint.connectInet(serverAddr, serverPort, *this); 47 } 48 49 Optional<ConnectionID> RemoteInspectorConnectionClient::listenInet(const char* address, uint16_t port) 50 { 51 auto& endpoint = Inspector::RemoteInspectorSocketEndpoint::singleton(); 52 return endpoint.listenInet(address, port, *this); 53 } 54 55 Optional<ConnectionID> RemoteInspectorConnectionClient::createClient(PlatformSocketType socket) 56 { 57 auto& endpoint = Inspector::RemoteInspectorSocketEndpoint::singleton(); 58 return endpoint.createClient(socket, *this); 59 } 60 61 void RemoteInspectorConnectionClient::send(ConnectionID id, const uint8_t* data, size_t size) 62 { 63 auto message = MessageParser::createMessage(data, size); 64 if (message.isEmpty()) 65 return; 66 67 auto& endpoint = RemoteInspectorSocketEndpoint::singleton(); 68 endpoint.send(id, message.data(), message.size()); 69 } 70 71 void RemoteInspectorConnectionClient::didReceive(ConnectionID clientID, Vector<uint8_t>&& data) 38 72 { 39 73 ASSERT(!isMainThread()); 40 74 75 LockHolder lock(m_parsersLock); 76 auto result = m_parsers.ensure(clientID, [this, clientID] { 77 return MessageParser([this, clientID](Vector<uint8_t>&& data) { 78 if (auto event = RemoteInspectorConnectionClient::extractEvent(clientID, WTFMove(data))) { 79 RunLoop::main().dispatch([this, event = WTFMove(*event)] { 80 const auto& methodName = event.methodName; 81 auto& methods = dispatchMap(); 82 if (methods.contains(methodName)) { 83 auto call = methods.get(methodName); 84 (this->*call)(event); 85 } else 86 LOG_ERROR("Unknown event: %s", methodName.utf8().data()); 87 }); 88 } 89 }); 90 }); 91 result.iterator->value.pushReceivedData(data.data(), data.size()); 92 } 93 94 Optional<RemoteInspectorConnectionClient::Event> RemoteInspectorConnectionClient::extractEvent(ConnectionID clientID, Vector<uint8_t>&& data) 95 { 41 96 if (data.isEmpty()) 42 return ;97 return WTF::nullopt; 43 98 44 99 String jsonData = String::fromUTF8(data); … … 46 101 RefPtr<JSON::Value> messageValue; 47 102 if (!JSON::Value::parseJSON(jsonData, messageValue)) 48 return ;103 return WTF::nullopt; 49 104 50 105 RefPtr<JSON::Object> messageObject; 51 106 if (!messageValue->asObject(messageObject)) 52 return; 53 54 String methodName; 55 if (!messageObject->getString("event"_s, methodName)) 56 return; 107 return WTF::nullopt; 57 108 58 109 Event event; 110 if (!messageObject->getString("event"_s, event.methodName)) 111 return WTF::nullopt; 112 59 113 event.clientID = clientID; 60 114 … … 71 125 event.message = message; 72 126 73 RunLoop::main().dispatch([this, methodName, event = WTFMove(event)] { 74 auto& methods = dispatchMap(); 75 if (methods.contains(methodName)) { 76 auto call = methods.get(methodName); 77 (this->*call)(event); 78 } else 79 LOG_ERROR("Unknown event: %s", methodName.utf8().data()); 80 }); 127 return event; 81 128 } 82 129 -
trunk/Source/JavaScriptCore/inspector/remote/socket/RemoteInspectorConnectionClient.h
r246299 r249158 29 29 30 30 #include "RemoteControllableTarget.h" 31 #include "RemoteInspectorMessageParser.h" 31 32 #include "RemoteInspectorSocketEndpoint.h" 32 #include <wtf/WeakPtr.h> 33 #include <wtf/HashMap.h> 34 #include <wtf/Lock.h> 33 35 #include <wtf/text/WTFString.h> 34 36 35 37 namespace Inspector { 36 38 37 class RemoteInspectorConnectionClient : public CanMakeWeakPtr<RemoteInspectorConnectionClient> { 39 class MessageParser; 40 41 class JS_EXPORT_PRIVATE RemoteInspectorConnectionClient : public RemoteInspectorSocketEndpoint::Client { 38 42 public: 39 void didReceiveWebInspectorEvent(ConnectionID, Vector<uint8_t>&&); 40 virtual void didAccept(ConnectionID /* acceptedID */, ConnectionID /* listenerID */, Socket::Domain) { } 41 virtual void didClose(ConnectionID) = 0; 43 virtual ~RemoteInspectorConnectionClient(); 44 45 Optional<ConnectionID> connectInet(const char* serverAddr, uint16_t serverPort); 46 Optional<ConnectionID> listenInet(const char* address, uint16_t port); 47 Optional<ConnectionID> createClient(PlatformSocketType); 48 void send(ConnectionID, const uint8_t* data, size_t); 49 50 void didReceive(ConnectionID, Vector<uint8_t>&&) override; 51 void didAccept(ConnectionID, ConnectionID, Socket::Domain) override { } 42 52 43 53 struct Event { 54 String methodName; 44 55 ConnectionID clientID { }; 45 56 Optional<ConnectionID> connectionID; … … 50 61 using CallHandler = void (RemoteInspectorConnectionClient::*)(const Event&); 51 62 virtual HashMap<String, CallHandler>& dispatchMap() = 0; 63 64 protected: 65 static Optional<Event> extractEvent(ConnectionID, Vector<uint8_t>&&); 66 67 HashMap<ConnectionID, MessageParser> m_parsers; 68 Lock m_parsersLock; 52 69 }; 53 70 -
trunk/Source/JavaScriptCore/inspector/remote/socket/RemoteInspectorMessageParser.cpp
r244657 r249158 42 42 */ 43 43 44 MessageParser::MessageParser( ConnectionID id, size_t bufferSize)45 : m_ connectionID(id)44 MessageParser::MessageParser(Function<void(Vector<uint8_t>&&)>&& listener) 45 : m_listener(WTFMove(listener)) 46 46 { 47 m_buffer.reserveCapacity(bufferSize);48 47 } 49 48 … … 63 62 void MessageParser::pushReceivedData(const uint8_t* data, size_t size) 64 63 { 65 if (!data || !size )64 if (!data || !size || !m_listener) 66 65 return; 67 66 … … 104 103 memcpy(&dataBuffer[0], &m_buffer[sizeof(uint32_t)], dataSize); 105 104 106 if (m_didParseMessageListener) 107 m_didParseMessageListener(m_connectionID, WTFMove(dataBuffer)); 105 m_listener(WTFMove(dataBuffer)); 108 106 109 107 m_buffer.remove(0, messageSize); -
trunk/Source/JavaScriptCore/inspector/remote/socket/RemoteInspectorMessageParser.h
r248546 r249158 28 28 #if ENABLE(REMOTE_INSPECTOR) 29 29 30 #include "RemoteInspectorSocketEndpoint.h"30 #include <wtf/Function.h> 31 31 #include <wtf/Vector.h> 32 32 … … 38 38 static Vector<uint8_t> createMessage(const uint8_t*, size_t); 39 39 40 MessageParser(ConnectionID, size_t); 40 MessageParser() { } 41 MessageParser(Function<void(Vector<uint8_t>&&)>&&); 41 42 void pushReceivedData(const uint8_t*, size_t); 42 void setDidParseMessageListener(Function<void(ConnectionID, Vector<uint8_t>)>&& listener) { m_didParseMessageListener = WTFMove(listener); }43 44 43 void clearReceivedData(); 45 44 … … 47 46 bool parse(); 48 47 49 Function<void( ConnectionID, Vector<uint8_t>&&)> m_didParseMessageListener;48 Function<void(Vector<uint8_t>&&)> m_listener { }; 50 49 Vector<uint8_t> m_buffer; 51 ConnectionID m_connectionID;52 50 }; 53 51 -
trunk/Source/JavaScriptCore/inspector/remote/socket/RemoteInspectorServer.cpp
r246860 r249158 29 29 #if ENABLE(REMOTE_INSPECTOR) 30 30 31 #include "RemoteInspectorMessageParser.h" 32 31 33 #include <wtf/JSONValues.h> 32 34 #include <wtf/MainThread.h> … … 36 38 Optional<PlatformSocketType> RemoteInspectorServer::connect() 37 39 { 38 if (!m_server) {39 LOG_ERROR("Inspector server is not running");40 return WTF::nullopt;41 }42 43 40 if (auto sockets = Socket::createPair()) { 44 if (auto id = m_server->createClient(sockets->at(0))) {41 if (auto id = createClient(sockets->at(0))) { 45 42 LockHolder lock(m_connectionsLock); 46 43 m_inspectorConnections.append(id.value()); … … 55 52 Optional<uint16_t> RemoteInspectorServer::listenForTargets() 56 53 { 57 if (!m_server) {58 LOG_ERROR("Inspector server is not running");59 return WTF::nullopt;60 }61 62 54 if (m_inspectorListener) { 63 55 LOG_ERROR("Inspector server is already listening for targets."); … … 65 57 } 66 58 67 if (auto connection = m_server->listenInet("127.0.0.1", 0)) {59 if (auto connection = listenInet("127.0.0.1", 0)) { 68 60 m_inspectorListener = connection; 69 return m_server->getPort(*connection); 61 62 auto& endpoint = RemoteInspectorSocketEndpoint::singleton(); 63 return endpoint.getPort(*connection); 70 64 } 71 65 … … 124 118 { 125 119 const CString message = event.utf8(); 126 m_server->send(id, reinterpret_cast<const uint8_t*>(message.data()), message.length());120 send(id, reinterpret_cast<const uint8_t*>(message.data()), message.length()); 127 121 } 128 122 … … 135 129 bool RemoteInspectorServer::start(const char* address, uint16_t port) 136 130 { 137 m_server = RemoteInspectorSocketEndpoint::create(this, "RemoteInspectorServer"); 138 139 if (!m_server->listenInet(address, port)) { 140 m_server = nullptr; 141 return false; 142 } 143 144 return true; 131 m_server = listenInet(address, port); 132 return isRunning(); 145 133 } 146 134 -
trunk/Source/JavaScriptCore/inspector/remote/socket/RemoteInspectorServer.h
r246860 r249158 71 71 72 72 HashSet<std::pair<ConnectionID, TargetID>> m_inspectionTargets; 73 std::unique_ptr<RemoteInspectorSocketEndpoint> m_server; 73 74 Optional<ConnectionID> m_server; 74 75 75 76 // Connections to the WebProcess. … … 80 81 Optional<ConnectionID> m_inspectorListener; 81 82 82 // Connection sfrom RemoteInspectorClient.83 // Connection from RemoteInspectorClient. 83 84 Optional<ConnectionID> m_clientConnection; 84 85 }; -
trunk/Source/JavaScriptCore/inspector/remote/socket/RemoteInspectorSocket.cpp
r245726 r249158 82 82 83 83 const CString message = event.utf8(); 84 m_socketConnection->send(m_clientID.value(), reinterpret_cast<const uint8_t*>(message.data()), message.length());84 send(m_clientID.value(), reinterpret_cast<const uint8_t*>(message.data()), message.length()); 85 85 } 86 86 … … 94 94 m_enabled = true; 95 95 96 m_socketConnection = RemoteInspectorSocketEndpoint::create(this, "RemoteInspector");97 98 96 if (s_connectionIdentifier) { 99 m_clientID = m_socketConnection->createClient(s_connectionIdentifier);97 m_clientID = createClient(s_connectionIdentifier); 100 98 s_connectionIdentifier = INVALID_SOCKET_VALUE; 101 99 } else 102 m_clientID = m_socketConnection->connectInet("127.0.0.1", s_serverPort);100 m_clientID = connectInet("127.0.0.1", s_serverPort); 103 101 104 102 if (!m_targetMap.isEmpty()) … … 121 119 122 120 m_automaticInspectionPaused = false; 123 m_socketConnection = nullptr;124 121 m_clientID = WTF::nullopt; 125 122 } … … 157 154 void RemoteInspector::pushListingsNow() 158 155 { 159 if (!m_ socketConnection)156 if (!m_clientID) 160 157 return; 161 158 … … 174 171 void RemoteInspector::pushListingsSoon() 175 172 { 176 if (!m_ socketConnection)173 if (!m_clientID) 177 174 return; 178 175 … … 196 193 { 197 194 LockHolder lock(m_mutex); 198 if (!m_ socketConnection)195 if (!m_clientID) 199 196 return; 200 197 -
trunk/Source/JavaScriptCore/inspector/remote/socket/RemoteInspectorSocket.h
r248546 r249158 32 32 #include <wtf/Vector.h> 33 33 34 #if PLATFORM(PLAYSTATION)35 #include <poll.h>36 #endif37 38 34 #if OS(WINDOWS) 39 35 #include <winsock2.h> 36 #else 37 #include <poll.h> 40 38 #endif 41 39 … … 57 55 58 56 #endif 59 60 class MessageParser;61 57 62 58 namespace Socket { … … 91 87 void clearWaitingWritable(PollingDescriptor&); 92 88 93 struct Connection {94 WTF_MAKE_STRUCT_FAST_ALLOCATED;95 std::unique_ptr<MessageParser> parser;96 Vector<uint8_t> sendBuffer;97 PlatformSocketType socket { INVALID_SOCKET_VALUE };98 PollingDescriptor poll;99 };100 101 89 constexpr size_t BufferSize = 65536; 102 90 -
trunk/Source/JavaScriptCore/inspector/remote/socket/RemoteInspectorSocketEndpoint.cpp
r248846 r249158 29 29 #if ENABLE(REMOTE_INSPECTOR) 30 30 31 #include "RemoteInspectorConnectionClient.h"32 #include "RemoteInspectorMessageParser.h"33 31 #include <wtf/CryptographicallyRandomNumber.h> 34 32 #include <wtf/MainThread.h> … … 37 35 namespace Inspector { 38 36 39 RemoteInspectorSocketEndpoint::RemoteInspectorSocketEndpoint(RemoteInspectorConnectionClient* inspectorClient, const char* name) 40 : m_inspectorClient(makeWeakPtr(inspectorClient)) 37 RemoteInspectorSocketEndpoint& RemoteInspectorSocketEndpoint::singleton() 38 { 39 static NeverDestroyed<RemoteInspectorSocketEndpoint> shared; 40 return shared; 41 } 42 43 RemoteInspectorSocketEndpoint::RemoteInspectorSocketEndpoint() 41 44 { 42 45 if (auto sockets = Socket::createPair()) { … … 45 48 } 46 49 47 m_workerThread = Thread::create( name, [this] {50 m_workerThread = Thread::create("SocketEndpoint", [this] { 48 51 workerThread(); 49 52 }); … … 70 73 } 71 74 72 Optional<ConnectionID> RemoteInspectorSocketEndpoint::connectInet(const char* serverAddress, uint16_t serverPort )75 Optional<ConnectionID> RemoteInspectorSocketEndpoint::connectInet(const char* serverAddress, uint16_t serverPort, Client& client) 73 76 { 74 77 if (auto socket = Socket::connect(serverAddress, serverPort)) 75 return createClient(*socket );78 return createClient(*socket, client); 76 79 return WTF::nullopt; 77 80 } 78 81 79 Optional<ConnectionID> RemoteInspectorSocketEndpoint::listenInet(const char* address, uint16_t port )82 Optional<ConnectionID> RemoteInspectorSocketEndpoint::listenInet(const char* address, uint16_t port, Client& client) 80 83 { 81 84 if (auto socket = Socket::listen(address, port)) 82 return createClient(*socket );85 return createClient(*socket, client); 83 86 84 87 return WTF::nullopt; … … 131 134 } 132 135 133 Optional<ConnectionID> RemoteInspectorSocketEndpoint::createClient(PlatformSocketType socket )136 Optional<ConnectionID> RemoteInspectorSocketEndpoint::createClient(PlatformSocketType socket, Client& client) 134 137 { 135 138 if (!Socket::isValid(socket)) … … 145 148 Socket::setup(socket); 146 149 147 auto connection = makeUnique<Socket::Connection>(); 148 150 auto connection = makeUnique<Connection>(client); 151 152 connection->id = id; 149 153 connection->poll = Socket::preparePolling(socket); 150 154 connection->socket = socket; 151 connection->parser = makeUnique<MessageParser>(id, Socket::BufferSize);152 connection->parser->setDidParseMessageListener([this](ConnectionID id, Vector<uint8_t>&& data) {153 if (m_inspectorClient)154 m_inspectorClient->didReceiveWebInspectorEvent(id, WTFMove(data));155 });156 155 m_connections.add(id, WTFMove(connection)); 157 156 wakeupWorkerThread(); … … 160 159 } 161 160 161 void RemoteInspectorSocketEndpoint::invalidateClient(Client& client) 162 { 163 LockHolder lock(m_connectionsLock); 164 m_connections.removeIf([&client](auto& keyValue) { 165 const auto& connection = keyValue.value; 166 167 if (&connection->client != &client) 168 return false; 169 170 Socket::close(connection->socket); 171 // do not call client.didClose because client is already invalidating phase. 172 return true; 173 }); 174 } 175 162 176 Optional<uint16_t> RemoteInspectorSocketEndpoint::getPort(ConnectionID id) const 163 177 { … … 175 189 Vector<uint8_t> recvBuffer(Socket::BufferSize); 176 190 if (auto readSize = Socket::read(connection->socket, recvBuffer.data(), recvBuffer.size())) { 177 if (*readSize > 0) 178 connection->parser->pushReceivedData(recvBuffer.data(), *readSize); 179 return; 191 if (*readSize > 0) { 192 recvBuffer.shrink(*readSize); 193 connection->client.didReceive(id, WTFMove(recvBuffer)); 194 return; 195 } 180 196 } 181 197 … … 184 200 185 201 lock.unlockEarly(); 186 if (m_inspectorClient) 187 m_inspectorClient->didClose(id); 202 connection->client.didClose(id); 188 203 } 189 204 } … … 218 233 LockHolder lock(m_connectionsLock); 219 234 if (const auto& connection = m_connections.get(id)) { 220 auto message = MessageParser::createMessage(data, size);221 if (message.isEmpty())222 return;223 224 235 size_t offset = 0; 225 236 if (connection->sendBuffer.isEmpty()) { 226 237 // Try to call send() directly if buffer is empty. 227 if (auto writeSize = Socket::write(connection->socket, message.data(), std::min(message.size(), Socket::BufferSize)))238 if (auto writeSize = Socket::write(connection->socket, data, std::min(size, Socket::BufferSize))) 228 239 offset = *writeSize; 229 240 // @TODO need to handle closed socket case? … … 231 242 232 243 // Check all data is sent. 233 if (offset == message.size())244 if (offset == size) 234 245 return; 235 246 236 247 // Copy remaining data to send later. 237 connection->sendBuffer.appendRange( message.begin() + offset, message.end());248 connection->sendBuffer.appendRange(data + offset, data + size); 238 249 Socket::markWaitingWritable(connection->poll); 239 250 … … 252 263 // Need to unlock before calling createClient as it also attempts to lock. 253 264 lock.unlockEarly(); 254 if (auto newID = createClient(*socket)) { 255 if (m_inspectorClient) { 256 m_inspectorClient->didAccept(newID.value(), id, Socket::Domain::Network); 257 return; 258 } 265 if (auto newID = createClient(*socket, connection->client)) { 266 connection->client.didAccept(newID.value(), id, Socket::Domain::Network); 267 return; 259 268 } 260 269 -
trunk/Source/JavaScriptCore/inspector/remote/socket/RemoteInspectorSocketEndpoint.h
r248846 r249158 36 36 #include <wtf/Threading.h> 37 37 #include <wtf/Vector.h> 38 #include <wtf/WeakPtr.h>39 38 40 39 namespace Inspector { 41 40 42 class MessageParser; 43 class RemoteInspectorConnectionClient; 44 45 class JS_EXPORT_PRIVATE RemoteInspectorSocketEndpoint { 41 class RemoteInspectorSocketEndpoint { 46 42 WTF_MAKE_FAST_ALLOCATED; 47 43 public: 48 static std::unique_ptr<RemoteInspectorSocketEndpoint> create(RemoteInspectorConnectionClient* inspectorClient, const char* name) 49 { 50 return makeUnique<RemoteInspectorSocketEndpoint>(inspectorClient, name); 51 } 44 class Client { 45 public: 46 virtual void didReceive(ConnectionID, Vector<uint8_t>&&) = 0; 47 virtual void didAccept(ConnectionID acceptedID, ConnectionID listenerID, Socket::Domain) = 0; 48 virtual void didClose(ConnectionID) = 0; 49 }; 52 50 53 RemoteInspectorSocketEndpoint(RemoteInspectorConnectionClient*, const char*); 51 static RemoteInspectorSocketEndpoint& singleton(); 52 53 RemoteInspectorSocketEndpoint(); 54 54 ~RemoteInspectorSocketEndpoint(); 55 55 56 Optional<ConnectionID> connectInet(const char* serverAddr, uint16_t serverPort); 57 Optional<ConnectionID> listenInet(const char* address, uint16_t port); 56 Optional<ConnectionID> connectInet(const char* serverAddr, uint16_t serverPort, Client&); 57 Optional<ConnectionID> listenInet(const char* address, uint16_t port, Client&); 58 void invalidateClient(Client&); 58 59 59 60 void send(ConnectionID, const uint8_t* data, size_t); 60 61 61 Optional<ConnectionID> createClient(PlatformSocketType fd);62 Optional<ConnectionID> createClient(PlatformSocketType, Client&); 62 63 63 64 Optional<uint16_t> getPort(ConnectionID) const; 64 65 65 66 protected: 67 struct Connection { 68 WTF_MAKE_STRUCT_FAST_ALLOCATED; 69 explicit Connection(Client& client) 70 : client(client) 71 { 72 } 73 74 ConnectionID id; 75 Vector<uint8_t> sendBuffer; 76 PlatformSocketType socket { INVALID_SOCKET_VALUE }; 77 PollingDescriptor poll; 78 Client& client; 79 }; 80 66 81 void recvIfEnabled(ConnectionID); 67 82 void sendIfEnabled(ConnectionID); … … 72 87 73 88 mutable Lock m_connectionsLock; 74 HashMap<ConnectionID, std::unique_ptr< Socket::Connection>> m_connections;89 HashMap<ConnectionID, std::unique_ptr<Connection>> m_connections; 75 90 76 91 PlatformSocketType m_wakeupSendSocket { INVALID_SOCKET_VALUE }; … … 79 94 RefPtr<Thread> m_workerThread; 80 95 std::atomic<bool> m_shouldAbortWorkerThread { false }; 81 82 WeakPtr<RemoteInspectorConnectionClient> m_inspectorClient;83 96 }; 84 97 -
trunk/Source/WebKit/ChangeLog
r249155 r249158 1 2019-08-27 Basuke Suzuki <Basuke.Suzuki@sony.com> 2 3 [RemoteInspector][Socket] Restructuring the components of Socket implementation 4 https://bugs.webkit.org/show_bug.cgi?id=201079 5 6 Reviewed by Ross Kirsling. 7 8 RemoteInspectorClient is now free from creation of SocketEndpoint. All communication 9 to SocketEndpoint id now the duty of super class. 10 11 * UIProcess/socket/RemoteInspectorClient.cpp: 12 (WebKit::RemoteInspectorClient::RemoteInspectorClient): Remove direct communication to Socket Endpoint. 13 (WebKit::RemoteInspectorClient::sendWebInspectorEvent): Ditto. 14 * UIProcess/socket/RemoteInspectorClient.h: 15 1 16 2019-08-27 Chris Dumez <cdumez@apple.com> 2 17 -
trunk/Source/WebKit/UIProcess/socket/RemoteInspectorClient.cpp
r248846 r249158 89 89 : m_observer(observer) 90 90 { 91 m_socket = Inspector::RemoteInspectorSocketEndpoint::create(this, "RemoteInspectorClient"); 92 93 m_connectionID = m_socket->connectInet(address, port); 91 m_connectionID = connectInet(address, port); 94 92 if (!m_connectionID) { 95 93 LOG_ERROR("Inspector client could not connect to %s:%d", address, port); 96 m_socket = nullptr;97 94 return; 98 95 } … … 110 107 ASSERT(m_connectionID.hasValue()); 111 108 auto message = event.utf8(); 112 m_socket->send(m_connectionID.value(), reinterpret_cast<const uint8_t*>(message.data()), message.length());109 send(m_connectionID.value(), reinterpret_cast<const uint8_t*>(message.data()), message.length()); 113 110 } 114 111 -
trunk/Source/WebKit/UIProcess/socket/RemoteInspectorClient.h
r245726 r249158 30 30 #include <JavaScriptCore/RemoteControllableTarget.h> 31 31 #include <JavaScriptCore/RemoteInspectorConnectionClient.h> 32 #include <JavaScriptCore/RemoteInspectorSocketEndpoint.h>33 32 #include <wtf/HashMap.h> 34 33 #include <wtf/Vector.h> … … 79 78 void setBackendCommands(const Event&); 80 79 81 void didClose(ConnectionID) override;82 HashMap<String, CallHandler>& dispatchMap() override;80 void didClose(ConnectionID) final; 81 HashMap<String, CallHandler>& dispatchMap() final; 83 82 84 83 void sendWebInspectorEvent(const String&); 85 84 86 85 RemoteInspectorObserver& m_observer; 87 std::unique_ptr<Inspector::RemoteInspectorSocketEndpoint> m_socket;88 86 Optional<ConnectionID> m_connectionID; 89 87 HashMap<ConnectionID, Vector<Target>> m_targets;
Note: See TracChangeset
for help on using the changeset viewer.