示例#1
0
func SpawnSession(stream cellnet.IPacketStream, callback func(cellnet.CellID, interface{})) cellnet.CellID {

	cid := cellnet.Spawn(callback)

	// io线程
	go func() {
		var err error
		var pkt *cellnet.Packet

		for {

			// 从Socket读取封包并转为ltv格式
			pkt, err = stream.Read()

			if err != nil {

				cellnet.Send(cid, EventClose{error: err})
				break
			}

			cellnet.Send(cid, pkt)

		}

	}()

	return cid
}
示例#2
0
func SpawnConnector(address string, callback func(cellnet.CellID, interface{})) cellnet.CellID {

	cid := cellnet.Spawn(callback)

	// io goroutine
	go func() {

		if config.SocketLog {
			log.Printf("[socket] #connect %s %s\n", cid.String(), address)
		}

		conn, err := net.Dial("tcp", address)
		if err != nil {

			cellnet.Send(cid, EventConnectError{error: err})

			if config.SocketLog {
				log.Println("[socket] connect failed", err.Error())
			}
			return
		}

		cellnet.Send(cid, EventConnected{stream: NewPacketStream(conn)})

	}()

	return cid

}
示例#3
0
func client() {
	// cellid: 1.1 acceptor
	// cellid: 1.2 connector

	// cellid: 1.3
	cid := cellnet.Spawn(func(src cellnet.CellID, data interface{}) {

		switch d := data.(type) {
		case *cellnet.Packet:
			log.Println("recv node msg", src.String(), cellnet.ReflectContent(d))
		}

	})

	log.Println(cid.String())

	nexus.Register(nexus.Dispatcher)

	// cellid: 1.4
	dispatcher.RegisterMessage(nexus.Dispatcher, coredef.TestEchoACK{}, func(src cellnet.CellID, content interface{}) {

		msg := content.(*coredef.TestEchoACK)
		log.Println("recv msg callback :", msg.GetContent())
	})

}
示例#4
0
func spawnsend() {

	// no block spawn cell, msg function here
	cid := cellnet.Spawn(func(_ cellnet.CellID, cl interface{}) {

		switch v := cl.(type) {
		case string:
			log.Println(v)
		}

	})

	cellnet.Send(cid, "hello world ")

	done <- true
}
示例#5
0
func SpawnAcceptor(address string, callback func(cellnet.CellID, interface{})) cellnet.CellID {

	cid := cellnet.Spawn(callback)

	// io goroutine
	go func() {

		if config.SocketLog {
			log.Printf("[socket] #listen %s %s\n", cid.String(), address)
		}

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

		if err != nil {
			cellnet.Send(cid, EventListenError{error: err})

			if config.SocketLog {
				log.Println("[socket] listen failed", err.Error())
			}

			return
		}

		for {
			conn, err := ln.Accept()

			if err != nil {
				continue
			}

			cellnet.Send(cid, EventAccepted{stream: NewPacketStream(conn)})
		}

	}()

	return cid

}