Пример #1
0
func Exit(xid uint32, raddr *net.UDPAddr) {
	if clt := tcp.GetClient(); clt == nil {
		return
	} else if bs, err := newXRequest(xid, raddr, "exit", 0, nil, true); err != nil {
		counts.Count("rpc.exit.error", 1)
		xlog.ErrLog.Printf("[rpc]: rpc exit error = '%v'\n", err)
	} else {
		counts.Count("rpc.exit", 1)
		async.Call(uint64(xid), func() {
			clt.Send(bs)
		})
	}
}
Пример #2
0
func Call(xid uint32, raddr *net.UDPAddr, callback float64, data []byte, reliable bool) {
	if clt := tcp.GetClient(); clt == nil {
		counts.Count("rpc.call.noclient", 1)
		xlog.ErrLog.Printf("[rpc]: rpc is disabled\n")
	} else if bs, err := newXRequest(xid, raddr, "call", callback, data, reliable); err != nil {
		counts.Count("rpc.call.error", 1)
		xlog.ErrLog.Printf("[rpc]: rpc call error = '%v'\n", err)
	} else {
		counts.Count("rpc.call", 1)
		async.Call(uint64(xid), func() {
			clt.Send(bs)
		})
	}
}
Пример #3
0
func Start() {
	shell := func(f func()) {
		s := func() {
			defer func() {
				if x := recover(); x != nil {
					counts.Count("server.panic", 1)
					xlog.ErrLog.Printf("[server]: panic = %v\n%s\n", x, utils.Trace())
				}
			}()
			f()
		}
		for {
			s()
		}
	}
	for _, udpsrv := range udp.GetServers() {
		s := udpsrv
		f := func() {
			for {
				if lport, addr, data := s.Recv(); addr != nil && len(data) != 0 {
					if xid, err := rtmfp.PacketXid(data); err != nil {
						continue
					} else if xid == 0 {
						handshake.HandlePacket(lport, addr, data)
					} else {
						session.HandlePacket(lport, addr, xid, data)
					}
				}
			}
		}
		for i := 0; i < args.Parallel(); i++ {
			go shell(f)
		}
	}
	if s := tcp.GetServer(); s != nil {
		f := func() {
			for {
				if bs := s.Recv(); len(bs) != 0 {
					if x := rpc.DecodeXMessage(bs); x != nil {
						if b := x.Broadcast; b != nil {
							xids, data, reliable := b.Xids, b.Data, *b.Reliable
							if len(xids) == 0 || len(data) == 0 {
								continue
							}
							session.RecvPull(xids, data, reliable)
						}
						if c := x.Close; c != nil {
							xids := c.Xids
							if len(xids) == 0 {
								continue
							}
							session.CloseAll(xids)
						}
					}
				}
			}
		}
		go shell(f)
	}
	if c := tcp.GetClient(); c != nil {
		f := func() {
			for {
				if bs := c.Recv(); len(bs) != 0 {
					if x := rpc.DecodeXResponse(bs); x != nil {
						xid, data, callback, reliable := *x.Xid, x.Data, *x.Callback, *x.Reliable
						if xid == 0 || len(data) == 0 {
							continue
						}
						session.Callback(xid, data, callback, reliable)
					}
				}
			}
		}
		go shell(f)
	}
	for {
		time.Sleep(time.Minute)
	}
}