Skip to content

Commit

Permalink
Added Socket.write(ByteArray)
Browse files Browse the repository at this point in the history
  • Loading branch information
aslze committed Mar 27, 2023
1 parent 4deae85 commit 608bece
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion doc/doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ With CMake 3.14+, instead of using `find_package()`, you can download and build
~~~
include(FetchContent)
FetchContent_Declare(asl URL https://github.com/aslze/asl/archive/1.11.6.zip)
FetchContent_Declare(asl URL https://github.com/aslze/asl/archive/1.11.7.zip)
FetchContent_MakeAvailable(asl)
~~~
Expand Down
16 changes: 10 additions & 6 deletions include/asl/Socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,14 @@ class ASL_API Socket : public SmartObject
*/
int read(void* data, int size) { return _()->read(data, size); }
/**
Writes `size` bytes from the bufffer pointed to by `data` to the socket.
Writes `n` bytes from the bufffer pointed to by `data` to the socket.
*/
int write(const void* data, int n) { return _()->write(data, n); }
/**
Writes the byte array to the socket and returns the number of bytes actually sent.
*/
int write(const ByteArray& data) { return _()->write(data.ptr(), data.length()); }
/**
Reads n bytes and returns them as an array of bytes, or reads all available bytes if no argument is given.
*/
Array<byte> read(int n = -1) { return _()->read(n); }
Expand Down Expand Up @@ -361,15 +365,15 @@ identifies the sender so you can reply.
PacketSocket socket;
socket.bind(port);
InetAddress sender;
socket.readFrom(sender, data, sizeof(data));
socket.sendTo(sender, "hi", 3);
auto data = socket.readFrom(sender);
socket.sendTo(sender, String("hi"));
```
The sender just sends packets to an endpoint:
```
PacketSocket socket;
socket.sendTo(InetAddress("localhost", port), data, sizeof(data));
socket.sendTo(InetAddress("localhost", port), data);
```
*/
class ASL_API PacketSocket : public Socket
Expand Down Expand Up @@ -462,7 +466,7 @@ A listening side would join the group, receive packets and then leave:
MulticastSocket socket;
socket.join(group);
InetAddress sender;
socket.readFrom(sender, data, sizeof(data));
socket.readFrom(sender, data);
socket.leave(group);
```
Expand All @@ -471,7 +475,7 @@ And a sending side would start a multicast session to the same group and send pa
```
MulticastSocket socket;
socket.multicast(group);
socket.write(data, sizeof(data));
socket.write(data);
```
*/
class ASL_API MulticastSocket : public PacketSocket
Expand Down

0 comments on commit 608bece

Please sign in to comment.