private static void EndBroadcastReceive(IAsyncResult asyncResult) { //Extract UDPClient from Async State BroadcastTransmissionState broadcastState = (BroadcastTransmissionState)asyncResult.AsyncState; Socket broadcaster = broadcastState.broadcaster; try { //End receiving data int receivedSize = broadcaster.EndReceiveFrom(asyncResult, ref broadcastState.senderEp); if (receivedSize > 0) { //Get Message flag and process accordingly Packet receivedPacket = new Packet(broadcastState.data, receivedSize); //Ignore packets that are not valid in our broadcast routines if (receivedPacket.flag == Packet.TypeFlag.BROADCAST) { BroadcastPacket broadcastPacket = new BroadcastPacket(receivedPacket, (IPEndPoint)broadcastState.senderEp); lock (broadcastDataQueueLock) { broadcastDataQueue.Enqueue(broadcastPacket); } } } //Keep receiving data broadcaster.BeginReceiveFrom(broadcastState.data, 0, NUUtilities.MTU, SocketFlags.None, ref broadcastState.senderEp, new AsyncCallback(EndBroadcastReceive), broadcastState); } catch (Exception ex) { if (ex.GetType() == typeof(ObjectDisposedException) || broadcaster == null || !broadcaster.IsBound) { return; } Debug.LogError("Error occurred receiving broadcast packet: " + ex.Message); //Keep receiving data (because one connection error should not eliminate other responses) broadcaster.BeginReceiveFrom(broadcastState.data, 0, NUUtilities.MTU, SocketFlags.None, ref broadcastState.senderEp, new AsyncCallback(EndBroadcastReceive), broadcastState); } }
internal static void ProcessQueues() { m_onBroadcastResponse = NUUtilities.SanitizeAction(m_onBroadcastResponse); m_onConnected = NUUtilities.SanitizeAction(m_onConnected); m_onConnectionFailed = NUUtilities.SanitizeAction(m_onConnectionFailed); m_onDisconnected = NUUtilities.SanitizeAction(m_onDisconnected); m_onServerDisconnected = NUUtilities.SanitizeAction(m_onServerDisconnected); m_onConnectionTimeout = NUUtilities.SanitizeAction(m_onConnectionTimeout); m_onPacketReceived = NUUtilities.SanitizeAction(m_onPacketReceived); if (hasDisconnected) { //Reset flag hasDisconnected = false; bool connectionFailed = !connected; //Destroy client component object if (clientComponent != null) { GameObject.Destroy(clientComponent.gameObject); clientComponent = null; } //Clear dataQueue dataQueue.Clear(); //Clear sequential structures seqDataList.Clear(); lastPacketId = -1; //Clear MultiPart Buffers multiPartBuffers.Clear(); //Disconnect client instance if (client != null) { client.Disconnect(); } //Do proper callbacks if (calledDisconnect) { calledDisconnect = false; if (m_onDisconnected != null) { m_onDisconnected(); } return; } if (serverDisconnected) { serverDisconnected = false; if (m_onServerDisconnected != null) { m_onServerDisconnected(serverDisconnectMsg); } } if (connectionFailed) { if (m_onConnectionFailed != null) { m_onConnectionFailed(); } return; } else { if (m_onConnectionTimeout != null) { m_onConnectionTimeout(); } } } if (hasConnected) { //Reset flag hasConnected = false; //Do proper callback if (m_onConnected != null) { m_onConnected(); } } //Process broadcast callbacks if (broadcaster != null) { //Process broadcast callbacks lock (broadcastDataQueueLock) { while (broadcastDataQueue.Count > 0) { BroadcastPacket packet = broadcastDataQueue.Dequeue(); //Do proper callback if (m_onBroadcastResponse != null) { m_onBroadcastResponse(packet); } } } } if (!connected) { return; } //Process packet callbacks lock (dataQueueLock) { while (dataQueue.Count > 0) { Packet packet = dataQueue.Dequeue(); //Do proper callback if (m_onPacketReceived != null) { m_onPacketReceived(packet); } } } }