Skip to content

Commit

Permalink
update api/conns
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Oct 17, 2023
1 parent 1043634 commit 7b2578d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
7 changes: 6 additions & 1 deletion cmds/liter-server/api_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,18 +511,23 @@ func registerPlayerAPI(s *Server, g *gin.RouterGroup){
func registerStatus(s *Server, g *gin.RouterGroup){
g.GET("/conns", func(ctx *gin.Context){
type resT struct {
Id int `json:"id"`
Addr string `json:"addr"`
When int64 `json:"when"`
Player *liter.PlayerInfo `json:"player,omitempty"`
}
ctx.Header("Cache-Control", "no-cache")
lm := s.conns.LastModified().Format(time.RFC1123)
data := make([]resT, 0, s.conns.Count())
s.conns.ForEach(func(_ int, conn *Conn){
s.conns.ForEach(func(i int, conn *Conn){
data = append(data, resT{
Id: i,
Addr: conn.RemoteAddr().String(),
When: conn.When.Unix(),
Player: conn.Player(),
})
})
ctx.Header("Last-Modified", lm)
ctx.JSON(http.StatusOK, gin.H{
"status": "ok",
"data": data,
Expand Down
12 changes: 7 additions & 5 deletions cmds/liter-server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func (s *Server)handle(c *liter.Conn, cfg *Config){
}
rc.SetReadDeadline(time.Time{})
ploger.Tracef("Handshake packet: %v", hp)
isPing := hp.NextState == liter.NextPingState
isLogin := hp.NextState == liter.NextLoginState

var svr *ServerIns = nil
F: for _, s := range cfg.Servers {
Expand All @@ -51,13 +53,13 @@ func (s *Server)handle(c *liter.Conn, cfg *Config){

var lp liter.LoginStartPacket

if hp.NextState == liter.NextPingState {
if isPing {
if svr.HandlePing {
ploger.Debugf("Handle ping connection for server %q", svr.Id)
handleServerStatus(ploger, c, "Idle", svr.Motd)
return
}
}else if hp.NextState == liter.NextLoginState {
}else if isLogin {
if err = c.RecvPkt(0x00, &lp); err != nil {
ploger.Debugf("Read login start packet error: %v", err)
return
Expand Down Expand Up @@ -106,10 +108,10 @@ func (s *Server)handle(c *liter.Conn, cfg *Config){
var conn *liter.Conn
if conn, err = liter.Dial(svr.Target); err != nil {
ploger.Errorf("Cannot dial to %q: %v", svr.Target, err)
if hp.NextState == liter.NextPingState {
if isPing {
ploger.Debugf("Handle ping connection for server %q", svr.Id)
handleServerStatus(ploger, c, "Closed", svr.MotdFailed)
}else if hp.NextState == liter.NextLoginState {
}else if isLogin {
c.SendPkt(0x00, &liter.DisconnectPkt{
Reason: liter.NewChatFromString("Cannot connect to the subserver"),
})
Expand All @@ -123,7 +125,7 @@ func (s *Server)handle(c *liter.Conn, cfg *Config){
}
ploger.Tracef("Handshake sent successed")

if hp.NextState == liter.NextLoginState {
if isLogin {
if err = conn.SendPkt(0x00, lp); err != nil {
ploger.Errorf("Connection login error: %v", err)
return
Expand Down
6 changes: 5 additions & 1 deletion cmds/liter-server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func (m *FlatMemory[T])Count()(int){
return len(m.data) - len(m.free)
}

func (m *FlatMemory[T])LastModified()(time.Time){
return m.lastEdit
}

// Clear will remove all data stored
func (m *FlatMemory[T])Clear(){
m.mux.Lock()
Expand All @@ -94,7 +98,7 @@ func (m *FlatMemory[T])ForEach(cb func(i int, v T)){
j := sort.Search(len(m.free), func(j int)(bool){
return m.free[j] <= i
})
if j < len(m.free) && m.free[j] == i {
if j >= len(m.free) || m.free[j] != i {
cb(i, v)
}
}
Expand Down

0 comments on commit 7b2578d

Please sign in to comment.