Example #1
0
func server() {

	disp := dispatcher.NewPacketDispatcher()

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

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

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

	ltvsocket.SpawnAcceptor("127.0.0.1:8001", dispatcher.PeerHandler(disp))
}
Example #2
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())
		}

	})

}
Example #3
0
func listenNexus() {

	listenAddr := config.Listen

	ltvsocket.SpawnAcceptor(listenAddr, dispatcher.PeerHandler(Dispatcher))

	dispatcher.RegisterMessage(Dispatcher, coredef.RegionLinkREQ{}, func(ses cellnet.CellID, content interface{}) {

		msg := content.(*coredef.RegionLinkREQ)

		profile := msg.GetProfile()

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

		addRegion(ses, profile)

		ack := coredef.RegionLinkACK{
			AddressList: make([]*coredef.Region, 0),
			Status: &coredef.Region{
				ID:      proto.Int32(cellnet.RegionID),
				Address: proto.String(config.Listen),
			},
		}

		IterateRegion(func(profile *RegionData) {

			ack.AddressList = append(ack.AddressList, profile.Region)

		})

		cellnet.Send(ses, &ack)

	})

	dispatcher.RegisterMessage(Dispatcher, coredef.ClosedACK{}, func(ses cellnet.CellID, _ interface{}) {

		removeRegion(ses)

	})
}
Example #4
0
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))

}