help_linux_test.go 535 B

123456789101112131415161718192021222324252627282930
  1. package door
  2. import (
  3. "net"
  4. "os"
  5. )
  6. var fdmap map[uintptr] *os.File
  7. func init() {
  8. fdmap = make(map[uintptr] *os.File)
  9. }
  10. func socket_to_fd(socket net.Conn) int {
  11. client_conn := socket.(*net.TCPConn)
  12. // This creates a duplicate, but once closed -- the fd gets reused!
  13. client_file, _ := client_conn.File()
  14. fdmap[client_file.Fd()] = client_file
  15. return int(client_file.Fd())
  16. }
  17. func close_fd(fd int) {
  18. var fd_uintptr uintptr = uintptr(fd)
  19. file, ok := fdmap[fd_uintptr]
  20. if ok {
  21. file.Close()
  22. delete(fdmap, fd_uintptr)
  23. }
  24. }