|
| 1 | +#include "InspectorServer.h" |
| 2 | +#include <Foundation/Foundation.h> |
| 3 | +#include <netinet/in.h> |
| 4 | +#include <sys/socket.h> |
| 5 | + |
| 6 | +namespace v8_inspector { |
| 7 | + |
| 8 | +in_port_t InspectorServer::Init(std::function<void (std::function<void (std::string)>)> onClientConnected, std::function<void (std::string)> onMessage) { |
| 9 | + in_port_t listenPort = 18183; |
| 10 | + |
| 11 | + int serverSocket = -1; |
| 12 | + if ((serverSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { |
| 13 | + assert(false); |
| 14 | + } |
| 15 | + |
| 16 | + struct sockaddr_in serverAddress; |
| 17 | + memset(&serverAddress, 0, sizeof(serverAddress)); |
| 18 | + serverAddress.sin_family = AF_INET; |
| 19 | + serverAddress.sin_addr.s_addr = htonl(INADDR_ANY); |
| 20 | + do { |
| 21 | + serverAddress.sin_port = htons(listenPort); |
| 22 | + } while (bind(serverSocket, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) < 0 && ++listenPort); |
| 23 | + |
| 24 | + // Make the socket non-blocking |
| 25 | + if (fcntl(serverSocket, F_SETFL, O_NONBLOCK) < 0) { |
| 26 | + shutdown(serverSocket, SHUT_RDWR); |
| 27 | + close(serverSocket); |
| 28 | + assert(false); |
| 29 | + } |
| 30 | + |
| 31 | + // Set up the dispatch source that will alert us to new incoming connections |
| 32 | + dispatch_queue_t q = dispatch_queue_create("server_queue", DISPATCH_QUEUE_CONCURRENT); |
| 33 | + dispatch_source_t acceptSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, serverSocket, 0, q); |
| 34 | + dispatch_source_set_event_handler(acceptSource, ^{ |
| 35 | + const unsigned long numPendingConnections = dispatch_source_get_data(acceptSource); |
| 36 | + for (unsigned long i = 0; i < numPendingConnections; i++) { |
| 37 | + int clientSocket = -1; |
| 38 | + struct sockaddr_in echoClntAddr; |
| 39 | + unsigned int clntLen = sizeof(echoClntAddr); |
| 40 | + |
| 41 | + // Wait for a client to connect |
| 42 | + if ((clientSocket = accept(serverSocket, (struct sockaddr *) &echoClntAddr, &clntLen)) >= 0) { |
| 43 | + dispatch_io_t channel = dispatch_io_create(DISPATCH_IO_STREAM, clientSocket, q, ^(int error) { |
| 44 | + if (error) { |
| 45 | + NSLog(@"Error: %s", strerror(error)); |
| 46 | + } |
| 47 | + close(clientSocket); |
| 48 | + }); |
| 49 | + |
| 50 | + onClientConnected([channel, q](std::string message) { |
| 51 | + Send(channel, q, message); |
| 52 | + }); |
| 53 | + |
| 54 | + __block dispatch_io_handler_t receiver = ^(bool done, dispatch_data_t data, int error) { |
| 55 | + if (error) { |
| 56 | + NSLog(@"Error: %s", strerror(error)); |
| 57 | + } |
| 58 | + |
| 59 | + const void* bytes = [(NSData*)data bytes]; |
| 60 | + if (!bytes) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + uint32_t length = ntohl(*(uint32_t*)bytes); |
| 65 | + |
| 66 | + // Configure the channel... |
| 67 | + dispatch_io_set_low_water(channel, length); |
| 68 | + |
| 69 | + // Setup read handler |
| 70 | + dispatch_io_read(channel, 0, length, q, ^(bool done, dispatch_data_t data, int error) { |
| 71 | + BOOL close = NO; |
| 72 | + if (error) { |
| 73 | + NSLog(@"Error: %s", strerror(error)); |
| 74 | + close = YES; |
| 75 | + } |
| 76 | + |
| 77 | + const size_t size = data ? dispatch_data_get_size(data) : 0; |
| 78 | + if (size) { |
| 79 | + NSString* payload = [[NSString alloc] initWithData:(NSData*)data encoding:NSUTF16LittleEndianStringEncoding]; |
| 80 | + |
| 81 | + onMessage([payload UTF8String]); |
| 82 | + |
| 83 | +#pragma clang diagnostic push |
| 84 | +#pragma clang diagnostic ignored "-Warc-retain-cycles" |
| 85 | + dispatch_io_read(channel, 0, 4, q, receiver); |
| 86 | +#pragma clang diagnostic pop |
| 87 | + } else { |
| 88 | + close = YES; |
| 89 | + } |
| 90 | + |
| 91 | + if (close) { |
| 92 | + dispatch_io_close(channel, DISPATCH_IO_STOP); |
| 93 | + } |
| 94 | + }); |
| 95 | + }; |
| 96 | + |
| 97 | + if (channel) { |
| 98 | + dispatch_io_read(channel, 0, 4, q, receiver); |
| 99 | + } |
| 100 | + } |
| 101 | + else { |
| 102 | + NSLog(@"accept() failed;\n"); |
| 103 | + } |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + // Resume the source so we're ready to accept once we listen() |
| 108 | + dispatch_resume(acceptSource); |
| 109 | + |
| 110 | + // Listen() on the socket |
| 111 | + if (listen(serverSocket, SOMAXCONN) < 0) { |
| 112 | + shutdown(serverSocket, SHUT_RDWR); |
| 113 | + close(serverSocket); |
| 114 | + assert(false); |
| 115 | + } |
| 116 | + |
| 117 | + return listenPort; |
| 118 | +} |
| 119 | + |
| 120 | +void InspectorServer::Send(dispatch_io_t channel, dispatch_queue_t queue, std::string message) { |
| 121 | + NSString* str = [NSString stringWithUTF8String:message.c_str()]; |
| 122 | + NSUInteger length = [str lengthOfBytesUsingEncoding:NSUTF16LittleEndianStringEncoding]; |
| 123 | + |
| 124 | + uint8_t* buffer = (uint8_t*)malloc(length + sizeof(uint32_t)); |
| 125 | + |
| 126 | + *(uint32_t*)buffer = htonl(length); |
| 127 | + |
| 128 | + [str getBytes:&buffer[sizeof(uint32_t)] |
| 129 | + maxLength:length |
| 130 | + usedLength:NULL |
| 131 | + encoding:NSUTF16LittleEndianStringEncoding |
| 132 | + options:0 |
| 133 | + range:NSMakeRange(0, str.length) |
| 134 | + remainingRange:NULL]; |
| 135 | + |
| 136 | + dispatch_data_t data = dispatch_data_create(buffer, length + sizeof(uint32_t), queue, ^{ |
| 137 | + free(buffer); |
| 138 | + }); |
| 139 | + |
| 140 | + dispatch_io_write(channel, 0, data, queue, ^(bool done, dispatch_data_t data, int error) { |
| 141 | + if (error) { |
| 142 | + NSLog(@"Error: %s", strerror(error)); |
| 143 | + } |
| 144 | + }); |
| 145 | +} |
| 146 | + |
| 147 | +} |
0 commit comments