mirror of
https://github.com/CPunch/gopenfusion.git
synced 2024-12-04 22:46:32 +00:00
Compare commits
6 Commits
23170093ee
...
0a28dbcc3e
Author | SHA1 | Date | |
---|---|---|---|
0a28dbcc3e | |||
1a6de671e5 | |||
261ea6505f | |||
556878544d | |||
bfcbe6d3d6 | |||
e5a9ed1481 |
9
cnet/protocol/time.go
Normal file
9
cnet/protocol/time.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package protocol
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetTime() uint64 {
|
||||||
|
return uint64(time.Now().UnixMilli())
|
||||||
|
}
|
@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
"github.com/CPunch/gopenfusion/cnet"
|
"github.com/CPunch/gopenfusion/cnet"
|
||||||
"github.com/CPunch/gopenfusion/cnet/protocol"
|
"github.com/CPunch/gopenfusion/cnet/protocol"
|
||||||
"github.com/CPunch/gopenfusion/util"
|
"github.com/CPunch/gopenfusion/internal/testutil"
|
||||||
"github.com/matryer/is"
|
"github.com/matryer/is"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ func TestService(t *testing.T) {
|
|||||||
// shutdown service when test is done
|
// shutdown service when test is done
|
||||||
defer func() {
|
defer func() {
|
||||||
cancel()
|
cancel()
|
||||||
is.True(util.SelectWithTimeout(srvc.Stopped(), timeout)) // wait for service to stop with timeout
|
is.True(testutil.SelectWithTimeout(srvc.Stopped(), timeout)) // wait for service to stop with timeout
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// our dummy packet handler
|
// our dummy packet handler
|
||||||
@ -67,8 +67,8 @@ func TestService(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// run service
|
// run service
|
||||||
go func() { is.NoErr(srvc.Start()) }() // srvc.Start error
|
go func() { is.NoErr(srvc.Start()) }() // srvc.Start error
|
||||||
is.True(util.SelectWithTimeout(srvc.Started(), timeout)) // wait for service to start with timeout
|
is.True(testutil.SelectWithTimeout(srvc.Started(), timeout)) // wait for service to start with timeout
|
||||||
|
|
||||||
wg.Add(maxDummyPeers * 2) // 2 wg.Done() per peer for receiving packets
|
wg.Add(maxDummyPeers * 2) // 2 wg.Done() per peer for receiving packets
|
||||||
for i := 0; i < maxDummyPeers; i++ {
|
for i := 0; i < maxDummyPeers; i++ {
|
||||||
@ -93,5 +93,5 @@ func TestService(t *testing.T) {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
is.True(util.WaitWithTimeout(&wg, timeout)) // wait for all dummy peers to be done with timeout
|
is.True(testutil.WaitWithTimeout(&wg, timeout)) // wait for all dummy peers to be done with timeout
|
||||||
}
|
}
|
||||||
|
116
internal/testutil/helpers.go
Normal file
116
internal/testutil/helpers.go
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
package testutil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/CPunch/gopenfusion/cnet"
|
||||||
|
"github.com/CPunch/gopenfusion/cnet/protocol"
|
||||||
|
"github.com/CPunch/gopenfusion/internal/db"
|
||||||
|
"github.com/CPunch/gopenfusion/internal/redis"
|
||||||
|
"github.com/alicebob/miniredis/v2"
|
||||||
|
"github.com/bitcomplete/sqltestutil"
|
||||||
|
"github.com/matryer/is"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DummyPeer struct {
|
||||||
|
Recv chan *cnet.PacketEvent
|
||||||
|
Peer *cnet.Peer
|
||||||
|
}
|
||||||
|
|
||||||
|
// MakeDummyPeer creates a new dummy peer and returns it
|
||||||
|
func MakeDummyPeer(ctx context.Context, is *is.I, port int) *DummyPeer {
|
||||||
|
conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port))
|
||||||
|
is.NoErr(err)
|
||||||
|
|
||||||
|
recv := make(chan *cnet.PacketEvent)
|
||||||
|
peer := cnet.NewPeer(ctx, conn)
|
||||||
|
go func() {
|
||||||
|
peer.Handler(recv)
|
||||||
|
}()
|
||||||
|
|
||||||
|
return &DummyPeer{Recv: recv, Peer: peer}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendAndRecv sends a packet (sID & out), waits for the expected response (rID) and decodes it into in
|
||||||
|
func (dp *DummyPeer) SendAndRecv(is *is.I, sID, rID uint32, out, in interface{}) {
|
||||||
|
// send out packet
|
||||||
|
err := dp.Peer.Send(sID, out)
|
||||||
|
is.NoErr(err) // peer.Send() should not return an error
|
||||||
|
|
||||||
|
// receive response
|
||||||
|
evnt := <-dp.Recv
|
||||||
|
defer protocol.PutBuffer(evnt.Pkt)
|
||||||
|
|
||||||
|
is.Equal(evnt.PktID, rID) // should receive expected type
|
||||||
|
is.NoErr(protocol.NewPacket(evnt.Pkt).Decode(in)) // packet.Decode() should not return an error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Kill closes the peer's connection
|
||||||
|
func (dp *DummyPeer) Kill() {
|
||||||
|
dp.Peer.Kill()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetupEnvironment spawns a postgres container and returns a db and redis handler
|
||||||
|
// along with a cleanup function
|
||||||
|
func SetupEnvironment(ctx context.Context) (*db.DBHandler, *redis.RedisHandler, func()) {
|
||||||
|
// spawn postgres container
|
||||||
|
psql, err := sqltestutil.StartPostgresContainer(ctx, "15")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// open db handler
|
||||||
|
testDB, err := db.OpenFromConnectionString("postgres", psql.ConnectionString()+"?sslmode=disable")
|
||||||
|
if err != nil {
|
||||||
|
psql.Shutdown(ctx)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = testDB.Setup(); err != nil {
|
||||||
|
psql.Shutdown(ctx)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// start miniredis
|
||||||
|
r, err := miniredis.Run()
|
||||||
|
if err != nil {
|
||||||
|
psql.Shutdown(ctx)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// open redis handler
|
||||||
|
rh, err := redis.OpenRedis(r.Addr())
|
||||||
|
if err != nil {
|
||||||
|
psql.Shutdown(ctx)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return testDB, rh, func() {
|
||||||
|
psql.Shutdown(ctx)
|
||||||
|
rh.Close()
|
||||||
|
r.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func SelectWithTimeout(ch <-chan struct{}, timeout time.Duration) bool {
|
||||||
|
select {
|
||||||
|
case <-ch:
|
||||||
|
return true
|
||||||
|
case <-time.After(timeout):
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WaitWithTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
defer close(done)
|
||||||
|
wg.Wait()
|
||||||
|
}()
|
||||||
|
|
||||||
|
return SelectWithTimeout(done, timeout)
|
||||||
|
}
|
35
internal/testutil/testutil_test.go
Normal file
35
internal/testutil/testutil_test.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package testutil_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/CPunch/gopenfusion/internal/testutil"
|
||||||
|
"github.com/matryer/is"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestWaitWithTimeout(t *testing.T) {
|
||||||
|
is := is.New(t)
|
||||||
|
wg := &sync.WaitGroup{}
|
||||||
|
go func() {
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
|
||||||
|
wg.Add(1)
|
||||||
|
is.True(!testutil.WaitWithTimeout(wg, 500*time.Millisecond)) // timeout should occur
|
||||||
|
is.True(testutil.WaitWithTimeout(wg, 750*time.Millisecond)) // timeout shouldn't occur
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSelectWithTimeout(t *testing.T) {
|
||||||
|
is := is.New(t)
|
||||||
|
ch := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
close(ch)
|
||||||
|
}()
|
||||||
|
|
||||||
|
is.True(!testutil.SelectWithTimeout(ch, 500*time.Millisecond)) // timeout should occur
|
||||||
|
is.True(testutil.SelectWithTimeout(ch, 750*time.Millisecond)) // timeout shouldn't occur
|
||||||
|
}
|
@ -12,7 +12,6 @@ import (
|
|||||||
"github.com/CPunch/gopenfusion/internal/config"
|
"github.com/CPunch/gopenfusion/internal/config"
|
||||||
"github.com/CPunch/gopenfusion/internal/db"
|
"github.com/CPunch/gopenfusion/internal/db"
|
||||||
"github.com/CPunch/gopenfusion/internal/redis"
|
"github.com/CPunch/gopenfusion/internal/redis"
|
||||||
"github.com/CPunch/gopenfusion/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -34,7 +33,7 @@ func (server *LoginServer) AcceptLogin(peer *cnet.Peer, SzID string, IClientVerC
|
|||||||
ISlotNum: ISlotNum,
|
ISlotNum: ISlotNum,
|
||||||
IPaymentFlag: 1,
|
IPaymentFlag: 1,
|
||||||
IOpenBetaFlag: 0,
|
IOpenBetaFlag: 0,
|
||||||
UiSvrTime: util.GetTime(),
|
UiSvrTime: protocol.GetTime(),
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := peer.Send(protocol.P_LS2CL_REP_LOGIN_SUCC, resp); err != nil {
|
if err := peer.Send(protocol.P_LS2CL_REP_LOGIN_SUCC, resp); err != nil {
|
||||||
|
@ -3,8 +3,6 @@ package login_test
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -12,9 +10,8 @@ import (
|
|||||||
"github.com/CPunch/gopenfusion/cnet/protocol"
|
"github.com/CPunch/gopenfusion/cnet/protocol"
|
||||||
"github.com/CPunch/gopenfusion/internal/db"
|
"github.com/CPunch/gopenfusion/internal/db"
|
||||||
"github.com/CPunch/gopenfusion/internal/redis"
|
"github.com/CPunch/gopenfusion/internal/redis"
|
||||||
|
"github.com/CPunch/gopenfusion/internal/testutil"
|
||||||
"github.com/CPunch/gopenfusion/login"
|
"github.com/CPunch/gopenfusion/login"
|
||||||
"github.com/alicebob/miniredis/v2"
|
|
||||||
"github.com/bitcomplete/sqltestutil"
|
|
||||||
"github.com/matryer/is"
|
"github.com/matryer/is"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -44,65 +41,16 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func makeDummyPeer(ctx context.Context, is *is.I, recv chan<- *cnet.PacketEvent) *cnet.Peer {
|
|
||||||
conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", loginPort))
|
|
||||||
is.NoErr(err)
|
|
||||||
|
|
||||||
peer := cnet.NewPeer(ctx, conn)
|
|
||||||
go func() {
|
|
||||||
peer.Handler(recv)
|
|
||||||
}()
|
|
||||||
|
|
||||||
return peer
|
|
||||||
}
|
|
||||||
|
|
||||||
func sendAndRecv(peer *cnet.Peer, recv chan *cnet.PacketEvent, is *is.I, sID, rID uint32, out, in interface{}) {
|
|
||||||
// send out packet
|
|
||||||
err := peer.Send(sID, out)
|
|
||||||
is.NoErr(err) // peer.Send() should not return an error
|
|
||||||
|
|
||||||
// receive response
|
|
||||||
evnt := <-recv
|
|
||||||
defer protocol.PutBuffer(evnt.Pkt)
|
|
||||||
|
|
||||||
is.Equal(evnt.PktID, rID) // should receive expected type
|
|
||||||
is.NoErr(protocol.NewPacket(evnt.Pkt).Decode(in)) // packet.Decode() should not return an error
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// spawn postgres container
|
// setup environment
|
||||||
psql, err := sqltestutil.StartPostgresContainer(ctx, "15")
|
var closer func()
|
||||||
if err != nil {
|
testDB, rh, closer = testutil.SetupEnvironment(ctx)
|
||||||
panic(err)
|
defer closer()
|
||||||
}
|
|
||||||
|
|
||||||
// open db handler
|
|
||||||
testDB, err = db.OpenFromConnectionString("postgres", psql.ConnectionString()+"?sslmode=disable")
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = testDB.Setup(); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// start miniredis
|
|
||||||
r, err := miniredis.Run()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer r.Close()
|
|
||||||
|
|
||||||
// open redis handler
|
|
||||||
rh, err = redis.OpenRedis(r.Addr())
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer rh.Close()
|
|
||||||
|
|
||||||
|
var err error
|
||||||
loginPort, err = cnet.RandomPort()
|
loginPort, err = cnet.RandomPort()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -134,13 +82,12 @@ func TestLoginSuccSequence(t *testing.T) {
|
|||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
recv := make(chan *cnet.PacketEvent)
|
dummy := testutil.MakeDummyPeer(ctx, is, loginPort)
|
||||||
peer := makeDummyPeer(ctx, is, recv)
|
defer dummy.Kill()
|
||||||
defer peer.Kill()
|
|
||||||
|
|
||||||
// send login request (this should create an account)
|
// send login request (this should create an account)
|
||||||
var resp protocol.SP_LS2CL_REP_LOGIN_SUCC
|
var resp protocol.SP_LS2CL_REP_LOGIN_SUCC
|
||||||
sendAndRecv(peer, recv, is, protocol.P_CL2LS_REQ_LOGIN, protocol.P_LS2CL_REP_LOGIN_SUCC,
|
dummy.SendAndRecv(is, protocol.P_CL2LS_REQ_LOGIN, protocol.P_LS2CL_REP_LOGIN_SUCC,
|
||||||
protocol.SP_CL2LS_REQ_LOGIN{
|
protocol.SP_CL2LS_REQ_LOGIN{
|
||||||
SzID: "testLoginSequence",
|
SzID: "testLoginSequence",
|
||||||
SzPassword: "test",
|
SzPassword: "test",
|
||||||
@ -161,13 +108,12 @@ func TestLoginFailSequence(t *testing.T) {
|
|||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
recv := make(chan *cnet.PacketEvent)
|
dummy := testutil.MakeDummyPeer(ctx, is, loginPort)
|
||||||
peer := makeDummyPeer(ctx, is, recv)
|
defer dummy.Kill()
|
||||||
defer peer.Kill()
|
|
||||||
|
|
||||||
// send login request (this should not create an account)
|
// send login request (this should not create an account)
|
||||||
var resp protocol.SP_LS2CL_REP_LOGIN_FAIL
|
var resp protocol.SP_LS2CL_REP_LOGIN_FAIL
|
||||||
sendAndRecv(peer, recv, is, protocol.P_CL2LS_REQ_LOGIN, protocol.P_LS2CL_REP_LOGIN_FAIL,
|
dummy.SendAndRecv(is, protocol.P_CL2LS_REQ_LOGIN, protocol.P_LS2CL_REP_LOGIN_FAIL,
|
||||||
protocol.SP_CL2LS_REQ_LOGIN{
|
protocol.SP_CL2LS_REQ_LOGIN{
|
||||||
SzID: "",
|
SzID: "",
|
||||||
SzPassword: "",
|
SzPassword: "",
|
||||||
@ -184,13 +130,12 @@ func TestCharacterSequence(t *testing.T) {
|
|||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
recv := make(chan *cnet.PacketEvent)
|
dummy := testutil.MakeDummyPeer(ctx, is, loginPort)
|
||||||
peer := makeDummyPeer(ctx, is, recv)
|
defer dummy.Kill()
|
||||||
defer peer.Kill()
|
|
||||||
|
|
||||||
// send login request (this should create an account)
|
// send login request (this should create an account)
|
||||||
var resp protocol.SP_LS2CL_REP_LOGIN_SUCC
|
var resp protocol.SP_LS2CL_REP_LOGIN_SUCC
|
||||||
sendAndRecv(peer, recv, is, protocol.P_CL2LS_REQ_LOGIN, protocol.P_LS2CL_REP_LOGIN_SUCC,
|
dummy.SendAndRecv(is, protocol.P_CL2LS_REQ_LOGIN, protocol.P_LS2CL_REP_LOGIN_SUCC,
|
||||||
protocol.SP_CL2LS_REQ_LOGIN{
|
protocol.SP_CL2LS_REQ_LOGIN{
|
||||||
SzID: "testCharacterSequence",
|
SzID: "testCharacterSequence",
|
||||||
SzPassword: "test",
|
SzPassword: "test",
|
||||||
@ -201,12 +146,12 @@ func TestCharacterSequence(t *testing.T) {
|
|||||||
is.Equal(resp.ICharCount, int8(0)) // should have 0 characters
|
is.Equal(resp.ICharCount, int8(0)) // should have 0 characters
|
||||||
|
|
||||||
// perform key swap
|
// perform key swap
|
||||||
peer.E_key = protocol.CreateNewKey(
|
dummy.Peer.E_key = protocol.CreateNewKey(
|
||||||
resp.UiSvrTime,
|
resp.UiSvrTime,
|
||||||
uint64(resp.ICharCount+1),
|
uint64(resp.ICharCount+1),
|
||||||
uint64(resp.ISlotNum+1),
|
uint64(resp.ISlotNum+1),
|
||||||
)
|
)
|
||||||
peer.FE_key = protocol.CreateNewKey(
|
dummy.Peer.FE_key = protocol.CreateNewKey(
|
||||||
binary.LittleEndian.Uint64([]byte(protocol.DEFAULT_KEY)),
|
binary.LittleEndian.Uint64([]byte(protocol.DEFAULT_KEY)),
|
||||||
0,
|
0,
|
||||||
1,
|
1,
|
||||||
@ -214,7 +159,7 @@ func TestCharacterSequence(t *testing.T) {
|
|||||||
|
|
||||||
// send character name check request
|
// send character name check request
|
||||||
var charResp protocol.SP_LS2CL_REP_SAVE_CHAR_NAME_SUCC
|
var charResp protocol.SP_LS2CL_REP_SAVE_CHAR_NAME_SUCC
|
||||||
sendAndRecv(peer, recv, is, protocol.P_CL2LS_REQ_SAVE_CHAR_NAME, protocol.P_LS2CL_REP_SAVE_CHAR_NAME_SUCC,
|
dummy.SendAndRecv(is, protocol.P_CL2LS_REQ_SAVE_CHAR_NAME, protocol.P_LS2CL_REP_SAVE_CHAR_NAME_SUCC,
|
||||||
protocol.SP_CL2LS_REQ_SAVE_CHAR_NAME{
|
protocol.SP_CL2LS_REQ_SAVE_CHAR_NAME{
|
||||||
ISlotNum: 1,
|
ISlotNum: 1,
|
||||||
IGender: 1,
|
IGender: 1,
|
||||||
@ -235,7 +180,7 @@ func TestCharacterSequence(t *testing.T) {
|
|||||||
charCreate := testCharCreate
|
charCreate := testCharCreate
|
||||||
charCreate.PCStyle.IPC_UID = charResp.IPC_UID
|
charCreate.PCStyle.IPC_UID = charResp.IPC_UID
|
||||||
var charCreateResp protocol.SP_LS2CL_REP_CHAR_CREATE_SUCC
|
var charCreateResp protocol.SP_LS2CL_REP_CHAR_CREATE_SUCC
|
||||||
sendAndRecv(peer, recv, is, protocol.P_CL2LS_REQ_CHAR_CREATE, protocol.P_LS2CL_REP_CHAR_CREATE_SUCC,
|
dummy.SendAndRecv(is, protocol.P_CL2LS_REQ_CHAR_CREATE, protocol.P_LS2CL_REP_CHAR_CREATE_SUCC,
|
||||||
charCreate, &charCreateResp)
|
charCreate, &charCreateResp)
|
||||||
|
|
||||||
// verify response
|
// verify response
|
||||||
|
@ -8,7 +8,6 @@ import (
|
|||||||
"github.com/CPunch/gopenfusion/cnet/protocol"
|
"github.com/CPunch/gopenfusion/cnet/protocol"
|
||||||
"github.com/CPunch/gopenfusion/internal/redis"
|
"github.com/CPunch/gopenfusion/internal/redis"
|
||||||
"github.com/CPunch/gopenfusion/shard/entity"
|
"github.com/CPunch/gopenfusion/shard/entity"
|
||||||
"github.com/CPunch/gopenfusion/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (server *ShardServer) attachPlayer(peer *cnet.Peer, meta redis.LoginMetadata) (*entity.Player, error) {
|
func (server *ShardServer) attachPlayer(peer *cnet.Peer, meta redis.LoginMetadata) (*entity.Player, error) {
|
||||||
@ -51,7 +50,7 @@ func (server *ShardServer) RequestEnter(peer *cnet.Peer, pkt protocol.Packet) er
|
|||||||
resp := &protocol.SP_FE2CL_REP_PC_ENTER_SUCC{
|
resp := &protocol.SP_FE2CL_REP_PC_ENTER_SUCC{
|
||||||
IID: int32(plr.PlayerID),
|
IID: int32(plr.PlayerID),
|
||||||
PCLoadData2CL: plr.ToPCLoadData2CL(),
|
PCLoadData2CL: plr.ToPCLoadData2CL(),
|
||||||
UiSvrTime: util.GetTime(),
|
UiSvrTime: protocol.GetTime(),
|
||||||
}
|
}
|
||||||
|
|
||||||
// setup peer
|
// setup peer
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
"github.com/CPunch/gopenfusion/cnet"
|
"github.com/CPunch/gopenfusion/cnet"
|
||||||
"github.com/CPunch/gopenfusion/cnet/protocol"
|
"github.com/CPunch/gopenfusion/cnet/protocol"
|
||||||
"github.com/CPunch/gopenfusion/shard/entity"
|
"github.com/CPunch/gopenfusion/shard/entity"
|
||||||
"github.com/CPunch/gopenfusion/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (server *ShardServer) updatePlayerPosition(plr *entity.Player, X, Y, Z, Angle int) {
|
func (server *ShardServer) updatePlayerPosition(plr *entity.Player, X, Y, Z, Angle int) {
|
||||||
@ -41,7 +40,7 @@ func (server *ShardServer) playerMove(peer *cnet.Peer, pkt protocol.Packet) erro
|
|||||||
CKeyValue: move.CKeyValue,
|
CKeyValue: move.CKeyValue,
|
||||||
ISpeed: move.ISpeed,
|
ISpeed: move.ISpeed,
|
||||||
IID: int32(plr.PlayerID),
|
IID: int32(plr.PlayerID),
|
||||||
ISvrTime: util.GetTime(),
|
ISvrTime: protocol.GetTime(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,7 +62,7 @@ func (server *ShardServer) playerStop(peer *cnet.Peer, pkt protocol.Packet) erro
|
|||||||
IY: stop.IY,
|
IY: stop.IY,
|
||||||
IZ: stop.IZ,
|
IZ: stop.IZ,
|
||||||
IID: int32(plr.PlayerID),
|
IID: int32(plr.PlayerID),
|
||||||
ISvrTime: util.GetTime(),
|
ISvrTime: protocol.GetTime(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,6 +90,6 @@ func (server *ShardServer) playerJump(peer *cnet.Peer, pkt protocol.Packet) erro
|
|||||||
CKeyValue: jump.CKeyValue,
|
CKeyValue: jump.CKeyValue,
|
||||||
ISpeed: jump.ISpeed,
|
ISpeed: jump.ISpeed,
|
||||||
IID: int32(plr.PlayerID),
|
IID: int32(plr.PlayerID),
|
||||||
ISvrTime: util.GetTime(),
|
ISvrTime: protocol.GetTime(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -50,9 +50,9 @@ func NewShardServer(ctx context.Context, dbHndlr *db.DBHandler, redisHndlr *redi
|
|||||||
return server, nil
|
return server, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *ShardServer) Start() {
|
func (server *ShardServer) Start() error {
|
||||||
server.LoadNPCs()
|
server.LoadNPCs()
|
||||||
server.service.Start()
|
return server.service.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *ShardServer) onDisconnect(peer *cnet.Peer) {
|
func (server *ShardServer) onDisconnect(peer *cnet.Peer) {
|
||||||
@ -66,3 +66,7 @@ func (server *ShardServer) onDisconnect(peer *cnet.Peer) {
|
|||||||
func (server *ShardServer) onConnect(peer *cnet.Peer) {
|
func (server *ShardServer) onConnect(peer *cnet.Peer) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (server *ShardServer) Service() *cnet.Service {
|
||||||
|
return server.service
|
||||||
|
}
|
||||||
|
29
util/util.go
29
util/util.go
@ -1,29 +0,0 @@
|
|||||||
package util
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func GetTime() uint64 {
|
|
||||||
return uint64(time.Now().UnixMilli())
|
|
||||||
}
|
|
||||||
|
|
||||||
func SelectWithTimeout(ch <-chan struct{}, timeout time.Duration) bool {
|
|
||||||
select {
|
|
||||||
case <-ch:
|
|
||||||
return true
|
|
||||||
case <-time.After(timeout):
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func WaitWithTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
|
|
||||||
done := make(chan struct{})
|
|
||||||
go func() {
|
|
||||||
defer close(done)
|
|
||||||
wg.Wait()
|
|
||||||
}()
|
|
||||||
|
|
||||||
return SelectWithTimeout(done, timeout)
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
package util_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/CPunch/gopenfusion/util"
|
|
||||||
"github.com/matryer/is"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestWaitWithTimeout(t *testing.T) {
|
|
||||||
is := is.New(t)
|
|
||||||
wg := &sync.WaitGroup{}
|
|
||||||
go func() {
|
|
||||||
time.Sleep(1 * time.Second)
|
|
||||||
wg.Done()
|
|
||||||
}()
|
|
||||||
|
|
||||||
wg.Add(1)
|
|
||||||
is.True(!util.WaitWithTimeout(wg, 500*time.Millisecond)) // timeout should occur
|
|
||||||
is.True(util.WaitWithTimeout(wg, 750*time.Millisecond)) // timeout shouldn't occur
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSelectWithTimeout(t *testing.T) {
|
|
||||||
is := is.New(t)
|
|
||||||
ch := make(chan struct{})
|
|
||||||
go func() {
|
|
||||||
time.Sleep(1 * time.Second)
|
|
||||||
close(ch)
|
|
||||||
}()
|
|
||||||
|
|
||||||
is.True(!util.SelectWithTimeout(ch, 500*time.Millisecond)) // timeout should occur
|
|
||||||
is.True(util.SelectWithTimeout(ch, 750*time.Millisecond)) // timeout shouldn't occur
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user