Created
February 1, 2018 18:08
-
-
Save kevin-cantwell/3e0fdd9d22ee31258878fb6c82d3aee4 to your computer and use it in GitHub Desktop.
PTTH Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ptth | |
import ( | |
"errors" | |
"net" | |
"net/http" | |
"golang.org/x/net/http2" | |
) | |
// DialAndServe establishes a tcp connection to addr and | |
// serves incoming HTTP/2 requests. Only a single connection | |
// is used to multiplex requests. An error will be returned | |
// if any of the following occur: 1) A tcp connection cannot | |
// be established to the remote host; 2) The connection is | |
// closed by the remote host; or 3) A network error occurs. | |
func DialAndServe(addr string, handler http.Handler) error { | |
raddr, err := net.ResolveTCPAddr("tcp", addr) | |
if err != nil { | |
return err | |
} | |
conn, err := net.DialTCP("tcp", nil, raddr) | |
if err != nil { | |
return err | |
} | |
s := http2.Server{} | |
s.ServeConn(conn, &http2.ServeConnOpts{ | |
Handler: handler, | |
}) | |
// if we've reached this point, then the underlying conn | |
// has been closed | |
return errors.New("ptth: remote address unavailable") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment