github.com/GuanceCloud/cliutils@v1.1.21/logger/tcpudp.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 package logger 7 8 import ( 9 "fmt" 10 "net" 11 "strings" 12 ) 13 14 type remoteEndpoint struct { 15 protocol string 16 host string 17 18 tcpConn *net.TCPConn 19 udpConn *net.UDPConn 20 } 21 22 func newRemoteSync(proto, iphost string) (*remoteEndpoint, error) { 23 switch strings.ToLower(proto) { 24 case SchemeTCP: 25 addr, err := net.ResolveTCPAddr("tcp", iphost) 26 if err != nil { 27 return nil, err 28 } 29 30 conn, err := net.DialTCP("tcp", nil, addr) 31 if err != nil { 32 return nil, err 33 } 34 35 return &remoteEndpoint{ 36 protocol: proto, 37 host: iphost, 38 tcpConn: conn, 39 }, nil 40 41 case SchemeUDP: 42 addr, err := net.ResolveUDPAddr("udp", iphost) 43 if err != nil { 44 return nil, err 45 } 46 conn, err := net.DialUDP("udp", nil, addr) 47 if err != nil { 48 return nil, err 49 } 50 51 return &remoteEndpoint{ 52 protocol: proto, 53 host: iphost, 54 udpConn: conn, 55 }, nil 56 default: 57 return nil, fmt.Errorf("unknown remote protocol: %s", proto) 58 } 59 } 60 61 func (te *remoteEndpoint) Write(data []byte) (int, error) { 62 switch strings.ToLower(te.protocol) { 63 case SchemeTCP: 64 return te.tcpConn.Write(data) 65 66 case SchemeUDP: 67 return te.udpConn.Write(data) 68 69 default: 70 return -1, fmt.Errorf("unknown remote protocol: %s", te.protocol) 71 } 72 } 73 74 func (te *remoteEndpoint) Sync() error { return nil } // do nothing