コード例 #1
0
ファイル: echo_client.go プロジェクト: youxidev/GoGameServer
func main() {
	flag.Parse()

	session, err := link.Connect("tcp", *addr, packet.New(
		binary.SplitByUint16BE, 1024, 1024, 1024,
	))
	if err != nil {
		panic(err)
	}

	go func() {
		var msg packet.RAW
		for {
			if err := session.Receive(&msg); err != nil {
				break
			}
			fmt.Printf("%s\n", msg)
		}
	}()

	for {
		var msg string
		if _, err := fmt.Scanf("%s\n", &msg); err != nil {
			break
		}
		if err = session.Send(packet.RAW(msg)); err != nil {
			break
		}
	}

	session.Close()
	println("bye")
}
コード例 #2
0
func (flink *frontendLink) dispathMsg(clientId uint64, data []byte) {
	flink.clientMutex.RLock()
	defer flink.clientMutex.RUnlock()

	if client, exists := flink.clients[clientId]; exists {
		client.AsyncSend(packet.RAW(data))
	}
}
コード例 #3
0
func (c *TransferProxyConn) Send(data interface{}) error {
	msg := data.(packet.RAW)
	result := make([]byte, 8+len(msg))
	copy(result[:2], msg[:2])
	binary.PutUint64LE(result[2:10], c.id)
	copy(result[10:], msg[2:])
	c.proxySession.Send(packet.RAW(result))
	return nil
}
コード例 #4
0
func (flink *frontendLink) broadcast(clientIds []uint64, data []byte) {
	flink.clientMutex.RLock()
	defer flink.clientMutex.RUnlock()

	for i := 0; i < len(clientIds); i++ {
		if client, exists := flink.clients[clientIds[i]]; exists {
			client.AsyncSend(packet.RAW(data))
		}
	}
}
コード例 #5
0
ファイル: gw_test.go プロジェクト: youxidev/GoGameServer
func createGetUserInfoBytes(userId uint64) packet.RAW {
	sendMsg := &gameProto.GetUserInfoC2S{
		UserID: proto.Uint64(userId),
	}
	msgBody, _ := proto.Marshal(sendMsg)

	msg1 := make([]byte, 2+len(msgBody))
	binary.PutUint16LE(msg1, gameProto.ID_GetUserInfoC2S)
	copy(msg1[2:], msgBody)
	return packet.RAW(msg1)
}
コード例 #6
0
ファイル: gw_test.go プロジェクト: youxidev/GoGameServer
func createLoginBytes(userName string) packet.RAW {
	sendMsg := &gameProto.UserLoginC2S{
		UserName: proto.String(userName),
	}
	msgBody, _ := proto.Marshal(sendMsg)

	msg1 := make([]byte, 2+len(msgBody))
	binary.PutUint16LE(msg1, gameProto.ID_UserLoginC2S)
	copy(msg1[2:], msgBody)
	return packet.RAW(msg1)
}
コード例 #7
0
ファイル: unit_test.go プロジェクト: youxidev/GoGameServer
func Test_Gateway_Complex(t *testing.T) {
	backend := StartTestBackend(t, func(session *link.Session) {
		var msg packet.RAW
		for {
			if err := session.Receive(&msg); err != nil {
				break
			}
			if err := session.Send(msg); err != nil {
				break
			}
		}
	})

	gateway := StartTestGateway(t, backend.Listener().Addr().String())
	gatewayAddr := gateway.server.Listener().Addr().String()

	var wg sync.WaitGroup
	for i := 0; i < 20; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()

			client, err := link.Connect("tcp", gatewayAddr, protocol)
			unitest.NotError(t, err)

			for j := 0; j < 500; j++ {
				msg1 := RandBytes(1024)
				err1 := client.Send(packet.RAW(msg1))
				unitest.NotError(t, err1)

				var msg2 packet.RAW
				err2 := client.Receive(&msg2)
				unitest.NotError(t, err2)

				if bytes.Equal(msg1, msg2) == false {
					t.Log(j, msg1, msg2)
					t.Fail()
				}
				unitest.Pass(t, bytes.Equal(msg1, msg2))
			}

			client.Close()
		}()
	}
	wg.Wait()

	gateway.Stop()
	backend.Stop()

	time.Sleep(time.Second * 2)
	MakeSureSessionGoroutineExit(t)
}
コード例 #8
0
// This is broadcast server demo work with the echo_client.
// usage:
//     go run echo_broadcast.go
func main() {
	flag.Parse()

	protocol := packet.New(
		binary.SplitByUint16BE, 1024, 1024, 1024,
	)

	server, err := link.Serve("tcp", *addr, protocol)
	if err != nil {
		panic(err)
	}
	println("server start:", server.Listener().Addr().String())

	channel := link.NewChannel(link.DefaultBroadcast)
	go func() {
		for range time.Tick(time.Second * 2) {
			now := time.Now().Format("2006-01-02 15:04:05")
			channel.Broadcast(packet.RAW("from channel: " + now))
		}
	}()

	server.Serve(func(session *link.Session) {
		addr := session.Conn().RemoteAddr().String()
		println("client", addr, "connected")
		channel.Join(session, nil)

		for {
			var msg packet.RAW
			if err := session.Receive(&msg); err != nil {
				break
			}
			println(addr, "say:", string(msg))
			channel.Broadcast(packet.RAW("from " + addr + ": " + string(msg)))
		}

		println("client", addr, "closed")
		channel.Exit(session)
	})
}
コード例 #9
0
func client(initWait *sync.WaitGroup, conn *CountConn, startChan chan int, timeout time.Time, msg packet.RAW) {
	client := link.NewSession(0, packet.NewConn(conn, packet.New(
		binary.SplitByUint16BE, 1024, 1024, 1024,
	)))

	var wg sync.WaitGroup

	wg.Add(1)
	go func() {
		defer wg.Done()
		initWait.Done()
		<-startChan

		for {
			outMsg := msg
			if *randsize {
				outMsg = packet.RAW(make([]byte, rand.Intn(*messageSize)))
			}
			if err := client.Send(outMsg); err != nil {
				if timeout.After(time.Now()) {
					println("send error:", err.Error())
				}
				break
			}
			atomic.AddUint32(&conn.SendCount, 1)
		}
	}()

	wg.Add(1)
	go func() {
		defer wg.Done()
		initWait.Done()
		<-startChan

		var inMsg packet.RAW
		for {
			if err := client.Receive(&inMsg); err != nil {
				if timeout.After(time.Now()) {
					println("recv error:", err.Error())
				}
				break
			}
			atomic.AddUint32(&conn.RecvCount, 1)
		}
	}()

	wg.Wait()
}
コード例 #10
0
// This is an benchmark tool work with the echo_server.
//
// Start echo_server with 'bench' flag
//     go run echo_server.go -bench
//
// Start benchmark with echo_server address
//     go run echo_benchmark.go
//     go run echo_benchmark.go -num=100
//     go run echo_benchmark.go -size=1024
//     go run echo_benchmark.go -time=20
//     go run echo_benchmark.go -addr="127.0.0.1:10010"
func main() {
	flag.Parse()

	if MultiProcess() {
		return
	}

	var (
		msg       = packet.RAW(make([]byte, *messageSize))
		timeout   = time.Now().Add(time.Second * time.Duration(*runTime))
		initWait  = new(sync.WaitGroup)
		startChan = make(chan int)
		conns     = make([]*CountConn, 0, *clientNum)
	)

	for i := 0; i < *clientNum; i++ {
		conn, err := net.DialTimeout("tcp", *serverAddr, time.Second*3)
		if err != nil {
			panic(err)
		}

		countConn := &CountConn{Conn: conn}
		conns = append(conns, countConn)

		initWait.Add(2)
		go client(initWait, countConn, startChan, timeout, msg)
	}
	initWait.Wait()
	close(startChan)

	time.Sleep(time.Second * time.Duration(*runTime))
	var sum CountConn
	for i := 0; i < *clientNum; i++ {
		conn := conns[i]
		conn.Conn.Close()
		sum.SendCount += conn.SendCount
		sum.RecvCount += conn.RecvCount
		sum.ReadCount += conn.ReadCount
		sum.WriteCount += conn.WriteCount
	}
	fmt.Printf(OutputFormat, sum.SendCount, sum.RecvCount, sum.ReadCount, sum.WriteCount)
}
コード例 #11
0
ファイル: dbProto.go プロジェクト: youxidev/GoGameServer
//发送消息
func Send(msgBody []byte, session *link.Session) {
	session.Send(packet.RAW(msgBody))
}
コード例 #12
0
ファイル: unit_test.go プロジェクト: youxidev/GoGameServer
func Test_Broadcast(t *testing.T) {
	var (
		clientNum     = 20
		channel       = link.NewChannel(Broadcast{})
		broadcastWait sync.WaitGroup
		clientWait    sync.WaitGroup
	)

	clientWait.Add(clientNum)
	backend := StartTestBackend(t, func(session *link.Session) {
		channel.Join(session)
		clientWait.Done()
		for {
			var msg packet.RAW
			if err := session.Receive(&msg); err != nil {
				break
			}
			broadcastWait.Done()
		}
	})

	go func() {
		clientWait.Wait()
		for i := 0; i < 10000; i++ {
			msg := RandBytes(10)
			channel.Broadcast(packet.RAW(msg))
			broadcastWait.Add(clientNum)
			broadcastWait.Wait()
		}
	}()

	gateway := StartTestGateway(t, backend.Listener().Addr().String())
	gatewayAddr := gateway.server.Listener().Addr().String()

	var wg sync.WaitGroup
	for i := 0; i < clientNum; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()

			client, err := link.Connect("tcp", gatewayAddr, protocol)
			unitest.NotError(t, err)

			for j := 0; j < 10000; j++ {
				var msg packet.RAW
				err := client.Receive(&msg)
				unitest.NotError(t, err)

				err = client.Send(msg)
				unitest.NotError(t, err)
			}

			client.Close()
		}()
	}
	wg.Wait()

	gateway.Stop()
	backend.Stop()

	time.Sleep(time.Second * 2)
	MakeSureSessionGoroutineExit(t)
}