|
|
@ -23,12 +23,19 @@ type RoleID uint32
|
|
|
|
|
|
|
|
|
|
|
|
type ServerSession struct {
|
|
|
|
type ServerSession struct {
|
|
|
|
Session
|
|
|
|
Session
|
|
|
|
|
|
|
|
active atomic.Bool
|
|
|
|
LastSeen time.Time
|
|
|
|
LastSeen time.Time
|
|
|
|
IncomingPackets chan[]byte
|
|
|
|
IncomingPackets chan[]byte
|
|
|
|
OutgoingPackets chan Payload
|
|
|
|
OutgoingPackets chan Payload
|
|
|
|
Channels []ChannelID
|
|
|
|
Channels []ChannelID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func(session *ServerSession) Send(payload Payload) {
|
|
|
|
|
|
|
|
if session.active.Load() {
|
|
|
|
|
|
|
|
session.OutgoingPackets <- payload
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
type Server struct {
|
|
|
|
key ed25519.PrivateKey
|
|
|
|
key ed25519.PrivateKey
|
|
|
|
active atomic.Bool
|
|
|
|
active atomic.Bool
|
|
|
@ -90,7 +97,7 @@ const SESSION_BUFFER_SIZE = 256
|
|
|
|
|
|
|
|
|
|
|
|
func handle_session_outgoing(session *ServerSession, server *Server) {
|
|
|
|
func handle_session_outgoing(session *ServerSession, server *Server) {
|
|
|
|
server.Log("Starting session outgoing goroutine %s", session.ID)
|
|
|
|
server.Log("Starting session outgoing goroutine %s", session.ID)
|
|
|
|
for true {
|
|
|
|
for session.active.Load() {
|
|
|
|
packet := <- session.OutgoingPackets
|
|
|
|
packet := <- session.OutgoingPackets
|
|
|
|
if packet == nil {
|
|
|
|
if packet == nil {
|
|
|
|
break
|
|
|
|
break
|
|
|
@ -133,22 +140,19 @@ type SessionChannelCommand struct {
|
|
|
|
func handle_session_incoming(session *ServerSession, server *Server) {
|
|
|
|
func handle_session_incoming(session *ServerSession, server *Server) {
|
|
|
|
server.Log("Starting session incoming goroutine %s", session.ID)
|
|
|
|
server.Log("Starting session incoming goroutine %s", session.ID)
|
|
|
|
ping_timer := time.After(SESSION_PING_TIME)
|
|
|
|
ping_timer := time.After(SESSION_PING_TIME)
|
|
|
|
running := true
|
|
|
|
for session.active.Load() {
|
|
|
|
for running {
|
|
|
|
|
|
|
|
select {
|
|
|
|
select {
|
|
|
|
case <- ping_timer:
|
|
|
|
case <- ping_timer:
|
|
|
|
if time.Now().Add(-1*SESSION_TIMEOUT).Compare(session.LastSeen) != 1 {
|
|
|
|
if time.Now().Add(-1*SESSION_TIMEOUT).Compare(session.LastSeen) != 1 {
|
|
|
|
server.sessions_lock.Lock()
|
|
|
|
server.sessions_lock.Lock()
|
|
|
|
server.close_session(session)
|
|
|
|
server.close_session(session)
|
|
|
|
server.sessions_lock.Unlock()
|
|
|
|
server.sessions_lock.Unlock()
|
|
|
|
running = false
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
session.OutgoingPackets <- NewPingPacket()
|
|
|
|
session.OutgoingPackets <- NewPingPacket()
|
|
|
|
ping_timer = time.After(SESSION_PING_TIME)
|
|
|
|
ping_timer = time.After(SESSION_PING_TIME)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case encrypted := <- session.IncomingPackets:
|
|
|
|
case encrypted := <- session.IncomingPackets:
|
|
|
|
if encrypted == nil {
|
|
|
|
if encrypted == nil {
|
|
|
|
running = false
|
|
|
|
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -185,7 +189,7 @@ func handle_session_incoming(session *ServerSession, server *Server) {
|
|
|
|
if slices.Contains(members, session) {
|
|
|
|
if slices.Contains(members, session) {
|
|
|
|
mode, has_mode := channel.Modes[packet.Mode]
|
|
|
|
mode, has_mode := channel.Modes[packet.Mode]
|
|
|
|
if has_mode {
|
|
|
|
if has_mode {
|
|
|
|
mode.Load().(Mode).Data(session, packet.Channel, members, data)
|
|
|
|
mode.Load().(Mode).Data(session, packet.Channel, members, packet.Data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -214,6 +218,7 @@ func(server *Server) handle_session_open(client_session_open []byte, from *net.U
|
|
|
|
IncomingPackets: make(chan[]byte, SESSION_BUFFER_SIZE),
|
|
|
|
IncomingPackets: make(chan[]byte, SESSION_BUFFER_SIZE),
|
|
|
|
OutgoingPackets: make(chan Payload, SESSION_BUFFER_SIZE),
|
|
|
|
OutgoingPackets: make(chan Payload, SESSION_BUFFER_SIZE),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
server.sessions[session.ID].active.Store(true)
|
|
|
|
server.sessions_lock.Unlock()
|
|
|
|
server.sessions_lock.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
go handle_session_outgoing(server.sessions[session.ID], server)
|
|
|
|
go handle_session_outgoing(server.sessions[session.ID], server)
|
|
|
@ -362,6 +367,7 @@ func(server *Server) listen_udp() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func(server *Server) close_session(session *ServerSession) {
|
|
|
|
func(server *Server) close_session(session *ServerSession) {
|
|
|
|
|
|
|
|
session.active.Store(false)
|
|
|
|
close(session.IncomingPackets)
|
|
|
|
close(session.IncomingPackets)
|
|
|
|
close(session.OutgoingPackets)
|
|
|
|
close(session.OutgoingPackets)
|
|
|
|
delete(server.sessions, session.ID)
|
|
|
|
delete(server.sessions, session.ID)
|
|
|
|