diff --git a/unity/Assets/Scripts/Client.cs b/unity/Assets/Scripts/Client.cs index 3b6bb92..6aa3e83 100644 --- a/unity/Assets/Scripts/Client.cs +++ b/unity/Assets/Scripts/Client.cs @@ -4,6 +4,7 @@ using System.Net.Sockets; using UnityEngine; using Utilities; using System.Net; +using System; public static class Client { @@ -30,7 +31,7 @@ public static class Client public class ConnectionImpl // : MonoBehaviour { - private TcpClient conn; + public TcpConnection conn; private string connId; private uint? lobby = null; @@ -39,19 +40,25 @@ public static class Client private Modifiable lobby_status = null; public ConnectionImpl(string user, string address) { - // channel = new Channel(address, ChannelCredentials.Insecure); - // connection = new ConnectionService.Connection.ConnectionClient(channel); - // lobby_client = new Lobby.Lobby.LobbyClient(channel); - // game_client = new Game.Game.GameClient(channel); - string[] ipAndPort = address.Split(':'); - conn = new TcpClient(ipAndPort[0], int.Parse(ipAndPort[1])); - // connId = connection.connect(new Common.Name { Name_ = user }).Id; + Debug.Log("Connecting with the tcp channel"); + conn = new TcpConnection(address); + conn.SendMessage(new Protocol.ClientServerPacket() {Connect = new Common.Name{Name_ = user}}); + Protocol.ServerClientPacket p = null; + Debug.Log("Waiting for response"); + while(p == null) { + conn.q.TryDequeue(out p); + } + if (p.ReturnConnect == null) { + throw new Exception("Unexpected non ReturnConnect packet"); + } + connId = p.ReturnConnect.Id; this.address = address; } - // public string Name() { - // return connection.name(new Google.Protobuf.WellKnownTypes.Empty()).Name_; - // } + public void QueryName() { + conn.SendMessage(new Protocol.ClientServerPacket() {QueryName = new Google.Protobuf.WellKnownTypes.Empty()}); + // return connection.name(new Google.Protobuf.WellKnownTypes.Empty()).Name_; + } public string GetServerAddress() { return address; } diff --git a/unity/Assets/Scripts/DiscordController.cs b/unity/Assets/Scripts/DiscordController.cs index 317b86f..23609ce 100644 --- a/unity/Assets/Scripts/DiscordController.cs +++ b/unity/Assets/Scripts/DiscordController.cs @@ -17,18 +17,9 @@ public class DiscordController : MonoBehaviour { void Awake() { DontDestroyOnLoad(this.gameObject); } - - public TcpConnection t; - // Use this for initialization void Start() { - t = new TcpConnection("127.0.0.1:50052"); - t.SendMessage(new Protocol.ClientServerPacket() { - Connect = new Common.Name() { - Name_ = "AAAAAAAAAAAAAAAAA" - } - }); - + Debug.Log("Starting discord"); discord = new Discord.Discord(778707900293971979, (uint)Discord.CreateFlags.NoRequireDiscord); Debug.Log("Started discord"); diff --git a/unity/Assets/Scripts/MainMenuController.cs b/unity/Assets/Scripts/MainMenuController.cs index 619c3fd..d565cb5 100644 --- a/unity/Assets/Scripts/MainMenuController.cs +++ b/unity/Assets/Scripts/MainMenuController.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; +using System; public class MainMenuController : MonoBehaviour { [Header("Prefabs")] @@ -96,28 +97,28 @@ public class MainMenuController : MonoBehaviour { } public void ConnectServer() { - // try { - // var conn = Client.GetConnection(); - // if (conn == null) { - // if (ip.text != "" && port.text != "") - // Client.Connect(username.text, ip.text.Trim() + ":" + port.text.Trim()); - // else - // Client.Connect(username.text); - // conn = Client.GetConnection(); - // currentUsername = username.text; - // } - - // if (conn != null) { - // mainMenu.SetActive(false); - // serverMenu.SetActive(true); - // lobbyMenu.SetActive(false); - // ReloadMenu(); - // } - // } catch(RpcException e) { - // if(e.StatusCode == StatusCode.Unavailable) { - // Popup.Open(PopupType.Alert, mainMenu.transform, "ConnectionUnavailableError", "Error", "Server unavailable.\nPlease retry later or contact the server admin.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nPOOTIS"); - // } - // } + try { + var conn = Client.GetConnection(); + if (conn == null) { + if (ip.text != "" && port.text != "") + Client.Connect(username.text, ip.text.Trim() + ":" + port.text.Trim()); + else + Client.Connect(username.text); + conn = Client.GetConnection(); + currentUsername = username.text; + } + + if (conn != null) { + mainMenu.SetActive(false); + serverMenu.SetActive(true); + lobbyMenu.SetActive(false); + ReloadMenu(); + } + } catch(Exception e) { + Debug.Log(e.ToString()); + Popup.Open(PopupType.Alert, mainMenu.transform, "ConnectionUnavailableError", "Error", "Server unavailable.\nPlease retry later or contact the server admin.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nPOOTIS"); + + } } public void LeaveServer() { @@ -235,6 +236,13 @@ public class MainMenuController : MonoBehaviour { } // conn.Close(); } + // FIXME + Client.GetConnection().QueryName(); + Protocol.ServerClientPacket p = null; + while (p == null) { + Client.GetConnection().conn.q.TryDequeue(out p); + } + serverName.text = p.ReturnName.Name_; // serverName.text = Client.GetConnection().Name(); serverMenu.GetComponentInChildren().Reload(); diff --git a/unity/Assets/Scripts/TcpConnection.cs b/unity/Assets/Scripts/TcpConnection.cs index ff70171..3514a5a 100644 --- a/unity/Assets/Scripts/TcpConnection.cs +++ b/unity/Assets/Scripts/TcpConnection.cs @@ -1,15 +1,49 @@ using System.Net.Sockets; +using System.Collections.Concurrent; +using System.Threading; using System; using Google.Protobuf; using UnityEngine; public class TcpConnection { private TcpClient c; + public ConcurrentQueue q; + private Thread receiverThread; + + private Protocol.ServerClientPacket Receive() { + byte[] b = new byte[4]; + c.Client.Receive(b, 0, 4, + SocketFlags.None); + if (!BitConverter.IsLittleEndian) { + // Invert byte order, in order to get little endian byte order + byte a = b[0]; + b[0] = b[3]; + b[3] = a; + a = b[1]; + b[1] = b[2]; + b[2] = a; + } + int size = BitConverter.ToInt32(b, 0); + byte[] data = new byte[size]; + c.Client.Receive(data, 0, size, SocketFlags.None); + return Protocol.ServerClientPacket.Parser.ParseFrom(data); + } + + private void ReceiverThread() { + Debug.Log("Started receiver thread"); + while(true) { + Protocol.ServerClientPacket packet = Receive(); + q.Enqueue(packet); + } + } public TcpConnection(string address) { string[] ipAndPort = address.Split(':'); c = new TcpClient(ipAndPort[0], int.Parse(ipAndPort[1])); - + q = new ConcurrentQueue(); + receiverThread = new Thread(new ThreadStart(ReceiverThread)); + Debug.Log("Starting receiver thread"); + receiverThread.Start(); } public void SendMessage(Protocol.ClientServerPacket packet) { @@ -28,16 +62,6 @@ public class TcpConnection { c.Client.Send(sizeBytes); byte[] data = packet.ToByteArray(); c.Client.Send(data); - - - byte[] b = new byte[4]; - int byteCount = c.Client.Receive(b, 0, 4, - SocketFlags.None); - size = BitConverter.ToInt32(b, 0); - data = new byte[size]; - c.Client.Receive(data, 0, size, SocketFlags.None); - var p = Protocol.ServerClientPacket.Parser.ParseFrom(data); - Debug.Log(p.ToString()); } } \ No newline at end of file