Copilot commented on code in PR #13823:
URL: https://github.com/apache/trafficserver/pull/13823#discussion_r4339425419
##########
src/iocore/net/UnixUDPNet.cc:
##########
@@ -707,339 +706,6 @@ UDPNetProcessorInternal::udp_callback(UDPNetHandler *,
UDPConnection *xuc, EThre
#define UNINITIALIZED_EVENT_PTR (Event *)0xdeadbeef
-// cheesy implementation of a asynchronous read and callback for Unix
-class UDPReadContinuation : public Continuation
-{
-public:
- UDPReadContinuation(Event *completionToken);
- UDPReadContinuation();
- ~UDPReadContinuation() override;
- inline void free();
- inline void init_token(Event *completionToken);
- inline void init_read(int fd, IOBufferBlock *buf, int len, struct sockaddr
*fromaddr, socklen_t *fromaddrlen);
-
- void
- set_timer(int seconds)
- {
- timeout_interval = HRTIME_SECONDS(seconds);
- }
-
- void cancel();
- int readPollEvent(int event, Event *e);
-
- Action *
- getAction()
- {
- return event;
- }
-
- void setupPollDescriptor();
-
-private:
- Event *event = UNINITIALIZED_EVENT_PTR; // the completion event token created
- // on behalf of the client
- Ptr<IOBufferBlock> readbuf{nullptr};
- int readlen = 0;
- struct sockaddr_in6 *fromaddr = nullptr;
- socklen_t *fromaddrlen = nullptr;
- int fd = NO_FD; // fd we are reading from
- int ifd = NO_FD; // poll fd index
- ink_hrtime period = 0; // polling period
- ink_hrtime elapsed_time = 0;
- ink_hrtime timeout_interval = 0;
-};
-
-ClassAllocator<UDPReadContinuation, false>
udpReadContAllocator("udpReadContAllocator");
-
-UDPReadContinuation::UDPReadContinuation(Event *completionToken)
- : Continuation(nullptr),
- event(completionToken),
- readbuf(nullptr),
-
- fd(-1),
- ifd(-1)
-
-{
- if (completionToken->continuation) {
- this->mutex = completionToken->continuation->mutex;
- } else {
- this->mutex = new_ProxyMutex();
- }
-}
-
-UDPReadContinuation::UDPReadContinuation() : Continuation(nullptr) {}
-
-inline void
-UDPReadContinuation::free()
-{
- ink_assert(event != nullptr);
- completionUtil::destroy(event);
- event = nullptr;
- readbuf = nullptr;
- readlen = 0;
- fromaddrlen = nullptr;
- fd = -1;
- ifd = -1;
- period = 0;
- elapsed_time = 0;
- timeout_interval = 0;
- mutex = nullptr;
- udpReadContAllocator.free(this);
-}
-
-inline void
-UDPReadContinuation::init_token(Event *completionToken)
-{
- if (completionToken->continuation) {
- this->mutex = completionToken->continuation->mutex;
- } else {
- this->mutex = new_ProxyMutex();
- }
- event = completionToken;
-}
-
-inline void
-UDPReadContinuation::init_read(int rfd, IOBufferBlock *buf, int len, struct
sockaddr *fromaddr_, socklen_t *fromaddrlen_)
-{
- ink_assert(rfd >= 0 && buf != nullptr && fromaddr_ != nullptr &&
fromaddrlen_ != nullptr);
- fd = rfd;
- readbuf = buf;
- readlen = len;
- fromaddr = ats_ip6_cast(fromaddr_);
- fromaddrlen = fromaddrlen_;
- SET_HANDLER(&UDPReadContinuation::readPollEvent);
- period = -HRTIME_MSECONDS(net_event_period);
- setupPollDescriptor();
- this_ethread()->schedule_every(this, period);
-}
-
-UDPReadContinuation::~UDPReadContinuation()
-{
- if (event != UNINITIALIZED_EVENT_PTR) {
- ink_assert(event != nullptr);
- completionUtil::destroy(event);
- event = nullptr;
- }
-}
-
-void
-UDPReadContinuation::cancel()
-{
- // I don't think this actually cancels it correctly right now.
- event->cancel();
-}
-
-void
-UDPReadContinuation::setupPollDescriptor()
-{
-#if TS_USE_EPOLL
- Pollfd *pfd;
- EThread *et = static_cast<EThread *>(this_thread());
- PollCont *pc = get_PollCont(et);
- if (pc->nextPollDescriptor == nullptr) {
- pc->nextPollDescriptor = new PollDescriptor();
- }
- pfd = pc->nextPollDescriptor->alloc();
- pfd->fd = fd;
- ifd = pfd - pc->nextPollDescriptor->pfd;
- ink_assert(pc->nextPollDescriptor->nfds > ifd);
- pfd->events = POLLIN;
- pfd->revents = 0;
-#endif
-}
-
-int
-UDPReadContinuation::readPollEvent(int event_, Event *e)
-{
- (void)event_;
- (void)e;
-
- // PollCont *pc = get_PollCont(e->ethread);
- Continuation *c;
-
- if (event->cancelled) {
- e->cancel();
- free();
- return EVENT_DONE;
- }
-
- // See if the request has timed out
- if (timeout_interval) {
- elapsed_time += -period;
- if (elapsed_time >= timeout_interval) {
- c = completionUtil::getContinuation(event);
- // TODO: Should we deal with the return code?
- c->handleEvent(NET_EVENT_DATAGRAM_READ_ERROR, event);
- e->cancel();
- free();
- return EVENT_DONE;
- }
- }
-
- c = completionUtil::getContinuation(event);
- // do read
- socklen_t tmp_fromlen = *fromaddrlen;
- UnixSocket sock{fd};
- int rlen = sock.recvfrom(readbuf->end(), readlen, 0,
ats_ip_sa_cast(fromaddr), &tmp_fromlen);
-
- completionUtil::setThread(event, e->ethread);
- // call back user with their event
- if (rlen > 0) {
- // do callback if read is successful
- *fromaddrlen = tmp_fromlen;
- completionUtil::setInfo(event, fd, readbuf, rlen, errno);
- readbuf->fill(rlen);
- // TODO: Should we deal with the return code?
- c->handleEvent(NET_EVENT_DATAGRAM_READ_COMPLETE, event);
- e->cancel();
- free();
-
- return EVENT_DONE;
- } else if (rlen < 0 && rlen != -EAGAIN) {
- // signal error.
- *fromaddrlen = tmp_fromlen;
- completionUtil::setInfo(event, fd, readbuf, rlen, errno);
- c = completionUtil::getContinuation(event);
- // TODO: Should we deal with the return code?
- c->handleEvent(NET_EVENT_DATAGRAM_READ_ERROR, event);
- e->cancel();
- free();
-
- return EVENT_DONE;
- } else {
- completionUtil::setThread(event, nullptr);
- }
-
- if (event->cancelled) {
- e->cancel();
- free();
-
- return EVENT_DONE;
- }
- // reestablish poll
- setupPollDescriptor();
-
- return EVENT_CONT;
-}
-
-/* recvfrom:
- * Unix:
- * assert(buf->write_avail() >= len);
- * *actual_len = recvfrom(fd,addr,buf->end(),len)
- * if successful then
- * buf->fill(*actual_len);
- * return ACTION_RESULT_DONE
- * else if nothing read
- * *actual_len is 0
- * create "UDP read continuation" C with 'cont's lock
- * set user callback to 'cont'
- * return C's action.
- * else
- * return error;
- */
-Action *
-UDPNetProcessor::recvfrom_re(Continuation *cont, void *token, int fd, struct
sockaddr *fromaddr, socklen_t *fromaddrlen,
- IOBufferBlock *buf, int len, bool useReadCont,
int timeout)
-{
- (void)useReadCont;
- ink_assert(buf->write_avail() >= len);
- int actual;
- Event *event = completionUtil::create();
-
- completionUtil::setContinuation(event, cont);
- completionUtil::setHandle(event, token);
- UnixSocket sock{fd};
- actual = sock.recvfrom(buf->end(), len, 0, fromaddr, fromaddrlen);
-
- if (actual > 0) {
- completionUtil::setThread(event, this_ethread());
- completionUtil::setInfo(event, sock.get_fd(), make_ptr(buf), actual,
errno);
- buf->fill(actual);
- cont->handleEvent(NET_EVENT_DATAGRAM_READ_COMPLETE, event);
- completionUtil::destroy(event);
- return ACTION_RESULT_DONE;
- } else if (actual == 0 || actual == -EAGAIN) {
- UDPReadContinuation *c = udpReadContAllocator.alloc();
- c->init_token(event);
- c->init_read(sock.get_fd(), buf, len, fromaddr, fromaddrlen);
- if (timeout) {
- c->set_timer(timeout);
- }
- return event;
- } else {
- completionUtil::setThread(event, this_ethread());
- completionUtil::setInfo(event, sock.get_fd(), make_ptr(buf), actual,
errno);
- cont->handleEvent(NET_EVENT_DATAGRAM_READ_ERROR, event);
- completionUtil::destroy(event);
- return ACTION_IO_ERROR;
- }
-}
-
-/* sendmsg:
- * Unix:
- * *actual_len = sendmsg(fd,msg,default-flags);
- * if successful,
- * return ACTION_RESULT_DONE
- * else
- * return error
- */
-Action *
-UDPNetProcessor::sendmsg_re(Continuation *cont, void *token, int fd, struct
msghdr *msg)
-{
- int actual;
- Event *event = completionUtil::create();
-
- completionUtil::setContinuation(event, cont);
- completionUtil::setHandle(event, token);
-
- UnixSocket sock{fd};
- actual = sock.sendmsg(msg, 0);
- if (actual >= 0) {
- completionUtil::setThread(event, this_ethread());
- completionUtil::setInfo(event, sock.get_fd(), msg, actual, errno);
- cont->handleEvent(NET_EVENT_DATAGRAM_WRITE_COMPLETE, event);
- completionUtil::destroy(event);
- return ACTION_RESULT_DONE;
- } else {
- completionUtil::setThread(event, this_ethread());
- completionUtil::setInfo(event, sock.get_fd(), msg, actual, errno);
- cont->handleEvent(NET_EVENT_DATAGRAM_WRITE_ERROR, event);
- completionUtil::destroy(event);
- return ACTION_IO_ERROR;
- }
-}
-
-/* sendto:
- * If this were implemented, it might be implemented like this:
- * Unix:
- * call sendto(fd,addr,buf->reader()->start(),len);
- * if successful,
- * buf->consume(len);
- * return ACTION_RESULT_DONE
- * else
- * return error
- *
- */
-Action *
-UDPNetProcessor::sendto_re(Continuation *cont, void *token, int fd, struct
sockaddr const *toaddr, int toaddrlen,
- IOBufferBlock *buf, int len)
-{
- (void)token;
- ink_assert(buf->read_avail() >= len);
- UnixSocket sock{fd};
- int nbytes_sent = sock.sendto(buf->start(), len, 0, toaddr,
toaddrlen);
-
- if (nbytes_sent >= 0) {
- ink_assert(nbytes_sent == len);
- buf->consume(nbytes_sent);
- cont->handleEvent(NET_EVENT_DATAGRAM_WRITE_COMPLETE, reinterpret_cast<void
*>(-1));
- return ACTION_RESULT_DONE;
- } else {
- cont->handleEvent(NET_EVENT_DATAGRAM_WRITE_ERROR, reinterpret_cast<void
*>(nbytes_sent));
- return ACTION_IO_ERROR;
- }
-}
-
bool
UDPNetProcessor::CreateUDPSocket(int *resfd, sockaddr const *remote_addr,
Action **status, NetVCOptions const &opt)
{
Review Comment:
`UNINITIALIZED_EVENT_PTR` looks like it was only supporting the removed
`UDPReadContinuation` flow. If there are no remaining references elsewhere in
this translation unit, please remove this macro as part of the cleanup to avoid
leaving a magic-value sentinel behind.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]