Beispiel #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: proto.String("rpc hello"),
		}, func(msg *coredef.TestEchoACK) {

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

			signal.Done(1)
		})

	})

	pipe.Start()

	signal.WaitAndExpect(1, "not recv data")
}
Beispiel #2
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: proto.String("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 gate")
	signal.WaitAndExpect(2, "not recv client msg")
	signal.WaitAndExpect(3, "not recv server msg")

}
Beispiel #3
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")

}
Beispiel #4
0
func client() {

	pipe := cellnet.NewEventPipe()

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

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

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

		signal.Done(1)
	})

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

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

	})

	pipe.Start()

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

}
Beispiel #5
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")
}
Beispiel #6
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")
}
Beispiel #7
0
func (self *socketAcceptor) Start(address string) cellnet.Peer {

	ln, err := net.Listen("tcp", address)

	self.listener = ln

	if err != nil {

		log.Errorln("[socket] listen failed", err.Error())
		return self
	}

	log.Debugln("[socket] listening: ", address)

	self.running = true

	// 接受线程
	go func() {
		for self.running {
			conn, err := ln.Accept()

			if err != nil {
				continue
			}

			ses := newSession(NewPacketStream(conn), self.EventQueue, self)

			// 添加到管理器
			self.sessionMgr.Add(ses)

			// 断开后从管理器移除
			ses.OnClose = func() {
				self.sessionMgr.Remove(ses)
			}

			// 通知逻辑
			self.PostData(NewSessionEvent(Event_SessionAccepted, ses, nil))

		}

	}()

	self.PostData(NewPeerEvent(Event_PeerStart, self))

	return self
}
Beispiel #8
0
func server() {

	pipe := cellnet.NewEventPipe()

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

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

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

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

	})

	pipe.Start()

}
Beispiel #9
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: proto.String(msg.String()),
		})

	})

	pipe.Start()

}
Beispiel #10
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")

}
Beispiel #11
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")

}
Beispiel #12
0
func (self *socketConnector) connect(address string) {
	self.working = true

	for {

		// 开始连接
		cn, err := net.Dial("tcp", address)

		// 连不上
		if err != nil {

			log.Errorln("[socket] cononect failed", err.Error())

			// 没重连就退出
			if self.autoReconnectSec == 0 {
				break
			}

			// 有重连就等待
			time.Sleep(time.Duration(self.autoReconnectSec) * time.Second)

			// 继续连接
			continue
		}

		log.Debugln("[socket] connected: ", address)

		// 连上了, 记录连接
		self.conn = cn

		// 创建Session
		ses := newSession(NewPacketStream(cn), self.EventQueue, self)
		self.sessionMgr.Add(ses)
		self.defaultSes = ses

		// 内部断开回调
		ses.OnClose = func() {
			self.sessionMgr.Remove(ses)
			self.closeSignal <- true
		}

		// 抛出事件
		self.PostData(NewSessionEvent(Event_SessionConnected, ses, nil))

		if <-self.closeSignal {

			self.conn = nil

			// 没重连就退出
			if self.autoReconnectSec == 0 {
				break
			}

			// 有重连就等待
			time.Sleep(time.Duration(self.autoReconnectSec) * time.Second)

			// 继续连接
			continue

		}

	}

	self.working = false
}