gmessages/libgm/response_handler.go

49 lines
1.2 KiB
Go
Raw Normal View History

2023-06-30 11:05:33 +00:00
package libgm
2023-06-30 09:54:08 +00:00
import (
2023-07-15 22:45:57 +00:00
"encoding/base64"
2023-06-30 09:54:08 +00:00
)
func (s *SessionHandler) waitResponse(requestID string) chan *IncomingRPCMessage {
ch := make(chan *IncomingRPCMessage, 1)
2023-07-15 22:45:57 +00:00
s.responseWaitersLock.Lock()
s.responseWaiters[requestID] = ch
s.responseWaitersLock.Unlock()
return ch
2023-06-30 09:54:08 +00:00
}
func (s *SessionHandler) cancelResponse(requestID string, ch chan *IncomingRPCMessage) {
2023-07-15 22:45:57 +00:00
s.responseWaitersLock.Lock()
close(ch)
delete(s.responseWaiters, requestID)
s.responseWaitersLock.Unlock()
2023-06-30 09:54:08 +00:00
}
func (s *SessionHandler) receiveResponse(msg *IncomingRPCMessage) bool {
2023-07-18 12:13:53 +00:00
if msg.Message == nil {
return false
}
requestID := msg.Message.SessionID
2023-07-15 22:45:57 +00:00
s.responseWaitersLock.Lock()
ch, ok := s.responseWaiters[requestID]
2023-06-30 09:54:08 +00:00
if !ok {
2023-07-15 22:45:57 +00:00
s.responseWaitersLock.Unlock()
return false
}
delete(s.responseWaiters, requestID)
2023-07-15 22:45:57 +00:00
s.responseWaitersLock.Unlock()
evt := s.client.Logger.Trace().
Str("request_id", requestID)
if evt.Enabled() {
if msg.DecryptedData != nil {
evt.Str("data", base64.StdEncoding.EncodeToString(msg.DecryptedData))
}
if msg.DecryptedMessage != nil {
evt.Str("proto_name", string(msg.DecryptedMessage.ProtoReflect().Descriptor().FullName()))
}
2023-07-15 22:45:57 +00:00
}
evt.Msg("Received response")
ch <- msg
2023-07-15 22:45:57 +00:00
return true
2023-06-30 09:55:49 +00:00
}