Esempio n. 1
0
func NewNetClientEngine(frame_ms int64, max_frames, port int) (*Engine, error) {
	var params core.EngineParams
	params.Id = 1234
	params.Delay = core.StateFrame(frame_ms) + 10
	params.Frame_ms = frame_ms
	params.Max_frames = max_frames
	net, err := core.MakeTcpUdpNetwork(port)
	if err != nil {
		return nil, err
	}

	ticker := core.NewBasicTicker()
	local_event, bundler, updater, communicator, auditor := makeUnstarted(params, net, ticker)
	engine := Engine{
		bundler:      bundler,
		updater:      updater,
		communicator: communicator,
		auditor:      auditor,
		local_event:  local_event,
		net:          net,
	}
	// ticker.Start()

	rhs, err := net.Ping([]byte("ASDFADSFADSF"))
	if err != nil {
		return nil, err
	}
	if len(rhs) == 0 {
		return nil, errors.New("Didn't find any remote hosts.")
	}
	conn, err := net.Join(rhs[0], []byte("I am joinng"))
	if err != nil {
		return nil, err
	}

	boot, id, err := communicator.Join(conn)
	if err != nil {
		return nil, err
	}

	bundler.Params.Id = id
	updater.Params.Id = id
	bundler.Current_ms = params.Frame_ms * (int64(boot.Frame))
	bundler.Start()
	updater.Bootstrap(boot)
	communicator.Start()
	auditor.Start()

	return &engine, nil
}
Esempio n. 2
0
func NewNetEngine(initial_state Game, frame_ms int64, max_frames, port int) (*Engine, error) {
	var params core.EngineParams
	params.Id = 1234
	params.Delay = core.StateFrame(frame_ms) + 10
	params.Frame_ms = frame_ms
	params.Max_frames = max_frames
	net, err := core.MakeTcpUdpNetwork(port)
	if err != nil {
		return nil, err
	}

	local_event, bundler, updater, communicator, auditor := makeUnstarted(params, net, core.NewBasicTicker())
	engine := Engine{
		bundler:      bundler,
		updater:      updater,
		communicator: communicator,
		auditor:      auditor,
		local_event:  local_event,
		net:          net,
	}
	data := core.FrameData{
		Bundle: make(core.EventBundle),
		Game:   initial_state,
		Info: core.EngineInfo{
			Engines: map[core.EngineId]bool{params.Id: true},
		},
	}

	bundler.Current_ms = frame_ms + 1
	bundler.Start()
	updater.Start(0, data)
	communicator.Start()
	auditor.Start()

	ping_func := func([]byte) ([]byte, error) {
		return []byte("I AM A HOST!!!"), nil
	}
	join_func := func([]byte) error {
		return nil
	}
	net.Host(ping_func, join_func)

	return &engine, nil
}