Esempio n. 1
0
func client() {

	pipe := cellnet.NewEventPipe()

	p := socket.NewConnector(pipe).Start("127.0.0.1:7201")

	rpc.InstallClient(p)

	socket.RegisterSessionMessage(p, coredef.SessionConnected{}, func(content interface{}, ses cellnet.Session) {

		rpc.Call(p, &coredef.TestEchoACK{
			Content: "rpc hello",
		}, func(msg *coredef.TestEchoACK) {

			log.Debugln("client recv", msg.Content)

			signal.Done(1)
		})

	})

	pipe.Start()

	signal.WaitAndExpect(1, "not recv data")
}
Esempio n. 2
0
func server() {

	pipe := cellnet.NewEventPipe()

	qpsm := benchmark.NewQPSMeter(pipe, func(qps int) {

		log.Infof("QPS: %d", qps)

	})

	evq := socket.NewAcceptor(pipe).Start(benchmarkAddress)

	socket.RegisterSessionMessage(evq, "gamedef.TestEchoACK", func(content interface{}, ses cellnet.Session) {

		if qpsm.Acc() > benchmarkSeconds {
			signal.Done(1)
			log.Infof("Average QPS: %d", qpsm.Average())
		}

		ses.Send(&gamedef.TestEchoACK{})

	})

	pipe.Start()

}
Esempio n. 3
0
// 客户端
func client() {

	pipe := cellnet.NewEventPipe()

	evq := socket.NewConnector(pipe).Start("127.0.0.1:7101")

	socket.RegisterSessionMessage(evq, "coredef.SessionConnected", func(content interface{}, ses cellnet.Session) {

		signal.Done(1)

		ack := &coredef.TestEchoACK{
			Content: "hello",
		}
		ses.Send(ack)

		log.Debugf("client send: %s\n", ack.String())

	})

	socket.RegisterSessionMessage(evq, "coredef.TestEchoACK", func(content interface{}, ses cellnet.Session) {
		msg := content.(*coredef.TestEchoACK)

		log.Debugln("client recv:", msg.String())

		signal.Done(3)
	})

	pipe.Start()

	signal.WaitAndExpect(1, "not connceted to router")
	signal.WaitAndExpect(2, "not recv client msg")
	signal.WaitAndExpect(3, "not recv server msg")

}
Esempio n. 4
0
func runServer() {
	pipe := cellnet.NewEventPipe()

	p := socket.NewAcceptor(pipe).Start("127.0.0.1:7235")

	// 计数器, 应该按照connCount倍数递增
	var counter int

	socket.RegisterSessionMessage(p, coredef.TestEchoACK{}, func(ses cellnet.Session, content interface{}) {
		msg := content.(*coredef.TestEchoACK)

		counter++
		log.Printf("No. %d: server recv: %v", counter, msg.String())

		// 发包后关闭
		ses.Send(&coredef.TestEchoACK{
			Content: proto.String(msg.GetContent()),
		})

		if msg.GetContent() != "noclose" {
			ses.Close()
		}

	})

	pipe.Start()

	done <- 0

}
Esempio n. 5
0
func TestTimer(t *testing.T) {

	signal := test.NewSignalTester(t)

	pipe := cellnet.NewEventPipe()

	evq := pipe.AddQueue()

	pipe.Start()

	const testTimes = 3

	var count int = testTimes

	cellnet.NewTimer(evq, time.Second, func(t *cellnet.Timer) {
		log.Debugln("timer 1 sec tick")

		signal.Done(1)

		count--

		if count == 0 {
			t.Stop()
			signal.Done(2)
		}
	})

	for i := 0; i < testTimes; i++ {
		signal.WaitAndExpect(1, "timer not tick")
	}

	signal.WaitAndExpect(2, "timer not stop")

}
Esempio n. 6
0
func client() {

	pipe := cellnet.NewEventPipe()

	evq := socket.NewConnector(pipe).Start("127.0.0.1:7201")

	socket.RegisterSessionMessage(evq, "gamedef.TestEchoACK", func(content interface{}, ses cellnet.Session) {
		msg := content.(*gamedef.TestEchoACK)

		log.Debugln("client recv:", msg.String())

		signal.Done(1)
	})

	socket.RegisterSessionMessage(evq, "gamedef.SessionConnected", func(content interface{}, ses cellnet.Session) {

		ses.Send(&gamedef.TestEchoACK{
			Content: "hello",
		})

	})

	pipe.Start()

	signal.WaitAndExpect(1, "not recv data")

}
Esempio n. 7
0
func client() {

	pipe := cellnet.NewEventPipe()

	p := socket.NewConnector(pipe)
	p.SetName("client")
	p.Start("127.0.0.1:9201")

	socket.RegisterSessionMessage(p, "gamedef.SessionConnected", func(content interface{}, ses cellnet.Session) {

		rpc.Call(p, &gamedef.TestEchoACK{
			Content: "rpc async call",
		}, func(msg *gamedef.TestEchoACK) {

			log.Debugln("client recv", msg.Content)

			signal.Done(1)
		})

	})

	pipe.Start()

	signal.WaitAndExpect(1, "not recv data")
}
Esempio n. 8
0
// 后台服务器
func backendServer() {

	gate.DebugMode = true

	pipe := cellnet.NewEventPipe()

	gate.StartGateConnector(pipe, []string{"127.0.0.1:7201"})

	gate.RegisterSessionMessage(coredef.SessionClosed{}, func(content interface{}, gateSes cellnet.Session, clientid int64) {
		log.Printf("client closed gate: %d clientid: %d\n", gateSes.ID(), clientid)
	})

	gate.RegisterSessionMessage(coredef.TestEchoACK{}, func(content interface{}, gateSes cellnet.Session, clientid int64) {
		msg := content.(*coredef.TestEchoACK)

		log.Printf("recv relay,  gate: %d clientid: %d\n", gateSes.ID(), clientid)

		gate.SendToClient(gateSes, clientid, &coredef.TestEchoACK{
			Content: proto.String(msg.GetContent()),
		})
	})

	pipe.Start()

	<-done
}
Esempio n. 9
0
// 客户端
func client() {

	pipe := cellnet.NewEventPipe()

	evq := socket.NewConnector(pipe).Start("127.0.0.1:7101")

	socket.RegisterSessionMessage(evq, coredef.SessionConnected{}, func(content interface{}, ses cellnet.Session) {

		ack := &coredef.TestEchoACK{
			Content: proto.String("hello"),
		}
		ses.Send(ack)

		log.Printf("client send: %s\n", ack.String())

	})

	socket.RegisterSessionMessage(evq, coredef.TestEchoACK{}, func(content interface{}, ses cellnet.Session) {
		msg := content.(*coredef.TestEchoACK)

		log.Println("client recv:", msg.String())

		done <- true
	})

	pipe.Start()

	<-done
}
Esempio n. 10
0
// 网关服务器
func routerServer() {

	router.DebugMode = true

	pipe := cellnet.NewEventPipe()

	router.StartBackendAcceptor(pipe, "127.0.0.1:7201", "svc->backend")
	router.StartFrontendAcceptor(pipe, "127.0.0.1:7101", "client->router")

	pipe.Start()

}
Esempio n. 11
0
// 网关服务器
func gateServer() {

	gate.DebugMode = true

	pipe := cellnet.NewEventPipe()

	gate.StartBackendAcceptor(pipe, "127.0.0.1:7201")
	gate.StartClientAcceptor(pipe, "127.0.0.1:7101")

	pipe.Start()

}
Esempio n. 12
0
// 客户端连接上后, 主动断开连接, 确保连接正常关闭
func connClose() {

	pipe := cellnet.NewEventPipe()

	p := socket.NewConnector(pipe).Start("127.0.0.1:7235")

	socket.RegisterSessionMessage(p, coredef.SessionConnected{}, func(ses cellnet.Session, content interface{}) {

		// 连接上发包,告诉服务器不要断开
		ses.Send(&coredef.TestEchoACK{
			Content: proto.String("noclose"),
		})

	})

	socket.RegisterSessionMessage(p, coredef.TestEchoACK{}, func(ses cellnet.Session, content interface{}) {
		msg := content.(*coredef.TestEchoACK)

		log.Println("client recv:", msg.String())
		done <- 1

		// 客户端主动断开
		ses.Close()

	})

	socket.RegisterSessionMessage(p, coredef.SessionClosed{}, func(ses cellnet.Session, content interface{}) {

		log.Println("close ok!")
		// 正常断开
		done <- 2

	})

	pipe.Start()

	// 收到回包
	if <-done != 1 {
		log.Panicln("test failed, not recv msg")
	}

	// 断开正常
	if <-done != 2 {
		log.Panicln("test failed, not close")
	}

	fmt.Println("connected close test done!")

}
Esempio n. 13
0
// 多连接收封包后被服务器关闭, 确保收到封包
func multiConn() {

	pipe := cellnet.NewEventPipe()

	// 同步量
	var endAcc sync.WaitGroup

	// 启动N个连接
	for i := 0; i < connCount; i++ {

		endAcc.Add(1)

		p := socket.NewConnector(pipe).Start("127.0.0.1:7235")

		p.SetName(fmt.Sprintf("%d", i))

		socket.RegisterSessionMessage(p, coredef.TestEchoACK{}, func(ses cellnet.Session, content interface{}) {
			msg := content.(*coredef.TestEchoACK)

			log.Println("client recv:", msg.String())

			// 正常收到
			endAcc.Done()
		})

		socket.RegisterSessionMessage(p, coredef.SessionConnected{}, func(ses cellnet.Session, content interface{}) {

			id, _ := strconv.Atoi(ses.FromPeer().Name())

			// 连接上发包
			ses.Send(&coredef.TestEchoACK{
				Content: proto.String(fmt.Sprintf("data#%d", id)),
			})

		})

	}

	pipe.Start()

	// 等待完成
	endAcc.Wait()

	fmt.Println("multi connection close test done!")

}
Esempio n. 14
0
// 网关服务器
func gateServer() {

	gate.DebugMode = true

	pipe := cellnet.NewEventPipe()

	gate.StartBackendAcceptor(pipe, "127.0.0.1:7201")
	gate.StartClientAcceptor(pipe, "127.0.0.1:7101")

	socket.RegisterSessionMessage(gate.ClientAcceptor, coredef.SessionAccepted{}, func(content interface{}, ses cellnet.Session) {

		log.Println("client accepted", ses.ID())

	})

	pipe.Start()

	<-done
}
Esempio n. 15
0
func server() {

	pipe := cellnet.NewEventPipe()

	evq := socket.NewAcceptor(pipe).Start("127.0.0.1:7234")

	socket.RegisterSessionMessage(evq, coredef.TestEchoACK{}, func(content interface{}, ses cellnet.Session) {
		msg := content.(*coredef.TestEchoACK)

		log.Println("server recv:", msg.String())

		ses.Send(&coredef.TestEchoACK{
			Content: proto.String(msg.String()),
		})

	})

	pipe.Start()

}
Esempio n. 16
0
func TestDelay(t *testing.T) {

	signal := test.NewSignalTester(t)

	pipe := cellnet.NewEventPipe()

	evq := pipe.AddQueue()

	pipe.Start()

	log.Debugln("delay 1 sec begin")

	evq.DelayPostData(time.Second, func() {

		log.Debugln("delay done")
		signal.Done(1)
	})

	signal.WaitAndExpect(1, "delay not work")
}
Esempio n. 17
0
func server() {

	pipe := cellnet.NewEventPipe()

	evq := socket.NewAcceptor(pipe).Start("127.0.0.1:7201")

	socket.RegisterSessionMessage(evq, "gamedef.TestEchoACK", func(content interface{}, ses cellnet.Session) {
		msg := content.(*gamedef.TestEchoACK)

		log.Debugln("server recv:", msg.String())

		ses.Send(&gamedef.TestEchoACK{
			Content: msg.String(),
		})

	})

	pipe.Start()

}
Esempio n. 18
0
// 客户端连接上后, 主动断开连接, 确保连接正常关闭
func testConnActiveClose() {

	pipe := cellnet.NewEventPipe()

	p := socket.NewConnector(pipe).Start("127.0.0.1:7201")

	socket.RegisterSessionMessage(p, coredef.SessionConnected{}, func(content interface{}, ses cellnet.Session) {

		signal.Done(1)
		// 连接上发包,告诉服务器不要断开
		ses.Send(&coredef.TestEchoACK{
			Content: proto.String("noclose"),
		})

	})

	socket.RegisterSessionMessage(p, coredef.TestEchoACK{}, func(content interface{}, ses cellnet.Session) {
		msg := content.(*coredef.TestEchoACK)

		log.Debugln("client recv:", msg.String())
		signal.Done(2)

		// 客户端主动断开
		ses.Close()

	})

	socket.RegisterSessionMessage(p, coredef.SessionClosed{}, func(content interface{}, ses cellnet.Session) {

		log.Debugln("close ok!")
		// 正常断开
		signal.Done(3)

	})

	pipe.Start()

	signal.WaitAndExpect(1, "TestConnActiveClose not connected")
	signal.WaitAndExpect(2, "TestConnActiveClose not recv msg")
	signal.WaitAndExpect(3, "TestConnActiveClose not close")
}
Esempio n. 19
0
func server() {

	pipe := cellnet.NewEventPipe()

	p := socket.NewAcceptor(pipe).Start("127.0.0.1:7201")
	rpc.InstallServer(p)

	rpc.RegisterMessage(p, coredef.TestEchoACK{}, func(resp rpc.Response, content interface{}) {
		msg := content.(*coredef.TestEchoACK)

		log.Debugln("server recv:", msg.String())

		resp.Feedback(&coredef.TestEchoACK{
			Content: msg.String(),
		})

	})

	pipe.Start()

}
Esempio n. 20
0
func client() {

	pipe := cellnet.NewEventPipe()

	evq := socket.NewConnector(pipe).Start(benchmarkAddress)

	socket.RegisterSessionMessage(evq, "gamedef.TestEchoACK", func(content interface{}, ses cellnet.Session) {

		ses.Send(&gamedef.TestEchoACK{})

	})

	socket.RegisterSessionMessage(evq, "gamedef.SessionConnected", func(content interface{}, ses cellnet.Session) {

		ses.Send(&gamedef.TestEchoACK{})

	})

	pipe.Start()

}
Esempio n. 21
0
func runServer() {
	pipe := cellnet.NewEventPipe()

	p := socket.NewAcceptor(pipe).Start("127.0.0.1:7201")

	socket.RegisterSessionMessage(p, coredef.TestEchoACK{}, func(content interface{}, ses cellnet.Session) {
		msg := content.(*coredef.TestEchoACK)

		// 发包后关闭
		ses.Send(&coredef.TestEchoACK{
			Content: proto.String(msg.GetContent()),
		})

		if msg.GetContent() != "noclose" {
			ses.Close()
		}

	})

	pipe.Start()

}
Esempio n. 22
0
func runServer() {
	pipe := cellnet.NewEventPipe()

	p := socket.NewAcceptor(pipe).Start("127.0.0.1:7201")

	socket.RegisterSessionMessage(p, "gamedef.TestEchoACK", func(content interface{}, ses cellnet.Session) {
		msg := content.(*gamedef.TestEchoACK)

		// 发包后关闭
		ses.Send(&gamedef.TestEchoACK{
			Content: msg.Content,
		})

		if msg.Content != "noclose" {
			ses.Close()
		}

	})

	pipe.Start()

}
Esempio n. 23
0
func server() {

	pipe := cellnet.NewEventPipe()

	p := socket.NewAcceptor(pipe)
	p.SetName("server")
	p.Start("127.0.0.1:9201")

	rpc.RegisterMessage(p, "gamedef.TestEchoACK", func(content interface{}, resp rpc.Response) {
		msg := content.(*gamedef.TestEchoACK)

		log.Debugln("server recv:", msg.String())

		resp.Feedback(&gamedef.TestEchoACK{
			Content: msg.String(),
		})

	})

	pipe.Start()

}
Esempio n. 24
0
// 接收封包后被断开
func testRecvDisconnected() {

	pipe := cellnet.NewEventPipe()

	p := socket.NewConnector(pipe).Start("127.0.0.1:7201")

	socket.RegisterSessionMessage(p, coredef.SessionConnected{}, func(content interface{}, ses cellnet.Session) {

		// 连接上发包
		ses.Send(&coredef.TestEchoACK{
			Content: proto.String("data"),
		})

		signal.Done(1)
	})

	socket.RegisterSessionMessage(p, coredef.TestEchoACK{}, func(content interface{}, ses cellnet.Session) {
		msg := content.(*coredef.TestEchoACK)

		log.Debugln("client recv:", msg.String())

		signal.Done(2)

	})

	socket.RegisterSessionMessage(p, coredef.SessionClosed{}, func(content interface{}, ses cellnet.Session) {

		// 断开
		signal.Done(3)
	})

	pipe.Start()

	signal.WaitAndExpect(1, "TestRecvDisconnected not connected")
	signal.WaitAndExpect(2, "TestRecvDisconnected not recv msg")
	signal.WaitAndExpect(3, "TestRecvDisconnected not closed")

}
Esempio n. 25
0
func client() {

	pipe := cellnet.NewEventPipe()

	p := socket.NewConnector(pipe).Start("127.0.0.1:7234")

	rpc.InstallClient(p)

	socket.RegisterSessionMessage(p, coredef.SessionConnected{}, func(content interface{}, ses cellnet.Session) {

		rpc.Call(p, &coredef.TestEchoACK{
			Content: proto.String("rpc hello"),
		}, func(msg *coredef.TestEchoACK) {

			log.Println("client recv", msg.GetContent())

			done <- true
		})

	})

	pipe.Start()
}
Esempio n. 26
0
// 后台服务器
func backendServer() {

	router.DebugMode = true

	pipe := cellnet.NewEventPipe()

	router.StartBackendConnector(pipe, []string{"127.0.0.1:7201"}, "svc->backend", "game")

	router.RegisterMessage("coredef.SessionClosed", func(content interface{}, routerSes cellnet.Session, clientid int64) {
		log.Debugf("client closed router: %d clientid: %d\n", routerSes.ID(), clientid)
	})

	router.RegisterMessage("coredef.TestEchoACK", func(content interface{}, routerSes cellnet.Session, clientid int64) {
		msg := content.(*coredef.TestEchoACK)

		log.Debugf("recv relay,  router: %d clientid: %d\n", routerSes.ID(), clientid)

		// issue #2. 客户端列表未添加到map, @lantiao reported
		cllist := router.NewClientList()
		cllist.Add(routerSes, 1)
		cllist.Add(routerSes, 2)

		list := cllist.Get(routerSes)

		if list[0] == 1 && list[1] == 2 {
			signal.Done(2)
		}

		router.SendToClient(routerSes, clientid, &coredef.TestEchoACK{
			Content: msg.Content,
		})

	})

	pipe.Start()
}
Esempio n. 27
0
func rundb() {
	pipe := cellnet.NewEventPipe()

	evq := pipe.AddQueue()

	pipe.Start()

	mdb := db.NewMongoDriver()

	var err error

	err = mdb.Start(&db.Config{
		URL:       "127.0.0.1:27017/test",
		ConnCount: 1,
	})

	if err != nil {
		signal.Fail()
		return
	}

	mdb.Execute(func(ses *mgo.Session) {

		col := ses.DB("").C("test")

		var c char

		err := col.Find(bson.M{"name": "davy"}).One(&c)

		evq.PostData(func() {

			if err == mgo.ErrNotFound {

				mdb.Execute(func(ses *mgo.Session) {

					col := ses.DB("").C("test")

					log.Debugln("insert new")

					col.Insert(&char{Name: "davy", HP: 10})
					col.Insert(&char{Name: "zerg", HP: 90})

					evq.PostData(func() {

						signal.Done(1)

						update(mdb, evq)
					})

				})

			} else {

				log.Debugln("exist")

				log.Debugln(c)

				signal.Done(1)
				update(mdb, evq)
			}
		})

	})

	signal.WaitAndExpect(1, "find failed")
	signal.WaitAndExpect(2, "update failed")

}
Esempio n. 28
0
func db() {
	pipe := cellnet.NewEventPipe()

	evq := pipe.AddQueue()

	pipe.Start()

	db := mgo.NewDB()

	var err error

	err = db.Start(&mgo.Config{
		URL:     "127.0.0.1:27017/test",
		ShowLog: true,
	})

	if err != nil {
		log.Errorln("db connect failed:", err)
		signal.Fail()
		return
	}

	db.FindOne(evq, "test", bson.M{"name": "davy"}, func(c *char, _ error) {

		// 没有记录, 创建
		if c == nil {
			db.Insert(evq, "test", &char{Name: "davy", HP: 10}, nil)
			db.Insert(evq, "test", &char{Name: "zerg", HP: 90}, func(_ error) {

				db.FindOne(evq, "test", bson.M{"name": "davy"}, func(c *char, _ error) {

					if c == nil {
						signal.Log("can not found record")
						signal.Fail()
					} else {
						log.Debugln(c)
						signal.Done(1)
					}

				})

			})

			// 有记录, 搞定
		} else {

			log.Debugln(c)
			signal.Done(1)

		}

	})

	db.Update(evq, "test", bson.M{"name": "davy"}, &char{Name: "davy", HP: 1}, func(err error) {

		if err != nil {
			signal.Log("update failed")
			signal.Fail()
		}

		db.FindOne(evq, "test", bson.M{"name": "davy"}, func(c *char, _ error) {

			if c == nil {
				signal.Log("update failed")
				signal.Fail()
			} else {
				if c.HP != 1 {
					signal.Fail()
				} else {
					signal.Done(2)
				}

			}

		})
	})

	signal.WaitAndExpect(1, "find failed")
	signal.WaitAndExpect(2, "update failed")

}