//NetworkClient.cspublicboolTryConnect(ConnectionInfo connectionInfo){try { _client =newTcpClient(connectionInfo.IpAddress,connectionInfo.Port);string msgStr ="CONNECT:"+connectionInfo.Serialize();NetworkProtocol.Send(_client, msgStr);returntrue; }catch (ArgumentNullException e) { _client =null;_gl.Log.WriteLine(":( CONNECT TO SERVER "+connectionInfo.IpAddress+" FAILED: "+ e);returnfalse; }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*?)");
}returnfalse; }}
NetworkProtocol
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.// SPDX-License-Identifier: MIT-0usingSystem;usingSystem.Collections.Generic;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Text;publicstaticclassNetworkProtocol{publicstaticstring[] Receive(TcpClient client) {NetworkStream stream =client.GetStream();var messages =newList<string>();while (stream.DataAvailable) {byte[] bufferLength =newbyte[4];stream.Read(bufferLength,0,bufferLength.Length);int msgSize =IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bufferLength,0));byte[] readBuffer =newbyte[msgSize];stream.Read(readBuffer,0,readBuffer.Length);string msgStr =Encoding.ASCII.GetString(readBuffer,0,readBuffer.Length);messages.Add(msgStr); }returnmessages.ToArray(); }publicstaticvoidSend(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); }}