Browse Source

Close receiver thread when disconnecting

new_protocol
KeyKoder 5 years ago
parent
commit
148c378387
  1. 1
      unity/Assets/Prefabs/CardCanvas.prefab
  2. 2
      unity/Assets/Scenes/SampleScene.unity
  3. 10
      unity/Assets/Scripts/Client.cs
  4. 33
      unity/Assets/Scripts/GameLoader.cs
  5. 5
      unity/Assets/Scripts/MainMenuController.cs
  6. 5
      unity/Assets/Scripts/TcpConnection.cs

1
unity/Assets/Prefabs/CardCanvas.prefab

@ -81,6 +81,7 @@ MonoBehaviour:
m_FallbackScreenDPI: 96
m_DefaultSpriteDPI: 96
m_DynamicPixelsPerUnit: 1
m_PresetInfoIsWorld: 0
--- !u!114 &5463950459347836261
MonoBehaviour:
m_ObjectHideFlags: 0

2
unity/Assets/Scenes/SampleScene.unity

@ -135,7 +135,7 @@ GameObject:
- component: {fileID: 40103895}
- component: {fileID: 40103897}
m_Layer: 0
m_Name: DeckGen
m_Name: Game Loader
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0

10
unity/Assets/Scripts/Client.cs

@ -40,10 +40,6 @@ public class Client : MonoBehaviour {
}
}
void OnApplicationQuit() {
CloseConnection();
}
public class NetEventManager {
private Dictionary<string, Action<Common.Name>> returnNameHandlers = new Dictionary<string, Action<Common.Name>>();
private Dictionary<string, Action<Connection.UserID>> returnConnectHandlers = new Dictionary<string, Action<Connection.UserID>>();
@ -118,6 +114,7 @@ public class Client : MonoBehaviour {
}
break;
case Protocol.ServerClientPacket.DataOneofCase.ReturnCardImage: {
Debug.Log("got: "+p);
var v = new Action<Game.Image>[this.returnCardImageHandlers.Count];
this.returnCardImageHandlers.Values.CopyTo(v, 0);
foreach (var n in v) {
@ -273,6 +270,10 @@ public class Client : MonoBehaviour {
conn.SendMessage(new Protocol.ClientServerPacket() { QueryUsers = new Empty() });
}
public void GetGameStatus() {
conn.SendMessage(new Protocol.ClientServerPacket() { QueryGameStatus = new Empty() });
}
public List<Lobby.Vote> GetLobbyVotes(uint game) {
var gameVotes = new List<Lobby.Vote>();
if (lobby_status.Get() != null){
@ -373,6 +374,7 @@ public class Client : MonoBehaviour {
public void Close() {
conn.SendMessage(new Protocol.ClientServerPacket() {Disconnect = new Empty()});
conn.Close();
}
}

33
unity/Assets/Scripts/GameLoader.cs

@ -1,7 +1,9 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;
public class GameLoader : MonoBehaviour {
public GameObject table;
@ -14,12 +16,15 @@ public class GameLoader : MonoBehaviour {
public GameObject deck;
public GameObject thrownCards;
public static MainMenuController mmc;
public static System.Action onReloadPiles;
public static GameLoader myself;
private int tmpCounter = 0;
void Awake() {
myself = this;
}
void Start() {
mmc = FindObjectOfType<MainMenuController>();
mmc.gameObject.SetActive(false);
var angleDelta = (270) / playerCount;
var dst = 15;
var angleOffset = 45;
@ -32,15 +37,13 @@ public class GameLoader : MonoBehaviour {
var player = Instantiate(playerCube, pos, Quaternion.AngleAxis(-angle, Vector3.up));
player.GetComponent<Renderer>().material.color = playerColors[i];
}
SpawnClientCards();
SpawnCommonCards();
onReloadPiles += SpawnCommonCards;
onReloadPiles += SpawnClientCards;
ReloadPiles();
}
void SpawnCard(Client.ConnectionImpl conn, string cardKind, GameObject parent, System.Action<GameObject> callback) {
var card = Instantiate(cardPrefab, Vector3.zero, Quaternion.identity, parent.transform);
conn.eventManager.AddHandler("game_card_image", (Game.Image image) => {
string uuid = Guid.NewGuid().ToString();
conn.eventManager.AddHandler("game_card_image_"+uuid, (Game.Image image) => {
var card = Instantiate(cardPrefab, Vector3.zero, Quaternion.identity, parent.transform);
var front = new Texture2D(1, 1);
front.LoadImage(image.Face.Span.ToArray());
var back = new Texture2D(1, 1);
@ -49,7 +52,7 @@ public class GameLoader : MonoBehaviour {
card.transform.Find("Images").GetComponentsInChildren<RawImage>()[1].texture = back;
card.transform.localScale = new Vector3(0.15f, 0.15f, 1f);
callback.Invoke(card);
Client.GetConnection().eventManager.RemoveHandler(Protocol.ServerClientPacket.DataOneofCase.ReturnCardImage, "game_card_image");
conn.eventManager.RemoveHandler(Protocol.ServerClientPacket.DataOneofCase.ReturnCardImage, "game_card_image_"+uuid);
});
conn.GetCardImage(cardKind);
}
@ -57,9 +60,7 @@ public class GameLoader : MonoBehaviour {
void SpawnClientCards() {
var conn = Client.GetConnection();
if (conn != null) {
Debug.Log("Spawning player cards.");
var clientPiles = conn.GetPlayerPiles(mmc.currentUsername);
Debug.Log("[player] clientPiles = "+clientPiles);
if (clientPiles == null) return;
foreach (string key in clientPiles.Keys) {
var pile = clientPiles[key];
@ -76,15 +77,14 @@ public class GameLoader : MonoBehaviour {
void SpawnCommonCards() {
var conn = Client.GetConnection();
if (conn != null) {
Debug.Log("Spawning common cards.");
var clientPiles = conn.GetCommonPiles();
Debug.Log("[common] clientPiles = " + clientPiles);
if (clientPiles == null) return;
foreach (string key in clientPiles.Keys) {
var pile = clientPiles[key];
int idx = 0;
foreach (Game.GameStatus.Types.Card card in pile.Cards) {
SpawnCard(conn, card.Kind.Kind, key == "placed" ? thrownCards : deck, (GameObject card) => {
card.GetComponent<Card>().ThrowCard(key == "placed" ? thrownCards : deck);
card.GetComponent<Card>().InitData(key, true, idx);
});
idx++;
@ -92,12 +92,13 @@ public class GameLoader : MonoBehaviour {
}
}
}
public static void ReloadPiles() {
if (myself == null) return;
var cards = GameObject.FindGameObjectsWithTag("Card");
foreach (GameObject card in cards) {
Destroy(card);
}
onReloadPiles();
myself.SpawnCommonCards();
myself.SpawnClientCards();
}
}

5
unity/Assets/Scripts/MainMenuController.cs

@ -74,7 +74,10 @@ public class MainMenuController : MonoBehaviour {
void Update() {
var conn = Client.GetConnection();
if(conn != null) {
if (conn.IsStarting()) SceneManager.LoadScene(1);
if (conn.IsStarting()) {
SceneManager.LoadScene(1);
conn.GetGameStatus();
}
}
}

5
unity/Assets/Scripts/TcpConnection.cs

@ -1,3 +1,4 @@
using System.IO;
using System.Net.Sockets;
using System.Collections.Concurrent;
using System.Threading;
@ -37,6 +38,10 @@ public class TcpConnection {
}
}
public void Close() {
receiverThread.Join();
}
public TcpConnection(string address) {
string[] ipAndPort = address.Split(':');
c = new TcpClient(ipAndPort[0], int.Parse(ipAndPort[1]));

Loading…
Cancel
Save