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

[Question]: eng.Dup() not supported by windows #666

Open
3 tasks done
fearlessfe opened this issue Dec 23, 2024 · 1 comment
Open
3 tasks done

[Question]: eng.Dup() not supported by windows #666

fearlessfe opened this issue Dec 23, 2024 · 1 comment
Labels
help wanted Extra attention is needed inferior issue question Further information is requested waiting for response waiting for the response from commenter

Comments

@fearlessfe
Copy link

Actions I've taken before I'm here

  • I've thoroughly read the documentations about this problem but still have no answer.
  • I've searched the Github Issues/Discussions but didn't find any similar problems that have been solved.
  • I've searched the internet for this problem but didn't find anything helpful.

Questions with details

I want to use gnet to send and reveice udp data on the same ip and port, the gnet version is github.com/panjf2000/gnet/v2 v2.6.3

I found some information on issue #345 , but it cannot work on windows.

Does it has any other method to work on windows?

Code snippets (optional)

type gnetConn struct {
	gnet.BuiltinEventEngine
	conn       *net.UDPConn
	log        log.Logger
	localAddr  net.Addr
	eng        gnet.Engine
	packetChan chan packet
	startChan  chan struct{}
}

func NewGnetConn(log log.Logger) *gnetConn {
	return &gnetConn{
		log:        log,
		packetChan: make(chan packet, 1024),
		startChan:  make(chan struct{}),
	}
}

func (gc *gnetConn) ListenUDP(ctx context.Context, addr string) error {
	udpAddr, err := net.ResolveUDPAddr("udp", addr)
	if err != nil {
		return err
	}
	go func() {
		err := gnet.Run(gc, "udp://"+addr)
		gc.log.Error("gnet run failed", "err", err)
	}()
	<-gc.startChan
	return nil
}

func (gc *gnetConn) OnTraffic(c gnet.Conn) gnet.Action {
	packet := packet{}
	data, err := c.Next(-1)

	if err != nil {
		return gnet.None
	}
	gc.log.Debug("<< reveice data", "remote", c.RemoteAddr())
	dst := make([]byte, len(data))
	copy(dst, data)
	packet.n = len(dst)
	packet.data = dst
	remote := c.RemoteAddr().(*net.UDPAddr)
	packet.addr = netip.AddrPortFrom(
		netip.AddrFrom4([4]byte(remote.IP.To4())),
		uint16(remote.Port),
	)
	gc.packetChan <- packet

	return gnet.None
}

func (gc *gnetConn) OnBoot(eng gnet.Engine) gnet.Action {
	fd, err := eng.Dup()
	if err != nil {
		gc.log.Error("on boot dup failed", "err", err)
		return gnet.Shutdown
	}
	file := os.NewFile(uintptr(fd), "udp")

	conn, err := net.FileConn(file)
	if err != nil {
		gc.log.Error("on boot file conn failed", "err", err)
		return gnet.Shutdown
	}

	udpConn := conn.(*net.UDPConn)
	gc.conn = udpConn
	gc.eng = eng
	gc.startChan <- struct{}{}
	return gnet.None
}
@fearlessfe fearlessfe added help wanted Extra attention is needed question Further information is requested labels Dec 23, 2024
@panjf2000
Copy link
Owner

You can't just simply throw some code and say it doesn't work. That's no help for solving the problem. You need to provide more contexts and details explaining how it's not working.

@panjf2000 panjf2000 added inferior issue waiting for response waiting for the response from commenter labels Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed inferior issue question Further information is requested waiting for response waiting for the response from commenter
Projects
None yet
Development

No branches or pull requests

2 participants