Ejemplo n.º 1
0
func (h *Handshake) handleAssign(r *xio.PacketReader) (rtmfp.ResponseMessage, uint32, error) {
	if req, err := parseAssignRequest(r); err != nil {
		return nil, 0, err
	} else {
		cookie := cookies.Find(string(req.cookie))
		if cookie == nil {
			return nil, 0, errors.New("assign.cookie not found")
		}
		cookie.Lock()
		defer cookie.Unlock()
		if cookie.Xid == 0 {
			responder, encrypt, decrypt := rtmfp.ComputeSharedKeys(h, req.pubkey, req.initiator)
			cookie.Pid = req.pid
			cookie.Responder = responder
			if xid, err := session.Create(req.yid, cookie.Pid, cookie.Value(), encrypt, decrypt, h.lport, h.raddr); err != nil {
				counts.Count("handshake.session.error", 1)
				return nil, 0, errors.New(fmt.Sprintf("assign.create session = %v", err))
			} else {
				cookie.Xid = xid
				counts.Count("handshake.assign", 1)
				xlog.SssLog.Printf("[join] %s [%s] xid = %d\n", xlog.StringToHex(cookie.Pid), h.raddr, xid)
			}
			xlog.OutLog.Printf("[handshake]: new session xid = %d from [%s]\n", cookie.Xid, h.raddr)
		}
		return &assignResponse{cookie.Xid, cookie.Responder}, req.yid, nil
	}
}
Ejemplo n.º 2
0
func init() {
	sessions.lastxid = 0
	for i := 0; i < len(sessions.buckets); i++ {
		sessions.buckets[i].xidmap = make(map[uint32]*Session, 8192)
		sessions.buckets[i].pidmap = make(map[string]*Session, 8192)
	}
	for i := 0; i < len(sessions.manages); i++ {
		m := &sessions.manages[i]
		m.freshlist = list.New()
		m.alivelist = list.New()
		go func() {
			manage := args.Manage()
			for {
				m.Lock()
				if m.freshlist.Len() != 0 {
					m.alivelist.PushBackList(m.freshlist)
					m.freshlist.Init()
				}
				m.Unlock()
				count := 0
				if e := m.alivelist.Front(); e != nil {
					for e != nil {
						next := e.Next()
						if s := e.Value.(*Session); s.Manage() {
							delSessionByXid(s.xid)
							delSessionByPid(s.pid)
							m.alivelist.Remove(e)
							count++
							xlog.SssLog.Printf("[exit] %s [%s] xid = %d cnt = %d\n", xlog.StringToHex(s.pid), s.raddr, s.xid, s.manage.cnt)
						}
						e = next
					}
				}
				if count != 0 {
					counts.Count("session.cleanup", count)
				}
				time.Sleep(time.Millisecond * time.Duration(manage))
			}
		}()
	}
}