Chris@47: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors Chris@47: // Licensed under the MIT License: Chris@47: // Chris@47: // Permission is hereby granted, free of charge, to any person obtaining a copy Chris@47: // of this software and associated documentation files (the "Software"), to deal Chris@47: // in the Software without restriction, including without limitation the rights Chris@47: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@47: // copies of the Software, and to permit persons to whom the Software is Chris@47: // furnished to do so, subject to the following conditions: Chris@47: // Chris@47: // The above copyright notice and this permission notice shall be included in Chris@47: // all copies or substantial portions of the Software. Chris@47: // Chris@47: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@47: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@47: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@47: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@47: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@47: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@47: // THE SOFTWARE. Chris@47: Chris@47: #ifndef KJ_ASYNC_IO_H_ Chris@47: #define KJ_ASYNC_IO_H_ Chris@47: Chris@47: #if defined(__GNUC__) && !KJ_HEADER_WARNINGS Chris@47: #pragma GCC system_header Chris@47: #endif Chris@47: Chris@47: #include "async.h" Chris@47: #include "function.h" Chris@47: #include "thread.h" Chris@47: #include "time.h" Chris@47: Chris@47: struct sockaddr; Chris@47: Chris@47: namespace kj { Chris@47: Chris@47: class UnixEventPort; Chris@47: class NetworkAddress; Chris@47: Chris@47: // ======================================================================================= Chris@47: // Streaming I/O Chris@47: Chris@47: class AsyncInputStream { Chris@47: // Asynchronous equivalent of InputStream (from io.h). Chris@47: Chris@47: public: Chris@47: virtual Promise read(void* buffer, size_t minBytes, size_t maxBytes) = 0; Chris@47: virtual Promise tryRead(void* buffer, size_t minBytes, size_t maxBytes) = 0; Chris@47: Chris@47: Promise read(void* buffer, size_t bytes); Chris@47: }; Chris@47: Chris@47: class AsyncOutputStream { Chris@47: // Asynchronous equivalent of OutputStream (from io.h). Chris@47: Chris@47: public: Chris@47: virtual Promise write(const void* buffer, size_t size) = 0; Chris@47: virtual Promise write(ArrayPtr> pieces) = 0; Chris@47: }; Chris@47: Chris@47: class AsyncIoStream: public AsyncInputStream, public AsyncOutputStream { Chris@47: // A combination input and output stream. Chris@47: Chris@47: public: Chris@47: virtual void shutdownWrite() = 0; Chris@47: // Cleanly shut down just the write end of the stream, while keeping the read end open. Chris@47: Chris@47: virtual void abortRead() {} Chris@47: // Similar to shutdownWrite, but this will shut down the read end of the stream, and should only Chris@47: // be called when an error has occurred. Chris@47: Chris@47: virtual void getsockopt(int level, int option, void* value, uint* length); Chris@47: virtual void setsockopt(int level, int option, const void* value, uint length); Chris@47: // Corresponds to getsockopt() and setsockopt() syscalls. Will throw an "unimplemented" exception Chris@47: // if the stream is not a socket or the option is not appropriate for the socket type. The Chris@47: // default implementations always throw "unimplemented". Chris@47: Chris@47: virtual void getsockname(struct sockaddr* addr, uint* length); Chris@47: virtual void getpeername(struct sockaddr* addr, uint* length); Chris@47: // Corresponds to getsockname() and getpeername() syscalls. Will throw an "unimplemented" Chris@47: // exception if the stream is not a socket. The default implementations always throw Chris@47: // "unimplemented". Chris@47: // Chris@47: // Note that we don't provide methods that return NetworkAddress because it usually wouldn't Chris@47: // be useful. You can't connect() to or listen() on these addresses, obviously, because they are Chris@47: // ephemeral addresses for a single connection. Chris@47: }; Chris@47: Chris@47: struct OneWayPipe { Chris@47: // A data pipe with an input end and an output end. (Typically backed by pipe() system call.) Chris@47: Chris@47: Own in; Chris@47: Own out; Chris@47: }; Chris@47: Chris@47: struct TwoWayPipe { Chris@47: // A data pipe that supports sending in both directions. Each end's output sends data to the Chris@47: // other end's input. (Typically backed by socketpair() system call.) Chris@47: Chris@47: Own ends[2]; Chris@47: }; Chris@47: Chris@47: class ConnectionReceiver { Chris@47: // Represents a server socket listening on a port. Chris@47: Chris@47: public: Chris@47: virtual Promise> accept() = 0; Chris@47: // Accept the next incoming connection. Chris@47: Chris@47: virtual uint getPort() = 0; Chris@47: // Gets the port number, if applicable (i.e. if listening on IP). This is useful if you didn't Chris@47: // specify a port when constructing the NetworkAddress -- one will have been assigned Chris@47: // automatically. Chris@47: Chris@47: virtual void getsockopt(int level, int option, void* value, uint* length); Chris@47: virtual void setsockopt(int level, int option, const void* value, uint length); Chris@47: // Same as the methods of AsyncIoStream. Chris@47: }; Chris@47: Chris@47: // ======================================================================================= Chris@47: // Datagram I/O Chris@47: Chris@47: class AncillaryMessage { Chris@47: // Represents an ancillary message (aka control message) received using the recvmsg() system Chris@47: // call (or equivalent). Most apps will not use this. Chris@47: Chris@47: public: Chris@47: inline AncillaryMessage(int level, int type, ArrayPtr data); Chris@47: AncillaryMessage() = default; Chris@47: Chris@47: inline int getLevel() const; Chris@47: // Originating protocol / socket level. Chris@47: Chris@47: inline int getType() const; Chris@47: // Protocol-specific message type. Chris@47: Chris@47: template Chris@47: inline Maybe as(); Chris@47: // Interpret the ancillary message as the given struct type. Most ancillary messages are some Chris@47: // sort of struct, so this is a convenient way to access it. Returns nullptr if the message Chris@47: // is smaller than the struct -- this can happen if the message was truncated due to Chris@47: // insufficient ancillary buffer space. Chris@47: Chris@47: template Chris@47: inline ArrayPtr asArray(); Chris@47: // Interpret the ancillary message as an array of items. If the message size does not evenly Chris@47: // divide into elements of type T, the remainder is discarded -- this can happen if the message Chris@47: // was truncated due to insufficient ancillary buffer space. Chris@47: Chris@47: private: Chris@47: int level; Chris@47: int type; Chris@47: ArrayPtr data; Chris@47: // Message data. In most cases you should use `as()` or `asArray()`. Chris@47: }; Chris@47: Chris@47: class DatagramReceiver { Chris@47: // Class encapsulating the recvmsg() system call. You must specify the DatagramReceiver's Chris@47: // capacity in advance; if a received packet is larger than the capacity, it will be truncated. Chris@47: Chris@47: public: Chris@47: virtual Promise receive() = 0; Chris@47: // Receive a new message, overwriting this object's content. Chris@47: // Chris@47: // receive() may reuse the same buffers for content and ancillary data with each call. Chris@47: Chris@47: template Chris@47: struct MaybeTruncated { Chris@47: T value; Chris@47: Chris@47: bool isTruncated; Chris@47: // True if the Receiver's capacity was insufficient to receive the value and therefore the Chris@47: // value is truncated. Chris@47: }; Chris@47: Chris@47: virtual MaybeTruncated> getContent() = 0; Chris@47: // Get the content of the datagram. Chris@47: Chris@47: virtual MaybeTruncated> getAncillary() = 0; Chris@47: // Ancilarry messages received with the datagram. See the recvmsg() system call and the cmsghdr Chris@47: // struct. Most apps don't need this. Chris@47: // Chris@47: // If the returned value is truncated, then the last message in the array may itself be Chris@47: // truncated, meaning its as() method will return nullptr or its asArray() method will Chris@47: // return fewer elements than expected. Truncation can also mean that additional messages were Chris@47: // available but discarded. Chris@47: Chris@47: virtual NetworkAddress& getSource() = 0; Chris@47: // Get the datagram sender's address. Chris@47: Chris@47: struct Capacity { Chris@47: size_t content = 8192; Chris@47: // How much space to allocate for the datagram content. If a datagram is received that is Chris@47: // larger than this, it will be truncated, with no way to recover the tail. Chris@47: Chris@47: size_t ancillary = 0; Chris@47: // How much space to allocate for ancillary messages. As with content, if the ancillary data Chris@47: // is larger than this, it will be truncated. Chris@47: }; Chris@47: }; Chris@47: Chris@47: class DatagramPort { Chris@47: public: Chris@47: virtual Promise send(const void* buffer, size_t size, NetworkAddress& destination) = 0; Chris@47: virtual Promise send(ArrayPtr> pieces, Chris@47: NetworkAddress& destination) = 0; Chris@47: Chris@47: virtual Own makeReceiver( Chris@47: DatagramReceiver::Capacity capacity = DatagramReceiver::Capacity()) = 0; Chris@47: // Create a new `Receiver` that can be used to receive datagrams. `capacity` specifies how much Chris@47: // space to allocate for the received message. The `DatagramPort` must outlive the `Receiver`. Chris@47: Chris@47: virtual uint getPort() = 0; Chris@47: // Gets the port number, if applicable (i.e. if listening on IP). This is useful if you didn't Chris@47: // specify a port when constructing the NetworkAddress -- one will have been assigned Chris@47: // automatically. Chris@47: Chris@47: virtual void getsockopt(int level, int option, void* value, uint* length); Chris@47: virtual void setsockopt(int level, int option, const void* value, uint length); Chris@47: // Same as the methods of AsyncIoStream. Chris@47: }; Chris@47: Chris@47: // ======================================================================================= Chris@47: // Networks Chris@47: Chris@47: class NetworkAddress { Chris@47: // Represents a remote address to which the application can connect. Chris@47: Chris@47: public: Chris@47: virtual Promise> connect() = 0; Chris@47: // Make a new connection to this address. Chris@47: // Chris@47: // The address must not be a wildcard ("*"). If it is an IP address, it must have a port number. Chris@47: Chris@47: virtual Own listen() = 0; Chris@47: // Listen for incoming connections on this address. Chris@47: // Chris@47: // The address must be local. Chris@47: Chris@47: virtual Own bindDatagramPort(); Chris@47: // Open this address as a datagram (e.g. UDP) port. Chris@47: // Chris@47: // The address must be local. Chris@47: Chris@47: virtual Own clone() = 0; Chris@47: // Returns an equivalent copy of this NetworkAddress. Chris@47: Chris@47: virtual String toString() = 0; Chris@47: // Produce a human-readable string which hopefully can be passed to Network::parseAddress() Chris@47: // to reproduce this address, although whether or not that works of course depends on the Network Chris@47: // implementation. This should be called only to display the address to human users, who will Chris@47: // hopefully know what they are able to do with it. Chris@47: }; Chris@47: Chris@47: class Network { Chris@47: // Factory for NetworkAddress instances, representing the network services offered by the Chris@47: // operating system. Chris@47: // Chris@47: // This interface typically represents broad authority, and well-designed code should limit its Chris@47: // use to high-level startup code and user interaction. Low-level APIs should accept Chris@47: // NetworkAddress instances directly and work from there, if at all possible. Chris@47: Chris@47: public: Chris@47: virtual Promise> parseAddress(StringPtr addr, uint portHint = 0) = 0; Chris@47: // Construct a network address from a user-provided string. The format of the address Chris@47: // strings is not specified at the API level, and application code should make no assumptions Chris@47: // about them. These strings should always be provided by humans, and said humans will know Chris@47: // what format to use in their particular context. Chris@47: // Chris@47: // `portHint`, if provided, specifies the "standard" IP port number for the application-level Chris@47: // service in play. If the address turns out to be an IP address (v4 or v6), and it lacks a Chris@47: // port number, this port will be used. If `addr` lacks a port number *and* `portHint` is Chris@47: // omitted, then the returned address will only support listen() and bindDatagramPort() Chris@47: // (not connect()), and an unused port will be chosen each time one of those methods is called. Chris@47: Chris@47: virtual Own getSockaddr(const void* sockaddr, uint len) = 0; Chris@47: // Construct a network address from a legacy struct sockaddr. Chris@47: }; Chris@47: Chris@47: // ======================================================================================= Chris@47: // I/O Provider Chris@47: Chris@47: class AsyncIoProvider { Chris@47: // Class which constructs asynchronous wrappers around the operating system's I/O facilities. Chris@47: // Chris@47: // Generally, the implementation of this interface must integrate closely with a particular Chris@47: // `EventLoop` implementation. Typically, the EventLoop implementation itself will provide Chris@47: // an AsyncIoProvider. Chris@47: Chris@47: public: Chris@47: virtual OneWayPipe newOneWayPipe() = 0; Chris@47: // Creates an input/output stream pair representing the ends of a one-way pipe (e.g. created with Chris@47: // the pipe(2) system call). Chris@47: Chris@47: virtual TwoWayPipe newTwoWayPipe() = 0; Chris@47: // Creates two AsyncIoStreams representing the two ends of a two-way pipe (e.g. created with Chris@47: // socketpair(2) system call). Data written to one end can be read from the other. Chris@47: Chris@47: virtual Network& getNetwork() = 0; Chris@47: // Creates a new `Network` instance representing the networks exposed by the operating system. Chris@47: // Chris@47: // DO NOT CALL THIS except at the highest levels of your code, ideally in the main() function. If Chris@47: // you call this from low-level code, then you are preventing higher-level code from injecting an Chris@47: // alternative implementation. Instead, if your code needs to use network functionality, it Chris@47: // should ask for a `Network` as a constructor or method parameter, so that higher-level code can Chris@47: // chose what implementation to use. The system network is essentially a singleton. See: Chris@47: // http://www.object-oriented-security.org/lets-argue/singletons Chris@47: // Chris@47: // Code that uses the system network should not make any assumptions about what kinds of Chris@47: // addresses it will parse, as this could differ across platforms. String addresses should come Chris@47: // strictly from the user, who will know how to write them correctly for their system. Chris@47: // Chris@47: // With that said, KJ currently supports the following string address formats: Chris@47: // - IPv4: "1.2.3.4", "1.2.3.4:80" Chris@47: // - IPv6: "1234:5678::abcd", "[1234:5678::abcd]:80" Chris@47: // - Local IP wildcard (covers both v4 and v6): "*", "*:80" Chris@47: // - Symbolic names: "example.com", "example.com:80", "example.com:http", "1.2.3.4:http" Chris@47: // - Unix domain: "unix:/path/to/socket" Chris@47: Chris@47: struct PipeThread { Chris@47: // A combination of a thread and a two-way pipe that communicates with that thread. Chris@47: // Chris@47: // The fields are intentionally ordered so that the pipe will be destroyed (and therefore Chris@47: // disconnected) before the thread is destroyed (and therefore joined). Thus if the thread Chris@47: // arranges to exit when it detects disconnect, destruction should be clean. Chris@47: Chris@47: Own thread; Chris@47: Own pipe; Chris@47: }; Chris@47: Chris@47: virtual PipeThread newPipeThread( Chris@47: Function startFunc) = 0; Chris@47: // Create a new thread and set up a two-way pipe (socketpair) which can be used to communicate Chris@47: // with it. One end of the pipe is passed to the thread's start function and the other end of Chris@47: // the pipe is returned. The new thread also gets its own `AsyncIoProvider` instance and will Chris@47: // already have an active `EventLoop` when `startFunc` is called. Chris@47: // Chris@47: // TODO(someday): I'm not entirely comfortable with this interface. It seems to be doing too Chris@47: // much at once but I'm not sure how to cleanly break it down. Chris@47: Chris@47: virtual Timer& getTimer() = 0; Chris@47: // Returns a `Timer` based on real time. Time does not pass while event handlers are running -- Chris@47: // it only updates when the event loop polls for system events. This means that calling `now()` Chris@47: // on this timer does not require a system call. Chris@47: // Chris@47: // This timer is not affected by changes to the system date. It is unspecified whether the timer Chris@47: // continues to count while the system is suspended. Chris@47: }; Chris@47: Chris@47: class LowLevelAsyncIoProvider { Chris@47: // Similar to `AsyncIoProvider`, but represents a lower-level interface that may differ on Chris@47: // different operating systems. You should prefer to use `AsyncIoProvider` over this interface Chris@47: // whenever possible, as `AsyncIoProvider` is portable and friendlier to dependency-injection. Chris@47: // Chris@47: // On Unix, this interface can be used to import native file descriptors into the async framework. Chris@47: // Different implementations of this interface might work on top of different event handling Chris@47: // primitives, such as poll vs. epoll vs. kqueue vs. some higher-level event library. Chris@47: // Chris@47: // On Windows, this interface can be used to import native HANDLEs into the async framework. Chris@47: // Different implementations of this interface might work on top of different event handling Chris@47: // primitives, such as I/O completion ports vs. completion routines. Chris@47: // Chris@47: // TODO(port): Actually implement Windows support. Chris@47: Chris@47: public: Chris@47: // --------------------------------------------------------------------------- Chris@47: // Unix-specific stuff Chris@47: Chris@47: enum Flags { Chris@47: // Flags controlling how to wrap a file descriptor. Chris@47: Chris@47: TAKE_OWNERSHIP = 1 << 0, Chris@47: // The returned object should own the file descriptor, automatically closing it when destroyed. Chris@47: // The close-on-exec flag will be set on the descriptor if it is not already. Chris@47: // Chris@47: // If this flag is not used, then the file descriptor is not automatically closed and the Chris@47: // close-on-exec flag is not modified. Chris@47: Chris@47: ALREADY_CLOEXEC = 1 << 1, Chris@47: // Indicates that the close-on-exec flag is known already to be set, so need not be set again. Chris@47: // Only relevant when combined with TAKE_OWNERSHIP. Chris@47: // Chris@47: // On Linux, all system calls which yield new file descriptors have flags or variants which Chris@47: // set the close-on-exec flag immediately. Unfortunately, other OS's do not. Chris@47: Chris@47: ALREADY_NONBLOCK = 1 << 2 Chris@47: // Indicates that the file descriptor is known already to be in non-blocking mode, so the flag Chris@47: // need not be set again. Otherwise, all wrap*Fd() methods will enable non-blocking mode Chris@47: // automatically. Chris@47: // Chris@47: // On Linux, all system calls which yield new file descriptors have flags or variants which Chris@47: // enable non-blocking mode immediately. Unfortunately, other OS's do not. Chris@47: }; Chris@47: Chris@47: virtual Own wrapInputFd(int fd, uint flags = 0) = 0; Chris@47: // Create an AsyncInputStream wrapping a file descriptor. Chris@47: // Chris@47: // `flags` is a bitwise-OR of the values of the `Flags` enum. Chris@47: Chris@47: virtual Own wrapOutputFd(int fd, uint flags = 0) = 0; Chris@47: // Create an AsyncOutputStream wrapping a file descriptor. Chris@47: // Chris@47: // `flags` is a bitwise-OR of the values of the `Flags` enum. Chris@47: Chris@47: virtual Own wrapSocketFd(int fd, uint flags = 0) = 0; Chris@47: // Create an AsyncIoStream wrapping a socket file descriptor. Chris@47: // Chris@47: // `flags` is a bitwise-OR of the values of the `Flags` enum. Chris@47: Chris@47: virtual Promise> wrapConnectingSocketFd(int fd, uint flags = 0) = 0; Chris@47: // Create an AsyncIoStream wrapping a socket that is in the process of connecting. The returned Chris@47: // promise should not resolve until connection has completed -- traditionally indicated by the Chris@47: // descriptor becoming writable. Chris@47: // Chris@47: // `flags` is a bitwise-OR of the values of the `Flags` enum. Chris@47: Chris@47: virtual Own wrapListenSocketFd(int fd, uint flags = 0) = 0; Chris@47: // Create an AsyncIoStream wrapping a listen socket file descriptor. This socket should already Chris@47: // have had `bind()` and `listen()` called on it, so it's ready for `accept()`. Chris@47: // Chris@47: // `flags` is a bitwise-OR of the values of the `Flags` enum. Chris@47: Chris@47: virtual Own wrapDatagramSocketFd(int fd, uint flags = 0); Chris@47: Chris@47: virtual Timer& getTimer() = 0; Chris@47: // Returns a `Timer` based on real time. Time does not pass while event handlers are running -- Chris@47: // it only updates when the event loop polls for system events. This means that calling `now()` Chris@47: // on this timer does not require a system call. Chris@47: // Chris@47: // This timer is not affected by changes to the system date. It is unspecified whether the timer Chris@47: // continues to count while the system is suspended. Chris@47: }; Chris@47: Chris@47: Own newAsyncIoProvider(LowLevelAsyncIoProvider& lowLevel); Chris@47: // Make a new AsyncIoProvider wrapping a `LowLevelAsyncIoProvider`. Chris@47: Chris@47: struct AsyncIoContext { Chris@47: Own lowLevelProvider; Chris@47: Own provider; Chris@47: WaitScope& waitScope; Chris@47: Chris@47: UnixEventPort& unixEventPort; Chris@47: // TEMPORARY: Direct access to underlying UnixEventPort, mainly for waiting on signals. This Chris@47: // field will go away at some point when we have a chance to improve these interfaces. Chris@47: }; Chris@47: Chris@47: AsyncIoContext setupAsyncIo(); Chris@47: // Convenience method which sets up the current thread with everything it needs to do async I/O. Chris@47: // The returned objects contain an `EventLoop` which is wrapping an appropriate `EventPort` for Chris@47: // doing I/O on the host system, so everything is ready for the thread to start making async calls Chris@47: // and waiting on promises. Chris@47: // Chris@47: // You would typically call this in your main() loop or in the start function of a thread. Chris@47: // Example: Chris@47: // Chris@47: // int main() { Chris@47: // auto ioContext = kj::setupAsyncIo(); Chris@47: // Chris@47: // // Now we can call an async function. Chris@47: // Promise textPromise = getHttp(*ioContext.provider, "http://example.com"); Chris@47: // Chris@47: // // And we can wait for the promise to complete. Note that you can only use `wait()` Chris@47: // // from the top level, not from inside a promise callback. Chris@47: // String text = textPromise.wait(ioContext.waitScope); Chris@47: // print(text); Chris@47: // return 0; Chris@47: // } Chris@47: // Chris@47: // WARNING: An AsyncIoContext can only be used in the thread and process that created it. In Chris@47: // particular, note that after a fork(), an AsyncIoContext created in the parent process will Chris@47: // not work correctly in the child, even if the parent ceases to use its copy. In particular Chris@47: // note that this means that server processes which daemonize themselves at startup must wait Chris@47: // until after daemonization to create an AsyncIoContext. Chris@47: Chris@47: // ======================================================================================= Chris@47: // inline implementation details Chris@47: Chris@47: inline AncillaryMessage::AncillaryMessage( Chris@47: int level, int type, ArrayPtr data) Chris@47: : level(level), type(type), data(data) {} Chris@47: Chris@47: inline int AncillaryMessage::getLevel() const { return level; } Chris@47: inline int AncillaryMessage::getType() const { return type; } Chris@47: Chris@47: template Chris@47: inline Maybe AncillaryMessage::as() { Chris@47: if (data.size() >= sizeof(T)) { Chris@47: return *reinterpret_cast(data.begin()); Chris@47: } else { Chris@47: return nullptr; Chris@47: } Chris@47: } Chris@47: Chris@47: template Chris@47: inline ArrayPtr AncillaryMessage::asArray() { Chris@47: return arrayPtr(reinterpret_cast(data.begin()), data.size() / sizeof(T)); Chris@47: } Chris@47: Chris@47: } // namespace kj Chris@47: Chris@47: #endif // KJ_ASYNC_IO_H_