agones.dev/agones@v1.53.0/examples/unity-simple/Assets/Scripts/UdpEchoServer.cs (about)

     1  // Copyright 2019 Google LLC
     2  // All Rights Reserved.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  using System;
    17  using Agones;
    18  using System.Net;
    19  using System.Net.Sockets;
    20  using System.Text;
    21  using UnityEngine;
    22  
    23  namespace AgonesExample
    24  {
    25      [RequireComponent(typeof(AgonesSdk))]
    26      public class UdpEchoServer : MonoBehaviour
    27      {
    28          private int Port { get; set; } = 7777;
    29          private UdpClient client = null;
    30          private AgonesSdk agones = null;
    31  
    32          async void Start()
    33          {
    34              client = new UdpClient(Port);
    35  
    36              agones = GetComponent<AgonesSdk>();
    37              bool ok = await agones.Connect();
    38              if (ok)
    39              {
    40                  Debug.Log(("Server - Connected"));
    41              }
    42              else
    43              {
    44                  Debug.Log(("Server - Failed to connect, exiting"));
    45                  Application.Quit(1);
    46              }
    47  
    48              ok = await agones.Ready();
    49              if (ok)
    50              {
    51                  Debug.Log($"Server - Ready");
    52              }
    53              else
    54              {
    55                  Debug.Log($"Server - Ready failed");
    56                  Application.Quit();
    57              }
    58          }
    59  
    60          async void Update()
    61          {
    62              if (client.Available > 0)
    63              {
    64                  IPEndPoint remote = null;
    65                  byte[] recvBytes = client.Receive(ref remote);
    66                  string recvText = Encoding.UTF8.GetString(recvBytes);
    67  
    68                  string[] recvTexts = recvText.Split(' ');
    69                  byte[] echoBytes = null;
    70                  bool ok = false;
    71                  switch (recvTexts[0])
    72                  {
    73                      case "Shutdown":
    74                          ok = await agones.Shutdown();
    75                          Debug.Log($"Server - Shutdown {ok}");
    76  
    77                          echoBytes = Encoding.UTF8.GetBytes($"Shutdown {ok}");
    78                          client.Send(echoBytes, echoBytes.Length, remote);
    79                          Application.Quit();
    80                          return;
    81  
    82                      case "Ready":
    83                          ok = await agones.Ready();
    84                          Debug.Log($"Server - Ready {ok}");
    85  
    86                          echoBytes = Encoding.UTF8.GetBytes($"Ready {ok}");
    87                          break;
    88                      
    89                      case "Allocate":
    90                          ok = await agones.Allocate();
    91                          Debug.Log($"Server - Allocate {ok}");
    92  
    93                          echoBytes = Encoding.UTF8.GetBytes($"Allocate {ok}");
    94                          break;
    95  
    96                      case "GameServer":
    97                          var gameserver = await agones.GameServer();
    98                          Debug.Log($"Server - GameServer {gameserver}");
    99  
   100                          ok = gameserver != null;
   101                          echoBytes = Encoding.UTF8.GetBytes(ok ? $"GameServer() Name: {gameserver.ObjectMeta.Name} {ok}" : $"GameServer(): {ok}");
   102                          break;
   103  
   104                      case "Label":
   105                          if (recvTexts.Length == 3)
   106                          {
   107                              (string key, string value) = (recvTexts[1], recvTexts[2]);
   108                              ok = await agones.SetLabel(key, value);
   109                              Debug.Log($"Server - SetLabel({recvTexts[1]}, {recvTexts[2]}) {ok}");
   110  
   111                              echoBytes = Encoding.UTF8.GetBytes($"SetLabel({recvTexts[1]}, {recvTexts[2]}) {ok}");
   112                          }
   113                          else
   114                          {
   115                              echoBytes = Encoding.UTF8.GetBytes($"ERROR: Invalid Label command, must use 2 arguments");
   116                          }
   117  
   118                          break;
   119  
   120                      case "Annotation":
   121                          if (recvTexts.Length == 3)
   122                          {
   123                              (string key, string value) = (recvTexts[1], recvTexts[2]);
   124                              ok = await agones.SetAnnotation(key, value);
   125                              Debug.Log($"Server - SetAnnotation({recvTexts[1]}, {recvTexts[2]}) {ok}");
   126  
   127                              echoBytes = Encoding.UTF8.GetBytes($"SetAnnotation({recvTexts[1]}, {recvTexts[2]}) {ok}");
   128                          }
   129                          else
   130                          {
   131                              echoBytes = Encoding.UTF8.GetBytes($"ERROR: Invalid Annotation command, must use 2 arguments");
   132                          }
   133                          break;
   134                      case "Reserve":
   135                          if (recvTexts.Length == 2)
   136                          {
   137                              TimeSpan duration = new TimeSpan(0, 0, Int32.Parse(recvTexts[1]));
   138                              ok = await agones.Reserve(duration);
   139                              Debug.Log($"Server - Reserve({recvTexts[1]} {ok}");
   140  
   141                              echoBytes = Encoding.UTF8.GetBytes($"Reserve({recvTexts[1]}) {ok}");
   142                          }
   143                          else
   144                          {
   145                              echoBytes = Encoding.UTF8.GetBytes($"ERROR: Invalid Reserve command, must use 1 argument");
   146                          }
   147                          break;
   148                      case "Watch":
   149                          agones.WatchGameServer(gameServer => Debug.Log($"Server - Watch {gameServer}"));
   150                          echoBytes = Encoding.UTF8.GetBytes("Watching()");
   151                          break;
   152                      default:
   153                          echoBytes = Encoding.UTF8.GetBytes($"Echo : {recvText}");
   154                          break;
   155                  }
   156  
   157                  client.Send(echoBytes, echoBytes.Length, remote);
   158  
   159                  Debug.Log($"Server - Receive[{remote.ToString()}] : {recvText}");
   160              }
   161          }
   162  
   163          void OnDestroy()
   164          {
   165              client.Close();
   166              Debug.Log("Server - Close");
   167          }
   168      }
   169  }