func (c Clients) clientMain(connectTo string, clientnum int, endch chan bool) { defer func() { <-endch }() clientname := fmt.Sprintf("test%v", clientnum) go func() { gconn := gogueclient.NewClientGogueConn(connectTo) if gconn == nil { return } defer gconn.Close() for i := 0; ; i++ { tosend := DataPacket{ clientname, i, } err := gconn.Send(tosend) if err != nil { if err.Error() != "EOF" { log.Error("send %v", err) } break } var torecv DataPacket err = gconn.Recv(&torecv) if err != nil { if err.Error() != "EOF" { log.Error("recv %v", err) } break } } }() time.Sleep(time.Duration(c.RunDur) * time.Second) }
func NewClientGogueConn(connectTo string) *gogueconn.GogueConn { conn, err := net.Dial("tcp", connectTo) if err != nil { log.Error("client %v", err) return nil } return gogueconn.New(conn) }
func servergo(gconn *gogueconn.GogueConn, clientQueue <-chan bool) { defer gconn.Close() // log.Info("client connected") for { var rdata DataPacket err := gconn.Recv(&rdata) if err != nil { if err.Error() != "EOF" { log.Error("recv %v", err) } break } err = gconn.Send(&rdata) if err != nil { if err.Error() != "EOF" { log.Error("send %v", err) } break } stat.Inc() } }
func TCPServer(listenString string, connNum int, connThrottle int, runConn ServerGoFn) { log.Info("Start server %v", listenString) // concurrent connection count control clientQueue := make(chan bool, connNum) listener, err := net.Listen("tcp", listenString) if err != nil { log.Error("%v", err) return } defer listener.Close() for { time.Sleep(time.Duration(connThrottle) * time.Millisecond) clientQueue <- true conn, err := listener.Accept() if err != nil { log.Error("%v", err) } else { go runConn(gogueconn.New(conn), clientQueue) } } }
func (c *GogueConn) Send(v interface{}) (err error) { defer func() { if e := recover(); e != nil { log.Error("Connection error %v", e) err = errors.New(fmt.Sprintf("%v", e)) } }() switch c.packettype { default: return errors.New("Unknown packet type") case PT_gob: err = c.enc.Encode(v) case PT_json: err = c.enc.Encode(v) } return }