Skip to content

feat(udp): add UdpConnection::sendTo() and Worker::getUdpSender() - #1171

Open
zgh419566 wants to merge 4 commits into
walkor:masterfrom
zgh419566:master
Open

zgh419566 wants to merge 4 commits into
walkor:masterfrom
zgh419566:master

Conversation

@zgh419566

Copy link
Copy Markdown

What

Add UdpConnection::sendTo(string $ip, int $port, mixed $data, bool $raw = false) for sending to an arbitrary destination.

Add Worker::getUdpSender(): ?UdpConnection to obtain a reusable sender bound to the listening socket.

Default $remoteAddress to '' in UdpConnection::__construct() to allow creating a sender-only connection.

Add an udp server example and update README.

Why
UdpConnection::send() only replies to the address of the incoming packet. Protocols like SIP/GB28181 require sending requests from the listening port (so the source port matches), validated by the peer. Currently, users must manually create a second socket. sendTo() and getUdpSender() fill this gap elegantly, keeping the logic inside the official UdpConnection without touching send()'s signature.

Why not modify send()
Adding a third parameter to send() would break signature compatibility for AsyncUdpConnection subclasses. A dedicated sendTo() is also clearer: send() replies, sendTo() targets. Placing it on UdpConnection makes the UDP-only nature obvious, as TcpConnection has no such concept.

Backward compatibility
Zero breakage. send() is untouched. The constructor change only adds a default value.

Added a method to get a UdpConnection for proactive sending.
Worker::getUdpSender() returns a reusable UdpConnection bound to the worker's listening socket, for use in onWorkerStart / timers / channel callbacks.

UdpConnection::sendTo($ip, $port, $data, $raw = false) sends to an arbitrary destination from that socket.

UdpConnection::__construct() now defaults $remoteAddress to '' so a sender-only connection can be created without one.
Added a UDP server example to the README.
@zgh419566

Copy link
Copy Markdown
Author

This PR adds the ability for a UDP worker to proactively send packets on its listening port, without waiting for an incoming packet. Two new methods are added:

UdpConnection::sendTo(string $ip, int $port, mixed $data, bool $raw = false): bool
Sends a datagram to an arbitrary destination using the connection's socket as the source.

Worker::getUdpSender(): ?UdpConnection
Returns a reusable sender bound to the worker's listening socket, for use in onWorkerStart, timers, and channel callbacks.

Additionally, UdpConnection::__construct() now defaults $remoteAddress to '', so a sender-only connection can be created without a remote address. A new example in README snippet are included.

本 PR 让 UDP Worker 可以用自己的监听端口主动发数据,而不必等收到包再回复。新增两个方法:

UdpConnection::sendTo(string $ip, int $port, mixed $data, bool $raw = false): bool
用当前连接的 socket 作为源,发往任意目标地址。

Worker::getUdpSender(): ?UdpConnection
返回一个绑定到监听 socket 的可复用发送器,方便在 onWorkerStart、Timer、channel 回调里使用。

此外,UdpConnection::__construct() 的 $remoteAddress 参数加了默认值 '',这样"只用来发送"的连接不需要远端地址就能构造。同时补充了示例的 README 片段。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant