Exemplo n.º 1
0
func (self *MsgServer) createChannels() {
	glog.Info("createChannels")
	for _, c := range base.ChannleList {
		glog.Info(c)
		channel := link.NewChannel(self.server.Protocol())
		self.channels[c] = base.NewChannelState(c, channel)
	}
}
Exemplo n.º 2
0
// This is broadcast server demo work with the echo_client.
// usage:
//     go run echo_broadcast.go
func main() {
	var addr string

	flag.StringVar(&addr, "addr", ":10010", "echo server address")
	flag.Parse()

	server, err := link.Serve("tcp", addr, link.String(link.Uint16BE))
	if err != nil {
		panic(err)
	}
	println("server start:", server.Listener().Addr().String())

	channel := link.NewChannel()
	go func() {
		for range time.Tick(time.Second * 2) {
			now := time.Now().Format("2006-01-02 15:04:05")
			channel.Broadcast("from channel: " + now)
		}
	}()

	for {
		session, err := server.Accept()
		if err != nil {
			break
		}

		go func() {
			addr := session.Conn().RemoteAddr().String()
			println("client", addr, "connected")

			session.EnableAsyncSend(1024)
			channel.Join(session)

			for {
				var msg string
				if err := session.Receive(&msg); err != nil {
					break
				}
				println(addr, "say:", msg)
				channel.Broadcast("from " + addr + ": " + string(msg))
			}

			println("client", addr, "closed")
			channel.Exit(session)
		}()
	}
}
Exemplo n.º 3
0
// This is broadcast server demo work with the echo_client.
// usage:
//     go run echo_broadcast.go
func main() {
	flag.Parse()

	protocol := packet.New(
		binary.SplitByUint16BE, 1024, 1024, 1024,
	)

	server, err := link.Serve("tcp", *addr, protocol)
	if err != nil {
		panic(err)
	}
	println("server start:", server.Listener().Addr().String())

	channel := link.NewChannel(link.DefaultBroadcast)
	go func() {
		for range time.Tick(time.Second * 2) {
			now := time.Now().Format("2006-01-02 15:04:05")
			channel.Broadcast(packet.RAW("from channel: " + now))
		}
	}()

	server.Serve(func(session *link.Session) {
		addr := session.Conn().RemoteAddr().String()
		println("client", addr, "connected")
		channel.Join(session, nil)

		for {
			var msg packet.RAW
			if err := session.Receive(&msg); err != nil {
				break
			}
			println(addr, "say:", string(msg))
			channel.Broadcast(packet.RAW("from " + addr + ": " + string(msg)))
		}

		println("client", addr, "closed")
		channel.Exit(session)
	})
}
Exemplo n.º 4
0
func Test_Broadcast(t *testing.T) {
	var (
		clientNum     = 20
		channel       = link.NewChannel(Broadcast{})
		broadcastWait sync.WaitGroup
		clientWait    sync.WaitGroup
	)

	clientWait.Add(clientNum)
	backend := StartTestBackend(t, func(session *link.Session) {
		channel.Join(session)
		clientWait.Done()
		for {
			var msg packet.RAW
			if err := session.Receive(&msg); err != nil {
				break
			}
			broadcastWait.Done()
		}
	})

	go func() {
		clientWait.Wait()
		for i := 0; i < 10000; i++ {
			msg := RandBytes(10)
			channel.Broadcast(packet.RAW(msg))
			broadcastWait.Add(clientNum)
			broadcastWait.Wait()
		}
	}()

	gateway := StartTestGateway(t, backend.Listener().Addr().String())
	gatewayAddr := gateway.server.Listener().Addr().String()

	var wg sync.WaitGroup
	for i := 0; i < clientNum; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()

			client, err := link.Connect("tcp", gatewayAddr, protocol)
			unitest.NotError(t, err)

			for j := 0; j < 10000; j++ {
				var msg packet.RAW
				err := client.Receive(&msg)
				unitest.NotError(t, err)

				err = client.Send(msg)
				unitest.NotError(t, err)
			}

			client.Close()
		}()
	}
	wg.Wait()

	gateway.Stop()
	backend.Stop()

	time.Sleep(time.Second * 2)
	MakeSureSessionGoroutineExit(t)
}