github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/ChaparKhane/services.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package chaparkhane
     4  
     5  // services store server services!
     6  type services struct {
     7  	services []*Service
     8  	byID     map[uint32]PacketHandler
     9  }
    10  
    11  func (ss *services) init() {
    12  	if ss.byID == nil {
    13  		ss.byID = make(map[uint32]PacketHandler)
    14  	}
    15  }
    16  
    17  // RegisterService use to set or change specific service detail!
    18  func (ss *services) RegisterService(s *Service) {
    19  	if s.ID == 0 {
    20  		Log("This service: ", s.Name, ", give 0 as service ID! it illegal to use 0 as ID! It must hash of service name")
    21  		panic("ChaparKhane occur panic situation due to ^")
    22  	}
    23  
    24  	_, ok := ss.byID[s.ID]
    25  	if ok {
    26  		// Warn developer this ServiceID use for other service and this panic
    27  		Log("This ID: ", s.ID, ", Used before for other service and it illegal to reuse IDs")
    28  		panic("ChaparKhane occur panic situation due to ^")
    29  	} else {
    30  		ss.byID[s.ID] = s.Handler
    31  		ss.services = append(ss.services, s)
    32  	}
    33  }
    34  
    35  // GetServiceHandlerByID use to get specific service handler by service ID!
    36  func (ss *services) GetServiceHandlerByID(serviceID uint32) (PacketHandler, bool) {
    37  	h, ok := ss.byID[serviceID]
    38  	return h, ok
    39  }