51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package inbound
|
|
|
|
import (
|
|
cmpp "github.com/bigwhite/gocmpp"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
const defaultHTTPTimeout = 10 * time.Second
|
|
|
|
type Server struct {
|
|
Addr string
|
|
APIBaseURL string
|
|
SecurityEventToken string
|
|
HTTPClient *http.Client
|
|
SubmitHTTPClient *http.Client
|
|
LogWriter io.Writer
|
|
PendingFlushInterval time.Duration
|
|
PresenceStore PresenceStore
|
|
RecoveryStore RecoveryStore
|
|
GatewayInstanceID string
|
|
MaxSubmitConcurrency int
|
|
}
|
|
|
|
func (s Server) ListenAndServe() error {
|
|
addr := s.Addr
|
|
if addr == "" {
|
|
addr = ":17890"
|
|
}
|
|
s.logRecoveryCandidates(log.Default())
|
|
go s.recoverPendingCandidates(log.Default())
|
|
go s.runPendingFlusher(log.Default())
|
|
return cmpp.ListenAndServeWithCloseAndSubmitWindow(addr, cmpp.V30, 30*time.Second, 3, s.LogWriter, s.handleConnectionClosed, submitWindowByConn,
|
|
cmpp.HandlerFunc(s.handleLogin),
|
|
cmpp.HandlerFunc(s.handleSubmit),
|
|
cmpp.HandlerFunc(s.handleActivity),
|
|
)
|
|
}
|
|
|
|
func boundedSubmitWindow(applicationWindow int, gatewayMaximum int) int {
|
|
if applicationWindow < 1 {
|
|
applicationWindow = 1
|
|
}
|
|
if gatewayMaximum < 1 {
|
|
gatewayMaximum = 64
|
|
}
|
|
return min(applicationWindow, min(gatewayMaximum, 1024))
|
|
}
|