// ================================================ // NETWORK EVENT PROCESSING // ================================================ void ReadNetworkEvents() { NetworkEvent netEvent; while (m_Network != null && m_Network.Dequeue(out netEvent)) { ProcessNetworkEvent(netEvent); } }
void HandleEvents() { if (network == null) { return; } network.Update(); NetworkEvent Event; while (network != null && network.Dequeue(out Event)) { if (Event.Type == NetEventType.ServerInitialized) { isServer = true; } else if (Event.Type == NetEventType.ServerInitFailed) { Disconnect(); } else if (Event.Type == NetEventType.ServerClosed) { isServer = false; } else if (Event.Type == NetEventType.NewConnection) { connections.Add(Event.ConnectionId); } else if (Event.Type == NetEventType.ConnectionFailed) { Disconnect(); } else if (Event.Type == NetEventType.Disconnected) { connections.Remove(Event.ConnectionId); if (isServer == false) { Disconnect(); } } onEvent.OnNext(Event); } if (network != null) { network.Flush(); } }
protected override void ReadMessages() { if (_webrtcNetwork == null) { return; } _webrtcNetwork.Update(); NetworkEvent evt; while (_webrtcNetwork != null && _webrtcNetwork.Dequeue(out evt)) { switch (evt.Type) { case NetEventType.UnreliableMessageReceived: case NetEventType.ReliableMessageReceived: var buffer = evt.MessageData; NetworkReceivedPacket(new PureP2PPeer(evt.ConnectionId), new ArraySegment <byte>(buffer.Buffer, buffer.Offset, buffer.ContentLength)); break; case NetEventType.FatalError: case NetEventType.ServerInitFailed: FatalError(string.Format("WebRTC `{0}`", evt.Type)); break; case NetEventType.ConnectionFailed: case NetEventType.Disconnected: ClientDisconnected(new PureP2PPeer(evt.ConnectionId)); break; case NetEventType.NewConnection: case NetEventType.Invalid: case NetEventType.ServerInitialized: case NetEventType.Warning: case NetEventType.Log: Log.Debug("Ignoring `{0}` message type", evt.Type); break; case NetEventType.ServerClosed: throw Log.CreatePossibleBugException(string.Format("Server received `{0}` event", evt.Type), "9576541B-13E4-432F-A8BF-5FD730AAD2D9"); default: throw new ArgumentOutOfRangeException(); } } }
private void HandleNetwork() { //check if the network was created if (mNetwork != null) { //first update it to read the data from the underlaying network system mNetwork.Update(); //handle all new events that happened since the last update NetworkEvent evt; //check for new messages and keep checking if mNetwork is available. it might get destroyed //due to an event while (mNetwork != null && mNetwork.Dequeue(out evt)) { //print to the console for debugging Debug.Log(evt); //check every message switch (evt.Type) { case NetEventType.ServerInitialized: { //server initialized message received //this is the reaction to StartServer -> switch GUI mode mIsServer = true; string address = evt.Info; Append("Server started. Address: " + address); uRoomName.text = "" + address; } break; case NetEventType.ServerInitFailed: { //user tried to start the server but it failed //maybe the user is offline or signaling server down? mIsServer = false; Append("Server start failed."); Reset(); } break; case NetEventType.ServerClosed: { //server shut down. reaction to "Shutdown" call or //StopServer or the connection broke down mIsServer = false; Append("Server closed. No incoming connections possible until restart."); } break; case NetEventType.NewConnection: { mConnections.Add(evt.ConnectionId); //either user runs a client and connected to a server or the //user runs the server and a new client connected Append("New local connection! ID: " + evt.ConnectionId); //if server -> send announcement to everyone and use the local id as username if (mIsServer) { //user runs a server. announce to everyone the new connection //using the server side connection id as identification string msg = "New user " + evt.ConnectionId + " joined the room."; Append(msg); SendString(msg); } } break; case NetEventType.ConnectionFailed: { //Bugfix: The behavior of IBasicNetwork changed a little in past updates. //It will now report also failed incoming connections if (this.mIsServer == false) { //Outgoing connection failed. Inform the user. Append("Connection failed"); Reset(); } else { //This happens if another user tries to connect //but the signaling fails e.g. //* a firewall blocked the direct connection but allowed // the signaling to start the connection process // (could be a local firewall or at the remote users side) // //* STUN / TURN server failed or isn't available at all // //* other user cut the connection before it could fully establish Debug.Log("An incoming connection failed."); } } break; case NetEventType.Disconnected: { mConnections.Remove(evt.ConnectionId); //A connection was disconnected //If this was the client then he was disconnected from the server //if it was the server this just means that one of the clients left Append("Local Connection ID " + evt.ConnectionId + " disconnected"); if (mIsServer == false) { Reset(); } else { string userLeftMsg = "User " + evt.ConnectionId + " left the room."; //show the server the message Append(userLeftMsg); //other users left? inform them if (mConnections.Count > 0) { SendString(userLeftMsg); } } } break; case NetEventType.ReliableMessageReceived: case NetEventType.UnreliableMessageReceived: { HandleIncommingMessage(ref evt); } break; } } //finish this update by flushing the messages out if the network wasn't destroyed during update if (mNetwork != null) { mNetwork.Flush(); } } }
private void HandleNetwork() { //check if the network was created if (mNetwork != null) { //first update it to read the data from the underlaying network system mNetwork.Update(); //handle all new events that accured since the last update NetworkEvent evt; while (mNetwork.Dequeue(out evt)) { Debug.Log(evt); switch (evt.Type) { case NetEventType.ServerInitialized: { mIsServer = true; string address = evt.Info; Append("Server started. Address: " + address); uRoomName.text = "" + address; SetGuiState(false); } break; case NetEventType.ServerInitFailed: { mIsServer = false; Append("Server start failed."); SetGuiState(true); } break; case NetEventType.ServerClosed: { mIsServer = false; Append("Server closed."); SetGuiState(true); } break; case NetEventType.NewConnection: { Append("New local connection! ID: " + evt.ConnectionId); //if server -> send announcement to everyone and use the local id as username if (mIsServer) { string msg = "New user " + evt.ConnectionId + " joined the room."; Append(msg); SendString(msg); } SetGuiState(false); } break; case NetEventType.ConnectionFailed: { Append("Connection failed"); SetGuiState(true); } break; case NetEventType.Disconnected: { Append("Local Connection ID " + evt.ConnectionId + " disconnected"); if (mNetwork.IsServer == false) { SetGuiState(true); } } break; case NetEventType.ReliableMessageReceived: case NetEventType.UnreliableMessageReceived: { HandleIncommingMessage(ref evt); } break; } } //finish this update by flushing the messages out mNetwork.Flush(); } }
private void HandleNetwork() { //check if the network was created if (mNetwork != null) { //first update it to read the data from the underlaying network system mNetwork.Update(); //handle all new events that accured since the last update NetworkEvent evt; while (mNetwork.Dequeue(out evt)) { //print to the console for debugging Debug.Log(evt); //check every message switch (evt.Type) { case NetEventType.ServerInitialized: { //server initialized message received //this is the reaction to StartServer -> switch gui mode mIsServer = true; string address = evt.Info; Append("Server started. Address: " + address); uRoomName.text = "" + address; SetGuiState(false); } break; case NetEventType.ServerInitFailed: { //user tried to start the server but it failed //maybe the user is offline or signaling server down? mIsServer = false; Append("Server start failed."); SetGuiState(true); } break; case NetEventType.ServerClosed: { //server shut down. reaction to "Shutdown" call or //StopServer or the connection broke down mIsServer = false; Append("Server closed."); SetGuiState(true); } break; case NetEventType.NewConnection: { //either user runs a client and connected to a server or the //user runs the server and a new client connected Append("New local connection! ID: " + evt.ConnectionId); //if server -> send announcement to everyone and use the local id as username if (mIsServer) { //user runs a server. announce to everyone the new connection //using the server side connection id as idendification string msg = "New user " + evt.ConnectionId + " joined the room."; Append(msg); SendString(msg); } SetGuiState(false); } break; case NetEventType.ConnectionFailed: { //Outgoing connection failed. Inform the user. Append("Connection failed"); SetGuiState(true); } break; case NetEventType.Disconnected: { //A connection was disconnected //If this was the client then he was disconnected from the server //if it was the server this just means that one of the clients left Append("Local Connection ID " + evt.ConnectionId + " disconnected"); if (mNetwork.IsServer == false) { SetGuiState(true); } else { string userLeftMsg = "User " + evt.ConnectionId + " left the room."; //show the server the message Append(userLeftMsg); //other users left? inform them if (mNetwork.Connections.Count > 0) { SendString(userLeftMsg); } } } break; case NetEventType.ReliableMessageReceived: case NetEventType.UnreliableMessageReceived: { HandleIncommingMessage(ref evt); } break; } } //finish this update by flushing the messages out mNetwork.Flush(); } }
void HandleNetwork() { //check if the network was created if (mNetwork != null) { //first update it to read the data from the underlaying network system mNetwork.Update(); //handle all new events that happened since the last update NetworkEvent evt; //check for new messages and keep checking if mNetwork is available. it might get destroyed //due to an event while (mNetwork != null && mNetwork.Dequeue(out evt)) { //check every message switch (evt.Type) { case NetEventType.ServerInitialized: { //server initialized message received //this is the reaction to StartServer -> switch GUI mode string address = evt.Info; PrintDebug("Server started. Address: " + address); //uRoomName.text = "" + address; } break; case NetEventType.ServerInitFailed: { //user tried to start the server but it failed //maybe the user is offline or signaling server down? PrintDebug("Server start failed."); } break; case NetEventType.ServerClosed: { mNetwork.Connect(roomName); } break; case NetEventType.NewConnection: { // mConnections.Add(evt.ConnectionId); //either user runs a client and connected to a server or the //user runs the server and a new client connected PrintDebug("New local connection! ID: " + evt.ConnectionId); peers[evt.ConnectionId.ToString()] = evt.ConnectionId; if (OnConnection != null) { OnConnection(evt.ConnectionId); } } break; case NetEventType.ConnectionFailed: { //Outgoing connection failed. Inform the user. PrintDebug("Connection failed"); mNetwork.Connect(roomName); } break; case NetEventType.Disconnected: { //mConnections.Remove(evt.ConnectionId); //A connection was disconnected //If this was the client then he was disconnected from the server //if it was the server this just means that one of the clients left PrintDebug("Local Connection ID " + evt.ConnectionId + " disconnected"); if (peers.ContainsKey(evt.ConnectionId.ToString())) { peers.Remove(evt.ConnectionId.ToString()); } if (OnDisconnection != null) { OnDisconnection(evt.ConnectionId); } } break; case NetEventType.ReliableMessageReceived: { if (OnReceivedMessage != null) { OnReceivedMessage(evt); } // Maybe call // evt.MessageData.Dispose(); // ? Idk. Same for Unreliable } break; case NetEventType.UnreliableMessageReceived: { if (OnReceivedMessage != null) { OnReceivedMessage(evt); } } break; } } //finish this update by flushing the messages out if the network wasn't destroyed during update if (mNetwork != null) { mNetwork.Flush(); } } }
private void HandleNetwork() { //check if the network was created if (mNetwork != null) { //first update it to read the data from the underlaying network system mNetwork.Update(); //handle all new events that happened since the last update NetworkEvent evt; //check for new messages and keep checking if mNetwork is available. it might get destroyed //due to an event while (mNetwork != null && mNetwork.Dequeue(out evt)) { lastTimeGotAnything = DateTime.Now; //check every message switch (evt.Type) { case NetEventType.ServerInitialized: { //server initialized message received mIsServer = true; string address = evt.Info; PrintDebug("Server started. Address: " + address); } break; case NetEventType.ServerInitFailed: { //user tried to start the server but it failed //maybe the user is offline or signaling server down? PrintDebug("Server start failed."); mIsServer = false; } break; case NetEventType.ServerClosed: { PrintDebug("Server closed"); mNetwork.StartServer(roomName); } break; case NetEventType.NewConnection: { //user runs a server. announce to everyone the new connection //using the server side connection id as identification string msg = "New user " + evt.ConnectionId + " joined the room."; PrintDebug(msg); peers[evt.ConnectionId.ToString()] = evt.ConnectionId; if (OnConnection != null) { OnConnection(evt.ConnectionId); } } break; case NetEventType.ConnectionFailed: { //Outgoing connection failed. Inform the user. PrintDebug("Connection failed"); } break; case NetEventType.Disconnected: { //mConnections.Remove(evt.ConnectionId); //A connection was disconnected //If this was the client then he was disconnected from the server //if it was the server this just means that one of the clients left PrintDebug("Local Connection ID " + evt.ConnectionId + " disconnected"); if (peers.ContainsKey(evt.ConnectionId.ToString())) { peers.Remove(evt.ConnectionId.ToString()); } if (OnDisconnection != null) { OnDisconnection(evt.ConnectionId); } } break; case NetEventType.ReliableMessageReceived: { if (OnReceivedMessage != null) { OnReceivedMessage(evt); } } break; case NetEventType.UnreliableMessageReceived: { if (OnReceivedMessage != null) { OnReceivedMessage(evt); } } break; } } //finish this update by flushing the messages out if the network wasn't destroyed during update if (mNetwork != null) { mNetwork.Flush(); } } }
void HandleNetwork() { if (mNetwork != null) { mNetwork.Update(); NetworkEvent evt; while (mNetwork != null && mNetwork.Dequeue(out evt)) { //Debug.Log(evt); switch (evt.Type) { case NetEventType.ServerInitialized: { //server initialized message received mIsServer = true; if (connected != null) { connected(); } } break; case NetEventType.ServerInitFailed: { //user tried to start the server but it failed mIsServer = false; Debug.Log("Server start failed: " + channel); Reset(); Invoke("CreateChannel", 5.0f); } break; case NetEventType.ServerClosed: { mIsServer = false; Debug.Log("Server closed. No incoming connections possible until restart."); Reset(); } break; case NetEventType.NewConnection: { ConnectionStatus status; status.isReadyForStream = false; status.isCompressStream = false; status.isTemporalStop = false; status.sendQueue = 0; mConnections.Add( new KeyValuePair <ConnectionId, ConnectionStatus>(evt.ConnectionId, status) ); if (mIsServer) { if (userConnected != null) { userConnected(evt.ConnectionId); } } } break; case NetEventType.ConnectionFailed: { //Outgoing connection failed. Inform the user. Debug.Log("Connection failed: " + channel); Reset(); Invoke("ConnectToChannel", 5.0f); } break; case NetEventType.Disconnected: { foreach (KeyValuePair <ConnectionId, ConnectionStatus> p in mConnections) { if (evt.ConnectionId.Equals(p.Key)) { mConnections.Remove(p); break; } } //A connection was disconnected Debug.Log("Local Connection ID " + evt.ConnectionId + " disconnected"); if (mIsServer == false) { //Reset(); } else { //other users left? inform them if (mConnections.Count > 0) { //SendString(userLeftMsg); } } } break; case NetEventType.ReliableMessageReceived: case NetEventType.UnreliableMessageReceived: { HandleIncommingData(ref evt); } break; } } //finish this update by flushing the messages out if the network wasn't destroyed during update if (mNetwork != null) { mNetwork.Flush(); } } }
protected override void ReadMessages() { if (_webrtcNetwork == null) { return; } _webrtcNetwork.Update(); NetworkEvent evt; while (_webrtcNetwork != null && _webrtcNetwork.Dequeue(out evt)) { switch (evt.Type) { case NetEventType.UnreliableMessageReceived: case NetEventType.ReliableMessageReceived: var id = NetworkReceivedPacket(new ArraySegment <byte>(evt.MessageData.Buffer, evt.MessageData.Offset, evt.MessageData.ContentLength)); if (id.HasValue) { ReceiveHandshakeP2P(id.Value, new PureP2PPeer(evt.ConnectionId)); } break; case NetEventType.NewConnection: if (evt.ConnectionId == _serverConnectionId) { Connected(); } else { SendHandshakeP2P(evt.ConnectionId); } break; case NetEventType.ServerClosed: Disconnect(); break; case NetEventType.Disconnected: if (evt.ConnectionId == _serverConnectionId) { Disconnect(); } break; case NetEventType.ConnectionFailed: if (evt.ConnectionId == _serverConnectionId) { FatalError("Failed to connect to server"); } else { Log.Warn("Failed connection: {0}", evt.ConnectionId); } break; case NetEventType.ServerInitFailed: case NetEventType.FatalError: FatalError(string.Format("WebRTC Fatal event: `{0}`", evt.Type)); break; case NetEventType.ServerInitialized: case NetEventType.Invalid: case NetEventType.Warning: case NetEventType.Log: Log.Debug("Ignoring WebRTC `{0}` message", evt.Type); break; default: throw new ArgumentOutOfRangeException(); } } }
private void FixedUpdate() { //check if the network was created if (mNetwork != null) { //sync all units before processing events if (batch.Count > 0) { SendBatch(); } mNetwork.Update(); NetworkEvent evt; while (mNetwork != null && mNetwork.Dequeue(out evt)) { switch (evt.Type) { case NetEventType.ServerInitialized: //server initialized message received state.isServer = true; // currently the server is always 0 networkID = 0; string address = evt.Info; state.gui.roomID.text = "Room ID: " + address; Debug.Log("Server started. Address: " + address); lobbyAddress = address; state.gui.SetUnitIconPosition(networkID); state.gui.MapSelectMenu(); state.gui.mapSelect.init(address); break; case NetEventType.ServerInitFailed: state.isServer = false; Debug.Log("Server start failed."); Reset(); break; case NetEventType.ServerClosed: state.isServer = false; state.LeaveGame(); Debug.Log("Server closed. No incoming connections possible until restart."); state.gui.ConnectMenu(); break; case NetEventType.NewConnection: ConnectionId connection = evt.ConnectionId; Debug.Log("new conection: " + connection.id.ToString()); if (!state.isServer && serverID == ConnectionId.INVALID) { //clients unconditionally connect to their server and wait for a response. mConnections.Add(connection); serverID = connection; } else if (state.inGame) { mConnections.Add(connection); SendMessage(new RefuseConnection { reason = "The game you tried to join has already begun." }, evt.ConnectionId.id, false); mConnections.Remove(connection); } else if (state.isServer) { bool openSlot = false; //check if there is an open slot in the playerSlots list for (short i = 0; i < playerSlots.Length; i++) { //if there is, occupy that slot with the connectionID of the client who connected //and add the connection to our connections list. if (playerSlots[i] == 0) { mConnections.Add(connection); openSlot = true; playerSlots[i] = evt.ConnectionId.id; SendMessage(new AssignClient { playerID = (short)(i + 1), address = lobbyAddress }, evt.ConnectionId.id, false); break; } } //if there was not an open slot, refuse the connection if (!openSlot) { mConnections.Add(connection); SendMessage(new RefuseConnection { reason = "The lobby you tried to join was full." }, evt.ConnectionId.id, false); mConnections.Remove(connection); } } break; case NetEventType.ConnectionFailed: //Outgoing connection failed. Inform the user. Debug.Log("Connection failed"); Reset(); break; case NetEventType.Disconnected: // A connection was disconnected Debug.Log("Local Connection ID " + evt.ConnectionId + " disconnected"); if (state.isServer) { if (state.inGame) { for (short i = 0; i < playerSlots.Length; i++) { if (playerSlots[i] == evt.ConnectionId.id) { Reset(); break; } } } else { for (short i = 0; i < playerSlots.Length; i++) { if (playerSlots[i] == evt.ConnectionId.id) { mConnections.Remove(evt.ConnectionId); state.gui.mapSelect.RecieveDisconnected((short)(i + 1)); playerSlots[i] = 0; break; } } } } else { if (evt.ConnectionId.id == serverID.id) { Reset(); state.gui.Cleanup(); } } break; case NetEventType.ReliableMessageReceived: case NetEventType.UnreliableMessageReceived: //Debug.Log ("Recieved message: " + evt); HandleIncommingMessage(ref evt); break; } } //finish this update by flushing the messages out if the network wasn't destroyed during update if (mNetwork != null) { mNetwork.Flush(); } } }
// Esta función realiza la función de leer que mensajes les llega desde la red... private void HandleNetwork() { if (mNetwork != null) { mNetwork.Update(); NetworkEvent evt; while (mNetwork != null && mNetwork.Dequeue(out evt)) { switch (evt.Type) { // El servidor de señalización informa de que se hace creado la sala de setreaming.... case NetEventType.ServerInitialized: { mIsServer = true; address = evt.Info; Debug.Log("Server started. Address: " + address); Debug.Log("ConnectionId: " + evt.ConnectionId); // Pone a true la variable isSending GetComponent <PlayerCode>().CmdIsSending(); } break; // El servidor de señalización informa de que ha habido un problema al crear la sala.... case NetEventType.ServerInitFailed: { mIsServer = false; Reset(); } break; // El servidor de señalización informa de que la sala se ha cerrado.... case NetEventType.ServerClosed: { mIsServer = false; // Pone a false la variable isSending GetComponent <PlayerCode>().CmdNotSending(); isTransmitter = false; } break; // El servidor de señalización informa de que se ha añadido alguien a la sala... case NetEventType.NewConnection: { mConnections.Add(evt.ConnectionId); // Pone a true la variable isBusy GetComponent <PlayerCode>().CmdIsBusy(); if (!mIsServer) { Append(DateTime.Now.ToString() + ": Te has unido a una sala."); isConnectedAsClient = true; } else { Append(DateTime.Now.ToString() + ": Un jugador se ha unido a tu sala."); } } break; // El servidor de señalización informa de que no se ha podido el jugador a una sala de streaming... case NetEventType.ConnectionFailed: { Reset(); DesactivateCamera(); } break; // El servidor de señalización informa de que se ha abandonado la sala... case NetEventType.Disconnected: { mConnections.Remove(evt.ConnectionId); if (!mIsServer) { Reset(); isConnectedAsClient = false; } else { Append(DateTime.Now.ToString() + ": Se han desconectado de tu sala."); } // Pone a false la variable isBusy GetComponent <PlayerCode>().CmdNotBusy(); isTransmitter = false; } break; // Canal RELIABLE del asset, aquí se llamará a la función de recepción de audio... case NetEventType.ReliableMessageReceived: { HandleIncommingStreamingAudio(ref evt); } break; // Canal UNRELIABLE del asset, aquí se llamará a la función de recepción de vídeo y mensajes... case NetEventType.UnreliableMessageReceived: { HandleIncommingStreamingVideoAndText(ref evt); } break; } } //finish this update by flushing the messages out if the network wasn't destroyed during update if (mNetwork != null) { mNetwork.Flush(); } } }