예제 #1
0
func joinNexus(addr string) {

	ltvsocket.SpawnConnector(addr, dispatcher.PeerHandler(Dispatcher))

	dispatcher.RegisterMessage(Dispatcher, coredef.ConnectedACK{}, func(src cellnet.CellID, _ interface{}) {
		cellnet.Send(src, &coredef.RegionLinkREQ{
			Profile: &coredef.Region{
				ID:      proto.Int32(cellnet.RegionID),
				Address: proto.String(config.Listen),
			},
		})
	})

	dispatcher.RegisterMessage(Dispatcher, coredef.RegionLinkACK{}, func(src cellnet.CellID, content interface{}) {

		msg := content.(*coredef.RegionLinkACK)

		status := msg.GetStatus()

		if status.GetID() == cellnet.RegionID {
			log.Printf("[nexus] duplicate regionid: %d@%s", status.GetID(), status.GetAddress())
			return
		}

		addRegion(src, status)

		for _, rg := range msg.GetAddressList() {

			//log.Printf("address: %d@%s", rg.GetID(), rg.GetAddress())

			// 不能是自己
			if rg.GetID() == cellnet.RegionID {
				continue
			}

			// 已经连上了, 不再连接
			if GetRegion(rg.GetID()) != nil {
				continue
			}

			// 连接地址中的服务器
			joinNexus(rg.GetAddress())
		}

	})

}
예제 #2
0
파일: main.go 프로젝트: huangbenyu/cellnet
func client() {

	disp := dispatcher.NewPacketDispatcher()

	dispatcher.RegisterMessage(disp, coredef.TestEchoACK{}, func(ses cellnet.CellID, content interface{}) {
		msg := content.(*coredef.TestEchoACK)

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

		done <- true
	})

	dispatcher.RegisterMessage(disp, coredef.ConnectedACK{}, func(ses cellnet.CellID, content interface{}) {
		cellnet.Send(ses, &coredef.TestEchoACK{
			Content: proto.String("hello"),
		})
	})

	ltvsocket.SpawnConnector("127.0.0.1:8001", dispatcher.PeerHandler(disp))

}
예제 #3
0
파일: main.go 프로젝트: huangbenyu/cellnet
func client() {

	ltvsocket.SpawnConnector("127.0.0.1:8001", func(self cellnet.CellID, cm interface{}) {

		switch v := cm.(type) {
		case ltvsocket.EventConnected:

			// new session
			ltvsocket.SpawnSession(v.Stream(), func(ses cellnet.CellID, sescm interface{}) {

				switch pkt := sescm.(type) {
				case *cellnet.Packet:

					var ack coredef.TestEchoACK
					if err := proto.Unmarshal(pkt.Data, &ack); err != nil {
						log.Println(err)
					} else {
						log.Println("client recv", ack.String())

						done <- true
					}

				}

			})

			// send packet on connected
			v.Stream().Write(cellnet.BuildPacket(&coredef.TestEchoACK{
				Content: proto.String("hello"),
			}))

		case IError:
			log.Println(cellnet.ReflectContent(v))

		}

	})

}