Esempio n. 1
0
//处理由客户端的发起远程调用
func (app *RemoteApp) Handle(src rpc.Mailbox, method string, args interface{}) error {
	app.Lock()
	defer app.Unlock()

	if app.Id == core.AppId {
		log.LogMessage("rpc inner handle:", method)
		return core.rpcServer.Call("C2S"+method, src, args)
	}

	if app.Conn == nil {

		if app.Conn == nil {
			conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", app.Host, app.Port), time.Second)
			if err != nil {
				return err
			}
			app.Conn = conn
			app.RpcClient = rpc.NewClient(conn)
			log.LogMessage("rpc connected:", app.Name, ",", fmt.Sprintf("%s:%d", app.Host, app.Port))
		}
	}
	log.LogMessage("remote handle:", app.Name, "/", method)
	err := app.RpcClient.Call("C2S"+method, src, args)
	if err == rpc.ErrShutdown {
		log.LogError(err)
		app.Close()
	}
	return err
}
Esempio n. 2
0
//向客户端发起一个远程调用
func (app *RemoteApp) ClientCall(src *rpc.Mailbox, session int64, method string, args interface{}) error {

	var err error
	var pb share.S2CMsg
	pb.Sender = app.Name
	pb.To = session
	pb.Method = method

	if pb.Data, err = core.rpcProto.CreateRpcMessage(app.Name, method, args); err != nil {
		log.LogError(err)
		return err
	}

	if src == nil {
		src = &core.MailBox
	}

	app.Lock()
	defer app.Unlock()

	if app.Id == core.AppId { //进程内调用
		msg := NewMessage()
		msg.Write(pb)
		msg.Flush()
		core.s2chelper.Call(*src, msg.GetMessage())
		msg.Free()
		return nil
	}

	if app.Conn == nil {
		if app.Conn == nil {
			conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", app.Host, app.Port), time.Second)
			if err != nil {
				return err
			}
			app.Conn = conn
			app.RpcClient = rpc.NewClient(conn)
			log.LogMessage("rpc connected:", app.Name, ",", fmt.Sprintf("%s:%d", app.Host, app.Port))
		}
	}

	err = app.RpcClient.Call("S2SS2CHelper.Call", *src, pb)
	if err == rpc.ErrShutdown {
		app.Close()
	}

	return err
}
Esempio n. 3
0
func (app *RemoteApp) CallBack(src *rpc.Mailbox, method string, cb rpc.ReplyCB, args ...interface{}) error {

	if src == nil {
		src = &core.MailBox
	}

	app.Lock()
	defer app.Unlock()

	if app.Id == core.AppId { //进程内调用
		log.LogMessage("rpc inner call:", method)
		return core.rpcServer.CallBack("S2S"+method, *src, cb, args...)
	}

	if app.Conn == nil {
		if app.Conn == nil {
			conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", app.Host, app.Port), time.Second)
			if err != nil {
				log.LogError(err)
				return err
			}
			app.Conn = conn
			app.RpcClient = rpc.NewClient(conn)
			log.LogMessage("rpc connected:", app.Name, ",", fmt.Sprintf("%s:%d", app.Host, app.Port))
		}
	}
	log.LogMessage("remote call:", app.Name, "/", method)
	err := app.RpcClient.CallBack("S2S"+method, *src, cb, args...)

	if err != nil {
		app.Close()
		log.LogError(err)
	}

	return err
}