Browse Source

Add receiver thread

new_protocol
ThePerkinrex 5 years ago
parent
commit
20ceddab6a
No known key found for this signature in database GPG Key ID: FD81DE6D75E20917
  1. 27
      unity/Assets/Scripts/Client.cs
  2. 9
      unity/Assets/Scripts/DiscordController.cs
  3. 50
      unity/Assets/Scripts/MainMenuController.cs
  4. 46
      unity/Assets/Scripts/TcpConnection.cs

27
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.LobbyStatus> 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() {
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;
}

9
unity/Assets/Scripts/DiscordController.cs

@ -17,17 +17,8 @@ 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);

50
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;
// }
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");
// }
// }
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<Scrollable>().Reload();

46
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<Protocol.ServerClientPacket> 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<Protocol.ServerClientPacket>();
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());
}
}
Loading…
Cancel
Save