public void ListenHost() { var response = ReceiveMessage <HostWithinLobbyResponse>(); switch (response.MessageCase) { case HostWithinLobbyResponse.MessageOneofCase.PeerJoinedNotification: System.Console.WriteLine("Peer joined"); AddPeerToLobby(response.PeerJoinedNotification); break; case MakeHostResponse: System.Console.WriteLine("Made smb host"); break; case HostWithinLobbyResponse.MessageOneofCase.LeaveLobbyResponse: System.Console.WriteLine("Leave lobby response"); break; case GoResponse: if (response.GoResponse.PeerAddressInfo.Count > 0) { var tasks = new Task <Socket> [response.GoResponse.PeerAddressInfo.Count]; for (int i = 0; i < tasks.Length; i++) { var info = response.GoResponse.PeerAddressInfo[i]; tasks[i] = Task.Run(() => EstablishOutboundTcp(info)); System.Console.WriteLine(info.ToPrettyString()); } Task.WaitAll(tasks); for (int i = 0; i < tasks.Length; i++) { System.Console.WriteLine($"{i}: Connection established? {tasks[i].Result != null}"); } } ConnectedToPeerEvent?.Invoke(); state = Tcp_State.Closing; break; default: System.Console.WriteLine("Unexpected response/notification"); break; } }
public void ListenPeer() { var response = ReceiveMessage <PeerWithinLobbyResponse>(); switch (response.MessageCase) { case PeerWithinLobbyResponse.MessageOneofCase.PeerJoinedNotification: AddPeerToLobby(response.PeerJoinedNotification); System.Console.WriteLine("Peer joined"); break; case LeaveLobbyNotification: System.Console.WriteLine("Leave lobby notification"); break; case BecomeHostNotification: System.Console.WriteLine("Promoted to host"); break; case PeerWithinLobbyResponse.MessageOneofCase.LeaveLobbyResponse: System.Console.WriteLine("Leave lobby response"); break; case HostAddressInfo: { var info = response.HostAddressInfo; System.Console.WriteLine(info.ToPrettyString()); var task = Task.Run(() => EstablishOutboundTcp(response.HostAddressInfo)); Task.WaitAll(task); System.Console.WriteLine($"Connection to host established? {task.Result != null}"); state = Tcp_State.Closing; ConnectedToPeerEvent?.Invoke(); break; } default: System.Console.WriteLine("Unexpected response/notification"); break; } }