github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/libkb/testfixtures/kb_pipetest_server/main.go (about) 1 //go:build windows 2 // +build windows 3 4 package main 5 6 import ( 7 "bufio" 8 "flag" 9 "fmt" 10 "net" 11 12 mspipe "github.com/keybase/go-winio" 13 ) 14 15 // Use Dial to connect to a server and read messages from it. 16 func DoDial(name string) { 17 conn, err := mspipe.DialPipe(name, nil) 18 if err != nil { 19 fmt.Printf("error %s\n", err.Error()) 20 return 21 } 22 if _, err := fmt.Fprintln(conn, "Hi server!"); err != nil { 23 fmt.Printf("error %s\n", err.Error()) 24 } 25 r := bufio.NewReader(conn) 26 msg, err := r.ReadString('\n') 27 if err != nil { 28 fmt.Printf("error %s\n", err.Error()) 29 } 30 fmt.Println(msg) 31 } 32 33 // Use Listen to start a server, and accept connections with Accept(). 34 func DoListen(name string) { 35 ln, err := mspipe.ListenPipe(name, nil) 36 if err != nil { 37 fmt.Printf("error %s\n", err.Error()) 38 return 39 } 40 41 for { 42 conn, err := ln.Accept() 43 if err != nil { 44 // handle error 45 fmt.Printf("error %s\n", err.Error()) 46 continue 47 } 48 49 // handle connection like any other net.Conn 50 go func(conn net.Conn) { 51 r := bufio.NewReader(conn) 52 msg, err := r.ReadString('\n') 53 if err != nil { 54 fmt.Printf("error %s\n", err.Error()) 55 return 56 } 57 fmt.Println(msg) 58 }(conn) 59 } 60 } 61 62 func main() { 63 dial := flag.Bool("d", false, "dial") 64 flag.Parse() 65 name := "\\\\.\\pipe\\kbservice\\test_pipe" 66 if flag.NArg() > 0 { 67 name = flag.Args()[0] 68 } 69 70 if *dial { 71 fmt.Printf("dialing pipe name %s\n", name) 72 DoDial(name) 73 } else { 74 fmt.Printf("listening on pipe name %s\n", name) 75 DoListen(name) 76 } 77 }