help_linux_test.go 679 B

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