Exemplo n.º 1
0
func server() {
	ltvsocket.SpawnAcceptor("127.0.0.1:8001", func(self cellnet.CellID, cm interface{}) {

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

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

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

					log.Println("server recv:", cellnet.ReflectContent(pkt))

					v.Stream().Write(cellnet.BuildPacket(&coredef.TestEchoACK{
						Content: proto.String("world"),
					}))
				}

			})

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

	})
}
Exemplo n.º 2
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))
}
Exemplo n.º 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)

	})
}