gmessages/libgm/response_handler.go

49 lines
1.3 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
"fmt"
"go.mau.fi/mautrix-gmessages/libgm/pblite"
2023-06-30 09:54:08 +00:00
)
2023-07-15 22:45:57 +00:00
func (s *SessionHandler) waitResponse(requestID string) chan *pblite.Response {
ch := make(chan *pblite.Response, 1)
s.responseWaitersLock.Lock()
// DEBUG
if _, ok := s.responseWaiters[requestID]; ok {
panic(fmt.Errorf("request %s already has a response waiter", requestID))
}
// END DEBUG
s.responseWaiters[requestID] = ch
s.responseWaitersLock.Unlock()
return ch
2023-06-30 09:54:08 +00:00
}
2023-07-15 22:45:57 +00:00
func (s *SessionHandler) cancelResponse(requestID string, ch chan *pblite.Response) {
s.responseWaitersLock.Lock()
close(ch)
delete(s.responseWaiters, requestID)
s.responseWaitersLock.Unlock()
2023-06-30 09:54:08 +00:00
}
2023-07-15 22:45:57 +00:00
func (s *SessionHandler) receiveResponse(resp *pblite.Response) bool {
s.responseWaitersLock.Lock()
ch, ok := s.responseWaiters[resp.Data.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, resp.Data.RequestID)
s.responseWaitersLock.Unlock()
evt := s.client.Logger.Trace().
Str("request_id", resp.Data.RequestID)
if evt.Enabled() && resp.Data.Decrypted != nil {
evt.Str("proto_name", string(resp.Data.Decrypted.ProtoReflect().Descriptor().FullName())).
Str("data", base64.StdEncoding.EncodeToString(resp.Data.RawDecrypted))
}
evt.Msg("Received response")
ch <- resp
return true
2023-06-30 09:55:49 +00:00
}