diff --git a/cmd/primetime/main.go b/cmd/primetime/main.go new file mode 100644 index 0000000..f2ecded --- /dev/null +++ b/cmd/primetime/main.go @@ -0,0 +1,23 @@ +package main + +import ( + "log" + "net" + + "protohackers/pkg/conn" +) + +type data struct { + Method string `json:"method"` + Number int `json:"number"` +} + +func main() { + err := conn.StartSimple(primetime) + if err != nil { + log.Fatalln(err) + } +} + +func primetime(c net.Conn) { +} diff --git a/cmd/smoketest/main.go b/cmd/smoketest/main.go index 10f924b..f90bcf8 100644 --- a/cmd/smoketest/main.go +++ b/cmd/smoketest/main.go @@ -1,40 +1,22 @@ package main import ( - "fmt" "io" "log" "net" - "os" -) -const ( - connPort = "3030" - connType = "tcp4" + "protohackers/pkg/conn" ) func main() { - l, err := net.Listen(connType, ":"+connPort) + err := conn.StartSimple(smoketest) if err != nil { log.Fatalln(err) - os.Exit(1) - } - defer l.Close() - - fmt.Println(fmt.Sprintf("Listening on port %s", connPort)) - - for { - conn, err := l.Accept() - if err != nil { - log.Fatalln(err) - } - go handleRequest(conn) } } -func handleRequest(conn net.Conn) { - defer conn.Close() - if _, err := io.Copy(conn, conn); err != nil { +func smoketest(c net.Conn) { + if _, err := io.Copy(c, c); err != nil { log.Fatalln(err) } } diff --git a/pkg/conn/conn.go b/pkg/conn/conn.go new file mode 100644 index 0000000..3d928ba --- /dev/null +++ b/pkg/conn/conn.go @@ -0,0 +1,33 @@ +package conn + +import ( + "fmt" + "net" +) + +const ( + Port = ":3030" + Type = "tcp4" +) + +func StartSimple(handler func(conn net.Conn)) error { + l, err := net.Listen(Type, Port) + if err != nil { + return err + } + defer l.Close() + + fmt.Println(fmt.Sprintf("Listening on port %s", Port)) + + for { + c, err := l.Accept() + if err != nil { + return err + } + go handler(c) + err = c.Close() + if err != nil { + return err + } + } +}