func (this *serviceMgr) OnRegiste(s *netlib.Session) { if this == nil || s == nil { return } if s.GetAttribute(SessionAttributeServiceFlag) == nil { return } attr := s.GetAttribute(SessionAttributeServerInfo) if attr != nil { if srvInfo, ok := attr.(*protocol.SSSrvRegiste); ok && srvInfo != nil { services := GetCareServicesBySession(srvInfo.GetType()) for _, v1 := range services { if v2, has := this.servicesPool[v1]; has { for _, v3 := range v2 { func(si *protocol.ServiceInfo, sInfo *protocol.SSSrvRegiste) { pack := &protocol.SSServiceInfo{} proto.SetDefaults(pack) pack.Service = si logger.Trace("serviceMgr.OnRegiste Server Type=", sInfo.GetType(), " Id=", sInfo.GetId(), " Name=", sInfo.GetName(), " careful => Service=", si) s.Send(pack) }(v3, srvInfo) } } } } } }
func (ssm *ServerSessionMgr) RegisteSession(s *netlib.Session) bool { attr := s.GetAttribute(SessionAttributeServerInfo) if attr != nil { if srvInfo, ok := attr.(*protocol.SSSrvRegiste); ok && srvInfo != nil { logger.Tracef("ServerSessionMgr.RegisteSession %v", srvInfo) areaId := int(srvInfo.GetAreaId()) srvType := int(srvInfo.GetType()) srvId := int(srvInfo.GetId()) if a, exist := ssm.sessions[areaId]; !exist { ssm.sessions[areaId] = make(map[int]map[int]*netlib.Session) a = ssm.sessions[areaId] a[srvType] = make(map[int]*netlib.Session) } else { if _, exist := a[srvType]; !exist { a[srvType] = make(map[int]*netlib.Session) } } ssm.sessions[areaId][srvType][srvId] = s if ssm.listener != nil { ssm.listener.OnRegiste(s) } } } else { logger.Tracef("ServerSessionMgr.RegisteSession SessionAttributeServerInfo=nil") } return true }
func (af *AuthenticationFilter) OnPacketReceived(s *netlib.Session, packetid int, packet interface{}) bool { if s.GetAttribute(SessionAttributeAuth) == nil { if auth, ok := packet.(*protocol.SSPacketAuth); ok { h := md5.New() rawText := fmt.Sprintf("%v;%v", auth.GetTimestamp(), s.GetSessionConfig().AuthKey) logger.Tracef("AuthenticationFilter rawtext=%v IsInnerLink(%v)", rawText, s.GetSessionConfig().IsInnerLink) h.Write([]byte(rawText)) expectKey := hex.EncodeToString(h.Sum(nil)) if expectKey != auth.GetAuthKey() { if af.SessionAuthHandler != nil { af.SessionAuthHandler(s, false) } s.Close() logger.Tracef("AuthenticationFilter AuthKey error[expect:%v get:%v]", expectKey, auth.GetAuthKey()) return false } s.SetAttribute(SessionAttributeAuth, true) if af.SessionAuthHandler != nil { af.SessionAuthHandler(s, true) } return false } else { s.Close() logger.Warn("AuthenticationFilter packet not expect") return false } } return true }
func (csm *ClientSessionMgr) UnregisteSession(s *netlib.Session) bool { attr := s.GetAttribute(SessionAttributeClientSession) if attr != nil { if sid, ok := attr.(SessionId); ok { delete(csm.sessions, sid.Get()) logger.Tracef("client session %v unregiste", sid.Get()) } } return true }
func (csm *ClientSessionMgr) RegisteSession(s *netlib.Session) bool { attr := s.GetAttribute(SessionAttributeClientSession) if attr == nil { sid := NewSessionId(s) s.SetAttribute(SessionAttributeClientSession, sid) csm.sessions[sid.Get()] = s logger.Tracef("client session %v registe", sid.Get()) } return true }
func (this *serviceMgr) ClearServiceBySession(s *netlib.Session) { attr := s.GetAttribute(SessionAttributeServiceInfo) if attr != nil { if services, ok := attr.([]*protocol.ServiceInfo); ok { for _, service := range services { this.UnregisteService(service) } } s.RemoveAttribute(SessionAttributeServiceInfo) } }
func (this *PacketSlicesHandler) Process(s *netlib.Session, data interface{}) error { if packetslices, ok := data.(*protocol.SSPacketSlices); ok { seqNo := int(packetslices.GetSeqNo()) if seqNo < 1 { return errors.New("PacketSlicesHandler unexpect packet seq:" + strconv.Itoa(seqNo)) } totalSize := int(packetslices.GetTotalSize()) if totalSize > s.GetSessionConfig().MaxPacket { return errors.New("PacketSlicesHandler exceed MaxPacket size:" + strconv.Itoa(s.GetSessionConfig().MaxPacket) + " size=" + strconv.Itoa(totalSize)) } attr := s.GetAttribute(SessionAttributeBigBuf) if seqNo == 1 { if attr == nil { attr = bytes.NewBuffer(make([]byte, 0, packetslices.GetTotalSize())) s.SetAttribute(SessionAttributeBigBuf, attr) } } if seqNo > 1 { if attr == nil { return errors.New("PacketSlicesHandler Incorrect packet seq, expect seq=1") } } else if attr == nil { return errors.New("PacketSlicesHandler get bytesbuf failed") } buf := attr.(*bytes.Buffer) if seqNo == 1 { buf.Reset() } if buf.Len() != int(packetslices.GetOffset()) { return errors.New("PacketSlicesHandler get next packet offset error") } buf.Write(packetslices.GetPacketData()) if buf.Len() == totalSize { packetid, pck, err := netlib.UnmarshalPacket(buf.Bytes()) if err != nil { return err } h := netlib.GetHandler(packetid) if h != nil { h.Process(s, pck) } } } return nil }
func (ssm *ServerSessionMgr) UnregisteSession(s *netlib.Session) bool { attr := s.GetAttribute(SessionAttributeServerInfo) if attr != nil { if srvInfo, ok := attr.(*protocol.SSSrvRegiste); ok && srvInfo != nil { logger.Tracef("ServerSessionMgr.UnregisteSession %v", srvInfo) areaId := int(srvInfo.GetAreaId()) srvType := int(srvInfo.GetType()) srvId := int(srvInfo.GetId()) if a, exist := ssm.sessions[areaId]; exist { if b, exist := a[srvType]; exist { delete(b, srvId) if ssm.listener != nil { ssm.listener.OnUnregiste(s) } } } } } return true }