12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- package main
- import (
- "flag"
- "fmt"
- "git.swzry.com/zry/SatorIPC/golang/sipc_conn"
- "io"
- "net"
- "os"
- "os/signal"
- "sync"
- "time"
- )
- func main() {
- var isServer bool
- var pipename string
- flag.BoolVar(&isServer, "s", false, "Server mode")
- flag.StringVar(&pipename, "p", sipc_conn.GeneratePath(), "Pipe name")
- flag.Parse()
- if isServer {
- fmt.Println("Listen Pipe Name:", pipename)
- c := make(chan os.Signal, 1)
- signal.Notify(c, os.Interrupt, os.Kill)
- server, err := sipc_conn.NewServer(pipename, false)
- if err != nil {
- fmt.Println("CreateServerError:", err.Error())
- return
- }
- server.SetConnErrorHandler(func(err error) {
- fmt.Println("[Conn] Failed Handling Connection:", err.Error())
- })
- server.SetConnHandler(handleConnection)
- err = server.Start()
- if err != nil {
- fmt.Println("StartServerError:", err.Error())
- return
- }
- s := <-c
- err = server.Stop()
- if err != nil {
- fmt.Println("StopServerError:", err.Error())
- }
- fmt.Println("Program Quit By Signal:", s)
- } else {
- fmt.Println("Dial Pipe Name:", pipename)
- conn, err := sipc_conn.Dial(pipename, 1*time.Second)
- if err != nil {
- fmt.Println("DialError:", err.Error())
- return
- }
- handleConnection(conn)
- fmt.Println("Program End By Connection Lost.")
- }
- }
- func handleConnection(netconn net.Conn) {
- wg := sync.WaitGroup{}
- wg.Add(2)
- go func() {
- outs := os.Stdout
- n, err := io.Copy(outs, netconn)
- if err != nil {
- fmt.Println("Recv IO Error:", err.Error())
- }
- fmt.Printf("%d byte(s) recieved.\n", n)
- wg.Done()
- }()
- go func() {
- ins := os.Stdin
- n, err := io.Copy(netconn, ins)
- if err != nil {
- fmt.Println("Transmit IO Error:", err.Error())
- }
- fmt.Printf("%d byte(s) transmitted.\n", n)
- wg.Done()
- }()
- wg.Wait()
- }
|