Browse Source

Make sure all session data is available early enough.

pull/28/head
Simon Eisenmann 11 years ago committed by Simon Eisenmann
parent
commit
41cb800c15
  1. 15
      src/app/spreed-speakfreely-server/connection.go
  2. 25
      src/app/spreed-speakfreely-server/hub.go
  3. 13
      src/app/spreed-speakfreely-server/server.go
  4. 4
      src/app/spreed-speakfreely-server/session.go

15
src/app/spreed-speakfreely-server/connection.go

@ -112,23 +112,16 @@ func (c *Connection) close() { @@ -112,23 +112,16 @@ func (c *Connection) close() {
func (c *Connection) register() error {
id, err := c.h.CreateSessionid()
if err != nil {
log.Println("Failed to create new Id while register", err)
return err
}
c.Id = id
log.Println("Created new id", len(id), id)
c.h.registerHandler(c)
s := c.h.CreateSession(nil)
c.h.registerHandler(c, s)
return nil
}
func (c *Connection) reregister(token string) error {
if st, err := c.h.DecodeSessionToken(token); err == nil {
c.Id = st.Id
c.h.registerHandler(c)
c.Session.Apply(st)
s := c.h.CreateSession(st)
c.h.registerHandler(c, s)
} else {
log.Println("Error while decoding session token", err)
c.register()

25
src/app/spreed-speakfreely-server/hub.go

@ -159,13 +159,22 @@ func (h *Hub) CreateTurnData(id string) *DataTurn { @@ -159,13 +159,22 @@ func (h *Hub) CreateTurnData(id string) *DataTurn {
}
func (h *Hub) CreateSessionid() (string, error) {
func (h *Hub) CreateSession(st *SessionToken) *Session {
// NOTE(longsleep): Is it required to make this a secure cookie,
// random data in itself should be sufficent if we do not validate
// session ids somewhere?
value := fmt.Sprintf("%s", securecookie.GenerateRandomKey(32))
return h.tickets.Encode("id", value)
session := &Session{}
if st == nil {
session.Id, _ = h.tickets.Encode("id", fmt.Sprintf("%s", securecookie.GenerateRandomKey(32)))
log.Println("Created new session id", len(session.Id), session.Id)
} else {
session.Apply(st)
}
return session
}
@ -254,15 +263,17 @@ func (h *Hub) isDefaultRoomid(id string) bool { @@ -254,15 +263,17 @@ func (h *Hub) isDefaultRoomid(id string) bool {
return id == ""
}
func (h *Hub) registerHandler(c *Connection) {
func (h *Hub) registerHandler(c *Connection, s *Session) {
// Apply session to connection.
c.Id = s.Id
c.Session = s
h.mutex.Lock()
// Create new session instance.
// Set flags.
h.count++
c.Idx = h.count
s := &Session{Id: c.Id}
c.Session = s
c.IsRegistered = true
// Register connection or replace existing one.

13
src/app/spreed-speakfreely-server/server.go

@ -37,11 +37,18 @@ type Server struct { @@ -37,11 +37,18 @@ type Server struct {
func (s *Server) OnRegister(c *Connection) {
//log.Println("OnRegister", c.id)
st := &SessionToken{Id: c.Id}
if token, err := c.h.EncodeSessionToken(st); err == nil {
if token, err := c.h.EncodeSessionToken(c.Session.Token()); err == nil {
log.Println("Created new session token", len(token), token)
// Send stuff back.
s.Unicast(c, c.Id, &DataSelf{Type: "Self", Id: c.Id, Token: token, Version: c.h.version, Turn: c.h.CreateTurnData(c.Id), Stun: c.h.config.StunURIs})
s.Unicast(c, c.Id, &DataSelf{
Type: "Self",
Id: c.Id,
Userid: c.Session.Userid,
Token: token,
Version: c.h.version,
Turn: c.h.CreateTurnData(c.Id),
Stun: c.h.config.StunURIs,
})
} else {
log.Println("Error in OnRegister", c.Idx, err)
}

4
src/app/spreed-speakfreely-server/session.go

@ -68,6 +68,10 @@ func (s *Session) Apply(st *SessionToken) { @@ -68,6 +68,10 @@ func (s *Session) Apply(st *SessionToken) {
}
func (s *Session) Token() *SessionToken {
return &SessionToken{Id: s.Id, Userid: s.Userid}
}
func (s *Session) Data() *DataSession {
s.mutex.RLock()

Loading…
Cancel
Save