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