server initial commit
This commit is contained in:
75
core/SocketContext.go
Normal file
75
core/SocketContext.go
Normal file
@ -0,0 +1,75 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"anvil/shared"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
)
|
||||
|
||||
func (st *SocketContext) Hello() shared.RPCReturnType {
|
||||
st.gs.Logger.Println("hello from socket client.")
|
||||
return shared.MakeRPCReturn("hello from socket server, too.", nil)
|
||||
}
|
||||
|
||||
func (st *SocketContext) IsCompatible(Type int32, Version string) bool {
|
||||
st.lock.Lock()
|
||||
defer st.lock.Unlock()
|
||||
|
||||
return st.Type == Type && st.Version == Version
|
||||
}
|
||||
|
||||
func (st *SocketContext) GetUrlChekedIn() string {
|
||||
st.lock.Lock()
|
||||
defer st.lock.Unlock()
|
||||
|
||||
if len(st.Ip) != 0 {
|
||||
return fmt.Sprintf("%s:%d", st.Ip.String(), st.Port)
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func (st *SocketContext) GetPriority() float32 {
|
||||
return rand.Float32()
|
||||
}
|
||||
|
||||
func (st *SocketContext) DSCheckIn(DsType int32, inSocketPort int32, Version string) shared.RPCReturnType {
|
||||
st.lock.Lock()
|
||||
defer st.lock.Unlock()
|
||||
|
||||
addr := st.connection.RemoteAddr().String()
|
||||
|
||||
st.gs.Logger.Printf("remoteaddr %s : %d", addr, inSocketPort)
|
||||
|
||||
ip, _, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
st.gs.Logger.Printf("userip: %q is not IP:port", addr)
|
||||
return shared.MakeRPCReturn(nil, errors.New("IP invalid"))
|
||||
}
|
||||
|
||||
userIP := net.ParseIP(ip)
|
||||
if userIP == nil {
|
||||
st.gs.Logger.Printf("userip: %q is not IP:port", addr)
|
||||
return shared.MakeRPCReturn(nil, errors.New("IP invalid"))
|
||||
}
|
||||
|
||||
if userIP.IsLoopback() {
|
||||
if tmpIp, err := externalIP(); err == nil {
|
||||
userIP = tmpIp
|
||||
} else {
|
||||
return shared.MakeRPCReturn(nil, errors.New("IP invalid"))
|
||||
}
|
||||
}
|
||||
|
||||
// st.gs.LobbyCheckIn(userIP, inSocketPort)
|
||||
st.Ip = userIP
|
||||
st.Port = inSocketPort
|
||||
st.Type = DsType
|
||||
st.Version = Version
|
||||
|
||||
st.gs.Logger.Printf("type : %d, id : %s, port : %d, version : %s", DsType, userIP, inSocketPort, st.Version)
|
||||
|
||||
return shared.MakeRPCReturn("ds checked in.", nil)
|
||||
}
|
||||
Reference in New Issue
Block a user