예제 #1
0
func Register_lua_common_SetInterval(L *lua.LState) int {
	identifier := L.CheckString(1)
	delay := L.CheckNumber(2)
	duration := L.CheckNumber(3)
	function := L.CheckFunction(4)
	indexSetIntervalMethods[identifier] = function

	go func() {
		time.Sleep(time.Duration(delay) * time.Second)
		for {
			err2 := L.CallByParam(lua.P{
				Fn:      indexSetIntervalMethods[identifier],
				NRet:    1,
				Protect: true,
			})

			if err2 != nil {
				logger.Warning("SetInterval [%v] already be clear!", identifier)
				break
			}

			time.Sleep(time.Duration(duration) * time.Second)
		}
	}()
	return 0
}
예제 #2
0
func (server *Server) register(rcvr interface{}, name string, useName bool, rcvrFns ...interface{}) error {
	server.mu.Lock()
	if server.serviceMap == nil {
		server.serviceMap = make(map[string]*service)
	}

	rcvrValue := reflect.ValueOf(rcvr)
	sname := reflect.Indirect(rcvrValue).Type().Name()
	if useName {
		sname = name
	}
	if sname == "" {
		logger.Fatal("rpc: no service name for type %v", reflect.ValueOf(rcvr).Interface())
	}
	if !isExported(sname) && !useName {
		s := "rpc Register: type " + sname + " is not exported"
		logger.Info(s)
		server.mu.Lock()
		return errors.New(s)
	}

	var s *service
	if value, ok := server.serviceMap[sname]; ok {
		server.mu.Lock()
		s = value
		logger.Warning("rpc: service already defined: %s", sname)
		//return errors.New("rpc: service already defined: " + sname)
	} else {

		s = new(service)
		s.typ = reflect.TypeOf(rcvr)
		s.rcvr = reflect.ValueOf(rcvr)
		s.name = sname
		s.method = make(map[uint32]*methodType)
	}

	// Install the methods
	// logger.Debug("Install the methods begine!")
	s.method = server.suitableMethods(rcvr, s, s.typ, true, rcvrFns...)

	if len(s.method) == 0 {
		str := ""
		// To help the user, see if a pointer receiver would work.
		method := server.suitableMethods(rcvr, s, reflect.PtrTo(s.typ), false)
		if len(method) != 0 {
			str = "rpc.Register: type " + sname + " has no exported methods of suitable type (hint: pass a pointer to value of that type)"
		} else {
			str = "rpc.Register: type " + sname + " has no exported methods of suitable type"
		}
		logger.Info(str)
		server.mu.Unlock()
		return errors.New(str)
	}
	server.serviceMap[s.name] = s
	server.mu.Unlock()
	return nil
}