ðŸ–ĨïļSampleGame Circle 9

Study

Main Sequence

Static object / script

  • QuitCanvasStatic

  • GameLiftStatic / GameLift.cs

Other

  1. sign up / sign in

  2. game start

  3. await GameLift.GetConnectionInfo(cancellationToken);

  4. TcpClient Connect

  5. game logic start

Connection

//GameLogic.cs
private async Task Connect(CancellationToken cancellationToken)
{
    StartGameScreen startGameScreen = Instantiate(_startGameScreenPrefab, _mainCanvas.transform);
    startGameScreen.SetSubmitAction(StartConnection);
    startGameScreen.Show();

    while (!ClientConnected)
    {
        while (!_startConnection)
        {
            // similar to yield return null
            await Task.Yield();
        }

        _startConnection = false;
        startGameScreen.SetInteractable(false);
        startGameScreen.SetResultText(string.Empty);

        (bool success, ConnectionInfo connectionInfo) = await GameLift.GetConnectionInfo(cancellationToken);

        if (success)
        {
            GameliftStatus = connectionInfo.IpAddress != NetworkClient.LocalHost;
            ClientConnected = _client.TryConnect(connectionInfo); // connection info
        }

        if (!ClientConnected)
        {
            startGameScreen.SetInteractable(true);
            startGameScreen.SetResultText(Strings.ErrorStartingGame);
        }
    }

    startGameScreen.Hide();
}
//NetworkClient.cs
public bool TryConnect(ConnectionInfo connectionInfo)
{
    try
    {
        _client = new TcpClient(connectionInfo.IpAddress, connectionInfo.Port);
        string msgStr = "CONNECT:" + connectionInfo.Serialize();
        NetworkProtocol.Send(_client, msgStr);
        return true;
    }
    catch (ArgumentNullException e)
    {
        _client = null;
        _gl.Log.WriteLine(":( CONNECT TO SERVER " + connectionInfo.IpAddress + " FAILED: " + e);
        return false;
    }
    catch (SocketException e) // server not available
    {
        _client = null;

        if (connectionInfo.IpAddress == LocalHost)
        {
            _gl.Log.WriteLine(":) CONNECT TO LOCAL SERVER FAILED: PROBABLY NO LOCAL SERVER RUNNING, TRYING GAMELIFT");
        }
        else
        {
            _gl.Log.WriteLine(":( CONNECT TO SERVER " + connectionInfo.IpAddress + "FAILED: " + e + " (ARE YOU ON THE *AMAZON*INTERNAL*NETWORK*?)");
        }

        return false;
    }
}

NetworkProtocol

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;

public static class NetworkProtocol
{
    public static string[] Receive(TcpClient client)
    {
        NetworkStream stream = client.GetStream();
        var messages = new List<string>();

        while (stream.DataAvailable)
        {
            byte[] bufferLength = new byte[4];
            stream.Read(bufferLength, 0, bufferLength.Length);
            int msgSize = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bufferLength, 0));
            byte[] readBuffer = new byte[msgSize];
            stream.Read(readBuffer, 0, readBuffer.Length);
            string msgStr = Encoding.ASCII.GetString(readBuffer, 0, readBuffer.Length);
            messages.Add(msgStr);
        }

        return messages.ToArray();
    }

    public static void Send(TcpClient client, string msgStr)
    {
        if (client == null)
        {
            return;
        }

        NetworkStream stream = client.GetStream();
        byte[] writeBuffer = Encoding.ASCII.GetBytes(msgStr);
        int msgSize = writeBuffer.Length;
        byte[] bufferLength = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(msgSize));
        stream.Write(bufferLength, 0, bufferLength.Length);
        stream.Write(writeBuffer, 0, msgSize);
    }
}

Last updated