support tls ?
zljohn-ux opened this issue · comments
if use tls.Listen(), netpoll.must Will fail,The reason for the failure is "could not get file descriptor", so support tls?
I was able to get TLS to work using netpoll.NewDesc(fd, netpoll.EventRead)
to get the fd from the tls.Conn, I had to use reflection
func FDFromTLSConn(conn net.Conn) int {
tls := reflect.TypeOf(conn) == reflect.TypeOf(&tls.Conn{})
tcpConn := reflect.Indirect(reflect.ValueOf(conn)).FieldByName("conn")
if tls {
tcpConn = reflect.Indirect(tcpConn.Elem())
}
fdVal := tcpConn.FieldByName("fd")
pfdVal := reflect.Indirect(fdVal).FieldByName("pfd")
return int(pfdVal.FieldByName("Sysfd").Int())
}
you also have to set the fd to non-blocking
func setNonblock(fd int, nonblocking bool) (err error) {
return syscall.SetNonblock(fd, nonblocking)
}
Even that doesn't work properly. I kept getting errors during writing to the websocket:
write tcp [::1]:8065->[::1]:22191: write: bad file descriptor
write tcp [::1]:8065->[::1]:22391: write: connection reset by peer
This needs to be supported properly from the standard library.
I'm on a mac and that worked for me, so it could be the difference between kqueue and epoll. I replaced this code with opening a tcp connection and then promoting it to a tls.Conn
ln, err := net.Listen("tcp", *addr)
if err != nil {
panic(err)
}
defer ln.Close()
tcpConn, err := ln.(*net.TCPListener).AcceptTCP()
if err != nil {
panic(err)
}
tlsConn := tls.Server(tcpConn, tlsConfig)
if err := tlsConn.Handshake(); err != nil {
panic(err)
}
desc := netpoll.Must(netpoll.HandleRead(tcpConn))