Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚸 Add perception of multiple packages instead of one to the send method #60

Merged
merged 1 commit into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions cubes/net/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ async def receive(self) -> io.BytesIO:
length = await _LengthVarInt.from_stream(self._stream)
return io.BytesIO(await self._stream.receive(length))

async def send(self, packet: io.BytesIO) -> None:
new_packet = io.BytesIO()
_LengthVarInt(packet.getbuffer().nbytes).to_buffer(new_packet)
new_packet.write(packet.getvalue())
await self._stream.send(new_packet.getvalue())
async def send(self, *packets: io.BytesIO) -> None:
buffer = io.BytesIO()
for packet in packets:
_LengthVarInt(packet.getbuffer().nbytes).to_buffer(buffer)
buffer.write(packet.getvalue())
await self._stream.send(buffer.getvalue())

async def close(self) -> None:
await self._stream.aclose()
8 changes: 4 additions & 4 deletions examples/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ async def login(self, player_name: str) -> uuid.UUID:
types_.String(self.connection.remote_address[0])
types_.UnsignedShort(self.connection.remote_address[1])
types_.VarInt(net.ConnectionStatus.LOGIN)
await self.connection.send(handshake)

self.connection.status = net.ConnectionStatus.LOGIN

login_start = io.BytesIO()
types_.VarInt(0x00).to_buffer(login_start)
types_.String(player_name).to_buffer(login_start)
await self.connection.send(login_start)

await self.connection.send(handshake, login_start)

self.connection.status = net.ConnectionStatus.LOGIN

response = await self.connection.receive()
packet_id = types_.VarInt.from_buffer(response)
Expand Down