/// <summary> /// The handler that we wish to execute when we receive a message packet. /// </summary> /// <param name="header">The associated packet header.</param> /// <param name="connection">The connection used for the incoming packet</param> /// <param name="incomingString">The incoming data converted to a string</param> private static void HandleIncomingMessagePacket(PacketHeader header, Connection connection, string incomingString) { Console.WriteLine("\n ... Incoming message from " + connection.ToString() + " saying '" + incomingString + "'."); }
/// <summary> /// Performs whatever functions we might so desire when an existing connection is closed. /// </summary> /// <param name="connection">The closed connection</param> private void HandleConnectionClosed(Connection connection) { //We are going to write a message to the chat history when a connection disconnects //We perform the following within a lock in case multiple connections disconnect simultaneously lock (lastPeerMessageDict) { //Get the remoteIdentifier from the closed connection //This a unique GUID which can be used to identify peers ShortGuid remoteIdentifier = connection.ConnectionInfo.NetworkIdentifier; //If at any point we received a message with a matching identifier we can //include the peer name in the disconnection message. if (lastPeerMessageDict.ContainsKey(remoteIdentifier)) AppendLineToChatHistory("Connection with '" + lastPeerMessageDict[remoteIdentifier].SourceName + "' has been closed."); else AppendLineToChatHistory("Connection with '" + connection.ToString() + "' has been closed."); //Last thing is to remove this peer from our message history lastPeerMessageDict.Remove(connection.ConnectionInfo.NetworkIdentifier); } }
/// <summary> /// Add all listener specific packet handlers to the provided connection /// </summary> /// <param name="connection">The connection to which packet handlers should be added</param> internal void AddListenerPacketHandlersToConnection(Connection connection) { lock (delegateLocker) { foreach (string packetType in incomingPacketHandlers.Keys) { foreach (IPacketTypeHandlerDelegateWrapper handler in incomingPacketHandlers[packetType]) { connection.AppendIncomingPacketHandler(packetType, handler, incomingPacketUnwrappers[packetType].Options); } } } if (NetworkComms.LoggingEnabled) { NetworkComms.Logger.Info("Appended connection specific packet handlers from listener '" + ToString() + "' to connection '" + connection.ToString() + "'."); } }
/// <summary> /// UDP - The response to a DFS_KnownPeersRequest /// </summary> /// <param name="packetHeader"></param> /// <param name="connection"></param> /// <param name="peerList"></param> private static void KnownPeersUpdate(PacketHeader packetHeader, Connection connection, KnownPeerEndPoints peerList) { try { DistributedItem currentItem = null; if (DFS.loggingEnabled) DFS.Logger.Trace("Handling 'DFS_KnownPeersUpdate' from " + connection.ToString() + " containing " + peerList.PeerEndPoints.Length + " peers."); lock (globalDFSLocker) { if (swarmedItemsDict.ContainsKey(peerList.ItemChecksm)) currentItem = swarmedItemsDict[peerList.ItemChecksm]; } if (currentItem != null) { //If we have some unknown peers we can request an update from them as well foreach (string peerContactInfo in peerList.PeerEndPoints) { try { IPEndPoint peerEndPoint = IPTools.ParseEndPointFromString(peerContactInfo); //We don't want to contact existing peers as this has already been done by SwarmChunkAvailability.UpdatePeerAvailability //We don't want to contact ourselves and for now that includes anything having the same ip as us if (!currentItem.SwarmChunkAvailability.PeerExistsInSwarm(peerEndPoint) && currentItem.SwarmChunkAvailability.PeerContactAllowed(ShortGuid.Empty, peerEndPoint, false)) { currentItem.AddBuildLogLine("Contacting " + peerContactInfo + " for a DFS_ChunkAvailabilityRequest from within KnownPeersUpdate."); UDPConnection.SendObject("DFS_ChunkAvailabilityRequest", peerList.ItemChecksm, peerEndPoint, nullCompressionSRO); } } catch (CommsException) { if (DFS.loggingEnabled) DFS.Logger.Trace("Removing " + peerContactInfo + " from item swarm due to CommsException."); currentItem.AddBuildLogLine("Removing " + peerContactInfo + " from item swarm due to CommsException."); } catch (Exception ex) { LogTools.LogException(ex, "UpdatePeerChunkAvailabilityError_3"); } } } else { if (DFS.loggingEnabled) DFS.Logger.Trace("Received 'DFS_KnownPeersUpdate' data for item which does not exist locally."); } } catch (CommsException) { //LogTools.LogException(e, "CommsError_IncomingChunkAvailabilityRequest"); } catch (Exception e) { LogTools.LogException(e, "Error_KnownPeersUpdate"); } }