Commit graph

19 commits

Author SHA1 Message Date
Sam Atkins
fe5fdb200b LibCore+LibIPC: Make Core::Stream read_without_waiting() return Bytes
For the reasoning, see the earlier commit about Core::Stream::read().
2022-04-16 13:27:51 -04:00
Sam Atkins
3b1e063d30 LibCore+Everywhere: Make Core::Stream::read() return Bytes
A mistake I've repeatedly made is along these lines:
```c++
auto nread = TRY(source_file->read(buffer));
TRY(destination_file->write(buffer));
```

It's a little clunky to have to create a Bytes or StringView from the
buffer's data pointer and the nread, and easy to forget and just use
the buffer. So, this patch changes the read() function to return a
Bytes of the data that were just read.

The other read_foo() methods will be modified in the same way in
subsequent commits.

Fixes #13687
2022-04-16 13:27:51 -04:00
kleines Filmröllchen
1b67e19bd6 LibCore: Extract Stream::OpenMode conversion helper into static function 2022-04-11 00:08:48 +02:00
Isak Holmstrom
41765895a8 LibCore: Make FreeBSD build SerenityOS 2022-02-17 09:26:56 +00:00
Sam Atkins
4d5080388a LibCore+Tests: Add SeekableStream::truncate() 2022-02-16 19:49:41 -05:00
sin-ack
17d3592cab LibCore: Add a timeout option to UDPSocket::connect
This allows us to set a timeout during connection and during receive and
send operations. I didn't add this to the other connect calls as it's
not used anywhere else for the time being.
2022-02-14 11:44:09 +01:00
Ali Mohammad Pur
07f444439c LibCore: Take StringViews by value in Stream::* function arguments 2022-02-06 13:10:10 +01:00
sin-ack
259ed04087 LibCore: Implement LocalSocket::release_fd
release_fd() releases the fd associated with the LocalSocket it is
called on. This is analogous to release_value() on container objects in
AK, after which the object does not contain the value.
2022-02-06 10:28:19 +01:00
sin-ack
3de51a4b99 LibCore: Make sockets close-on-exec by default
This mirrors the previous default in Core::LocalSocket, and is the safer
default anyway. This prevents fds from living on in other processes when
exec() is called in certain programs such as Assistant.

Fixes #12029.
2022-01-21 15:21:52 +01:00
sin-ack
ab36fb7a23 LibCore: Use Core::System and Error::from_syscall in Stream classes
This makes what caused a failure more obvious.
The use of Core::System additionally allows us to remove a lot of
boilerplate code from Stream classes.
2022-01-20 10:40:51 +01:00
sin-ack
4d2e3de94c LibCore: Don't manually close the fd when connection fails in sockets
This is wrong because we have already set the fd in the
PosixSocketHelper, and the destructor of the respective Socket class
will close the fd for us. With the manual closing of the fd, we attempt
to close the same fd twice which results in a crash.

Thanks to stelar7 for noticing this bug.
2022-01-20 10:40:51 +01:00
sin-ack
4cad0dd74c LibCore: Implement LocalSocket::peer_pid
Mostly a copy of Core::LocalSocket::peer_pid.
2022-01-15 13:29:48 +03:30
sin-ack
92be52fd9f LibCore: Implement LocalSocket::adopt_fd
Similar to File::adopt_fd, this function creates a new LocalSocket with
an existing fd. The main use of this function is to create LocalSocket
objects from fds that have been passed to us by SystemServer to take
over.
2022-01-15 13:29:48 +03:30
sin-ack
27b49a60d0 LibCore: Implement LocalSocket::read_without_waiting
This uses recv with MSG_DONTWAIT to disable blocking operation for a
single call. LibIPC uses this to read in a non-blocking manner from an
otherwise blocking socket.
2022-01-15 13:29:48 +03:30
sin-ack
f9847372f3 LibCore: Implement LocalSocket::receive_fd and send_fd
These are just wrappers over the sendfd and recvfd syscalls.
2022-01-15 13:29:48 +03:30
sin-ack
5f645e84d8 LibCore: Use Error::from_errno in Stream APIs
This makes Stream APIs work with Lagom and is overall cleaner.
2022-01-13 15:16:12 +03:30
sin-ack
dbd25916a3 LibCore+Userland+Tests: Convert Stream APIs to construct on heap
As per previous discussion, it was decided that the Stream classes
should be constructed on the heap.

While I don't personally agree with this change, it does have the
benefit of avoiding Function object reconstructions due to the lambda
passed to Notifier pointing to a stale object reference. This also has
the benefit of not having to "box" objects for virtual usage, as the
objects come pre-boxed.

However, it means that we now hit the heap everytime we construct a
TCPSocket for instance, which might not be desirable.
2022-01-13 15:16:12 +03:30
sin-ack
eb389db92c LibCore: Remove the SocketError class
SocketError is a relic from the KResult days when we couldn't have a
string in the KResult type, only an errno. Now that we can use string
literals with Error, it's no longer necessary. gai_strerror is thread
safe, so we can use it here unlike strerror.
2022-01-13 15:16:12 +03:30
sin-ack
19e13117ad LibCore: Implement the Serenity Stream API classes
The Serenity Stream API is the name for the new set of classes intended
to replace IODevice and its descendants. It provides more flexibility
for subclasses by allowing each subclass to override all the possible
functionalities according to their wishes.

Stream is the base class which implements majority of the functionality
expected from a readable and writable stream. SeekableStream adds
seeking on top, and provides a couple utility methods which derive from
seek. Socket adds a couple of BSD socket utility functions such as
checking whether there is data available to read and checking the
pending bytes on the socket.

As for the concrete classes, there is File which is a SeekableStream and
is intended to operate on file-like objects; and TCPSocket, UDPSocket
and LocalSocket, which handle TCP, UDP and UNIX sockets respectively.

The concrete classes do not do buffering by default. For buffering
functionality, a set of augmentative classes named BufferedSeekable and
BufferedSocket have been implemented, intended to wrap a SeekableStream
and a Socket, respectively.
2021-12-16 22:21:35 +03:30