private void BecomeHost() { joined_lobby.host_id = id; var host_notification = new BecomeHostNotification(); var response = new PeerWithinLobbyResponse { BecomeHostNotification = host_notification }; response.WriteDelimitedTo(stream); // Maybe notify of new host // var peer_notification = new Pee // foreach (int peer_id in lobby.GetNonHostPeerIds()) // { // } }
private void SendPeerJoinedNotification(PeerJoinedNotification notification) { IMessage response; if (IsHost) { var resp = new HostWithinLobbyResponse(); resp.PeerJoinedNotification = notification; response = resp; } else { var resp = new PeerWithinLobbyResponse(); resp.PeerJoinedNotification = notification; response = resp; } response.WriteDelimitedTo(stream); }
public HostWithinLobbyResponse ReceiveWithinLobbyHostRequest() { // same spiel as above goes for here as well if (!TryGetMessageOrStateChange(out HostWithinLobbyRequest request)) { return(null); } var outerResponse = new HostWithinLobbyResponse(); switch (request.MessageCase) { case HostWithinLobbyRequest.MessageOneofCase.LeaveLobbyRequest: { LeaveLobby(); var response = new LeaveLobbyResponse(); response.Success = true; state = Tcp_State.WithoutLobby; return(new HostWithinLobbyResponse { LeaveLobbyResponse = response }); } case MakeHostRequest: { var response = new MakeHostResponse(); int peer_id = request.MakeHostRequest.PeerId; if (id != peer_id && // since we are host joined_lobby.peers.ContainsKey(peer_id)) { response.NewHostId = peer_id; MakeHost(joined_lobby.peers[peer_id]); } return(new HostWithinLobbyResponse { MakeHostResponse = response }); } case GoRequest: { var host_response = new GoResponse(); var host_address_message = CreateAddressMessage(); var peer_notification = new PeerWithinLobbyResponse { HostAddressInfo = host_address_message }; // TODO: run concurrently (although sending is pretty fast, I suppose) foreach (var peer_id in joined_lobby.GetNonHostPeerIds()) { var peer = joined_lobby.peers[peer_id]; try { peer.joined_lobby = null; peer.TransitionState(Tcp_State.Closing); peer_notification.WriteDelimitedTo(peer.stream); host_response.PeerAddressInfo.Add(peer.CreateAddressMessage()); } catch { peer.Log($"An error has been catched while trying to send PeerAddressInfo"); } } server.lobbies.Remove(joined_lobby.id); joined_lobby = null; state = Tcp_State.Closing; return(new HostWithinLobbyResponse { GoResponse = host_response }); } default: Log($"Unexpected within lobby request message."); return(null); } }