/// <summary> /// Send a null packet (1 byte) to the remotEndPoint. Helps keep the TCP connection alive while ensuring the bandwidth usage is an absolute minimum. If an exception is thrown the connection will be closed. /// </summary> protected override void SendNullPacket() { try { //Only once the connection has been established do we send null packets if (ConnectionInfo.ConnectionState == ConnectionState.Established) { //Multiple threads may try to send packets at the same time so we need this lock to prevent a thread cross talk lock (sendLocker) { if (NetworkComms.LoggingEnabled) { NetworkComms.Logger.Trace("Sending null packet to " + ConnectionInfo); } //Send a single 0 byte double maxSendTimePerKB = double.MaxValue; if (!NetworkComms.DisableConnectionSendTimeouts) { if (SendTimesMSPerKBCache.Count > MinNumSendsBeforeConnectionSpecificSendTimeout) { maxSendTimePerKB = Math.Max(MinimumMSPerKBSendTimeout, SendTimesMSPerKBCache.CalculateMean() + NumberOfStDeviationsForWriteTimeout * SendTimesMSPerKBCache.CalculateStdDeviation()); } else { maxSendTimePerKB = DefaultMSPerKBSendTimeout; } } #if WINDOWS_PHONE var stream = socket.OutputStream.AsStreamForWrite(); StreamWriteWithTimeout.Write(new byte[] { 0 }, 1, stream, 1, maxSendTimePerKB, MinSendTimeoutMS); stream.Flush(); #else StreamWriteWithTimeout.Write(new byte[] { 0 }, 1, tcpClientNetworkStream, 1, maxSendTimePerKB, MinSendTimeoutMS); #endif //Update the traffic time after we have written to netStream ConnectionInfo.UpdateLastTrafficTime(); } } //If the connection is shutdown we should call close if (ConnectionInfo.ConnectionState == ConnectionState.Shutdown) { CloseConnection(false, -8); } } catch (Exception) { CloseConnection(true, 19); } }
/// <summary> /// Sends a null packet using UDP /// </summary> protected override void SendNullPacket() { //We cant send a null packet to the IPAddress.Any address if (!ConnectionInfo.RemoteEndPoint.Address.Equals(IPAddress.Any)) { try { if (NetworkComms.LoggingEnabled) { NetworkComms.Logger.Trace("Sending null packet to " + ConnectionInfo); } #if WINDOWS_PHONE //Send a single 0 byte var getStreamTask = socket.GetOutputStreamAsync(new HostName(ConnectionInfo.RemoteEndPoint.Address.ToString()), ConnectionInfo.RemoteEndPoint.Port.ToString()).AsTask(); getStreamTask.Wait(); var outputStream = getStreamTask.Result; outputStream.WriteAsync(WindowsRuntimeBufferExtensions.AsBuffer(new byte[] { 0 })).AsTask().Wait(); outputStream.FlushAsync().AsTask().Wait(); #else //Send a single 0 byte udpClientThreadSafe.Send(new byte[] { 0 }, 1, ConnectionInfo.RemoteEndPoint); #endif //Update the traffic time after we have written to netStream ConnectionInfo.UpdateLastTrafficTime(); //If the connection is shutdown we should call close if (ConnectionInfo.ConnectionState == ConnectionState.Shutdown) { CloseConnection(false, -9); } } catch (Exception) { CloseConnection(true, 40); } } }
/// <summary> /// Send the provided packet to the remoteEndPoint. Waits for receive confirmation if required. /// </summary> /// <param name="packet">The packet to send</param> /// <param name="packetSequenceNumber">The sequence number of the packet sent</param> internal void SendPacket(Packet packet, out long packetSequenceNumber) { if (NetworkComms.LoggingEnabled) { string packetDataMD5 = ""; if (packet.PacketHeader.ContainsOption(PacketHeaderStringItems.CheckSumHash)) { packetDataMD5 = packet.PacketHeader.GetOption(PacketHeaderStringItems.CheckSumHash); } NetworkComms.Logger.Trace("Entering packet send of '" + packet.PacketHeader.PacketType + "' packetType to " + ConnectionInfo + (packetDataMD5 == "" ? "" : ". PacketCheckSum=" + packetDataMD5)); } //Multiple threads may try to send packets at the same time so wait one at a time here lock (sendLocker) { //We don't allow sends on a closed connection if (ConnectionInfo.ConnectionState == ConnectionState.Shutdown) { throw new CommunicationException("Attempting to send packet on connection which has been closed or is currently closing."); } //Set packet sequence number inside sendLocker //Increment the global counter as well to ensure future connections with the same host can not create duplicates Interlocked.Increment(ref NetworkComms.totalPacketSendCount); packetSequenceNumber = packetSequenceCounter++; packet.PacketHeader.SetOption(PacketHeaderLongItems.PacketSequenceNumber, packetSequenceNumber); string confirmationCheckSum = ""; AutoResetEvent confirmationWaitSignal = new AutoResetEvent(false); bool remotePeerDisconnectedDuringWait = false; #region Delegates //Specify a delegate we may use if we require receive confirmation NetworkComms.PacketHandlerCallBackDelegate <string> confirmationDelegate = (packetHeader, connectionInfo, incomingString) => { //if (connectionInfo.NetworkIdentifier == this.ConnectionInfo.NetworkIdentifier && connectionInfo.RemoteEndPoint == this.ConnectionInfo.RemoteEndPoint) //{ confirmationCheckSum = incomingString; confirmationWaitSignal.Set(); //} }; //We use the following delegate to quickly force a response timeout if the remote end disconnects during a send/wait NetworkComms.ConnectionEstablishShutdownDelegate ConfirmationShutDownDelegate = (connectionInfo) => { //if (connectionInfo.NetworkIdentifier == this.ConnectionInfo.NetworkIdentifier && connectionInfo.RemoteEndPoint == this.ConnectionInfo.RemoteEndPoint) //{ remotePeerDisconnectedDuringWait = true; confirmationWaitSignal.Set(); //} }; #endregion try { #region Prepare For Confirmation and Possible Validation //Add the confirmation handler if required if (packet.PacketHeader.ContainsOption(PacketHeaderStringItems.ReceiveConfirmationRequired)) { AppendIncomingPacketHandler(Enum.GetName(typeof(ReservedPacketType), ReservedPacketType.Confirmation), confirmationDelegate, NetworkComms.InternalFixedSendReceiveOptions); AppendShutdownHandler(ConfirmationShutDownDelegate); } //If this packet is not a checkSumFailResend if (NetworkComms.EnablePacketCheckSumValidation && packet.PacketHeader.PacketType != Enum.GetName(typeof(ReservedPacketType), ReservedPacketType.CheckSumFailResend)) { //We only want to keep packets when they are under some provided theshold //otherwise this becomes a quick 'memory leak' if (packet.PacketData.Length < NetworkComms.CheckSumMismatchSentPacketCacheMaxByteLimit) { lock (sentPacketsLocker) { var hash = packet.PacketHeader.GetOption(PacketHeaderStringItems.CheckSumHash); if (!sentPackets.ContainsKey(hash)) { sentPackets.Add(hash, new SentPacket(packet)); } } } } #endregion SendPacketSpecific(packet); #region SentPackets Cleanup //If sent packets is greater than 40 we delete anything older than a minute lock (sentPacketsLocker) { if ((DateTime.Now - NetworkComms.LastSentPacketCacheCleanup).TotalMinutes > NetworkComms.MinimumSentPacketCacheTimeMinutes / 2) { Dictionary <string, SentPacket> newSentPackets = new Dictionary <string, SentPacket>(); DateTime thresholdTime = DateTime.Now.AddMinutes(-NetworkComms.MinimumSentPacketCacheTimeMinutes); foreach (var storedPacket in sentPackets) { if (storedPacket.Value.SentPacketCreationTime >= thresholdTime) { newSentPackets.Add(storedPacket.Key, storedPacket.Value); } } sentPackets = newSentPackets; NetworkComms.LastSentPacketCacheCleanup = DateTime.Now; } } #endregion #region Wait For Confirmation If Required //If we required receive confirmation we now wait for that confirmation if (packet.PacketHeader.ContainsOption(PacketHeaderStringItems.ReceiveConfirmationRequired)) { if (NetworkComms.LoggingEnabled) { NetworkComms.Logger.Trace(" ... waiting for receive confirmation packet."); } if (!(confirmationWaitSignal.WaitOne(NetworkComms.PacketConfirmationTimeoutMS))) { throw new ConfirmationTimeoutException("Confirmation packet timeout."); } if (remotePeerDisconnectedDuringWait) { throw new ConfirmationTimeoutException("Remote end closed connection before confirmation packet was returned."); } else { if (NetworkComms.LoggingEnabled) { NetworkComms.Logger.Trace(" ... confirmation packet received."); } } } #endregion //Update the traffic time as late as possible incase there is a problem ConnectionInfo.UpdateLastTrafficTime(); } catch (ConfirmationTimeoutException) { //Confirmation timeout there is no need to close the connection as this //does not neccessarily mean there is a conneciton problem throw; } catch (CommunicationException) { //We close the connection due to communication exceptions CloseConnection(true, 47); throw; } catch (TimeoutException ex) { //We close the connection due to communication exceptions if (NetworkComms.LoggingEnabled) { NetworkComms.Logger.Warn("Timeout exception for connection " + this.ConnectionInfo + (ex.Message != null ? ". " + ex.Message : ".")); } CloseConnection(true, 48); throw new ConnectionSendTimeoutException(ex.ToString()); } catch (Exception ex) { //We close the connection due to communication exceptions CloseConnection(true, 49); throw new CommunicationException(ex.ToString()); } finally { if (packet.PacketHeader.ContainsOption(PacketHeaderStringItems.ReceiveConfirmationRequired)) { //Cleanup our delegates RemoveIncomingPacketHandler(Enum.GetName(typeof(ReservedPacketType), ReservedPacketType.Confirmation), confirmationDelegate); RemoveShutdownHandler(ConfirmationShutDownDelegate); } } } if (NetworkComms.LoggingEnabled) { NetworkComms.Logger.Trace("Completed packet send of '" + packet.PacketHeader.PacketType + "' packetType to " + ConnectionInfo); } }
/// <summary> /// Asynchronous incoming connection data delegate /// </summary> /// <param name="ar">The call back state object</param> void IncomingTCPPacketHandler(IAsyncResult ar) { //Initialised with true so that logic still works in WP8 bool dataAvailable = true; #if !WINDOWS_PHONE //Incoming data always gets handled in a timeCritical fashion at this point Thread.CurrentThread.Priority = NetworkComms.timeCriticalThreadPriority; //int bytesRead; #endif try { #if WINDOWS_PHONE var stream = ar.AsyncState as Stream; var count = stream.EndRead(ar); totalBytesRead = count + totalBytesRead; #else NetworkStream netStream = (NetworkStream)ar.AsyncState; if (!netStream.CanRead) { throw new ObjectDisposedException("Unable to read from stream."); } totalBytesRead = netStream.EndRead(ar) + totalBytesRead; dataAvailable = netStream.DataAvailable; #endif if (totalBytesRead > 0) { ConnectionInfo.UpdateLastTrafficTime(); //If we have read a single byte which is 0 and we are not expecting other data if (totalBytesRead == 1 && dataBuffer[0] == 0 && packetBuilder.TotalBytesExpected - packetBuilder.TotalBytesCached == 0) { if (NetworkComms.LoggingEnabled) { NetworkComms.Logger.Trace(" ... null packet removed in IncomingPacketHandler() from " + ConnectionInfo + ". 1"); } } else { //if (NetworkComms.LoggingEnabled) NetworkComms.Logger.Trace(" ... " + totalBytesRead.ToString() + " bytes added to packetBuilder."); //If there is more data to get then add it to the packets lists; packetBuilder.AddPartialPacket(totalBytesRead, dataBuffer); #if !WINDOWS_PHONE //If we have more data we might as well continue reading syncronously //In order to deal with data as soon as we think we have sufficient we will leave this loop while (dataAvailable && packetBuilder.TotalBytesCached < packetBuilder.TotalBytesExpected) { int bufferOffset = 0; //We need a buffer for our incoming data //First we try to reuse a previous buffer if (packetBuilder.TotalPartialPacketCount > 0 && packetBuilder.NumUnusedBytesMostRecentPartialPacket() > 0) { dataBuffer = packetBuilder.RemoveMostRecentPartialPacket(ref bufferOffset); } else { //If we have nothing to reuse we allocate a new buffer dataBuffer = new byte[NetworkComms.ReceiveBufferSizeBytes]; } totalBytesRead = netStream.Read(dataBuffer, bufferOffset, dataBuffer.Length - bufferOffset) + bufferOffset; if (totalBytesRead > 0) { ConnectionInfo.UpdateLastTrafficTime(); //If we have read a single byte which is 0 and we are not expecting other data if (totalBytesRead == 1 && dataBuffer[0] == 0 && packetBuilder.TotalBytesExpected - packetBuilder.TotalBytesCached == 0) { if (NetworkComms.LoggingEnabled) { NetworkComms.Logger.Trace(" ... null packet removed in IncomingPacketHandler() from " + ConnectionInfo + ". 2"); } //LastTrafficTime = DateTime.Now; } else { //if (NetworkComms.LoggingEnabled) NetworkComms.Logger.Trace(" ... " + totalBytesRead.ToString() + " bytes added to packetBuilder for connection with " + ConnectionInfo + ". Cached " + packetBuilder.TotalBytesCached.ToString() + "B, expecting " + packetBuilder.TotalBytesExpected.ToString() + "B."); packetBuilder.AddPartialPacket(totalBytesRead, dataBuffer); dataAvailable = netStream.DataAvailable; } } else { break; } } #endif } } if (packetBuilder.TotalBytesCached > 0 && packetBuilder.TotalBytesCached >= packetBuilder.TotalBytesExpected) { //Once we think we might have enough data we call the incoming packet handle handoff //Should we have a complete packet this method will start the appriate task //This method will now clear byes from the incoming packets if we have received something complete. IncomingPacketHandleHandOff(packetBuilder); } if (totalBytesRead == 0 && (!dataAvailable || ConnectionInfo.ConnectionState == ConnectionState.Shutdown)) { CloseConnection(false, -2); } else { //We need a buffer for our incoming data //First we try to reuse a previous buffer if (packetBuilder.TotalPartialPacketCount > 0 && packetBuilder.NumUnusedBytesMostRecentPartialPacket() > 0) { dataBuffer = packetBuilder.RemoveMostRecentPartialPacket(ref totalBytesRead); } else { //If we have nothing to reuse we allocate a new buffer dataBuffer = new byte[NetworkComms.ReceiveBufferSizeBytes]; totalBytesRead = 0; } #if WINDOWS_PHONE stream.BeginRead(dataBuffer, totalBytesRead, dataBuffer.Length - totalBytesRead, IncomingTCPPacketHandler, stream); #else netStream.BeginRead(dataBuffer, totalBytesRead, dataBuffer.Length - totalBytesRead, IncomingTCPPacketHandler, netStream); #endif } } catch (IOException) { CloseConnection(true, 12); } catch (ObjectDisposedException) { CloseConnection(true, 13); } catch (SocketException) { CloseConnection(true, 14); } catch (InvalidOperationException) { CloseConnection(true, 15); } catch (Exception ex) { NetworkComms.LogError(ex, "Error_TCPConnectionIncomingPacketHandler"); CloseConnection(true, 31); } #if !WINDOWS_PHONE Thread.CurrentThread.Priority = ThreadPriority.Normal; #endif }
/// <summary> /// Synchronous incoming connection data worker /// </summary> void IncomingTCPDataSyncWorker() { bool dataAvailable = false; try { while (true) { if (ConnectionInfo.ConnectionState == ConnectionState.Shutdown) { break; } int bufferOffset = 0; //We need a buffer for our incoming data //First we try to reuse a previous buffer if (packetBuilder.TotalPartialPacketCount > 0 && packetBuilder.NumUnusedBytesMostRecentPartialPacket() > 0) { dataBuffer = packetBuilder.RemoveMostRecentPartialPacket(ref bufferOffset); } else { //If we have nothing to reuse we allocate a new buffer dataBuffer = new byte[NetworkComms.ReceiveBufferSizeBytes]; } //We block here until there is data to read //When we read data we read until method returns or we fill the buffer length totalBytesRead = tcpClientNetworkStream.Read(dataBuffer, bufferOffset, dataBuffer.Length - bufferOffset) + bufferOffset; //Check to see if there is more data ready to be read dataAvailable = tcpClientNetworkStream.DataAvailable; //If we read any data it gets handed off to the packetBuilder if (totalBytesRead > 0) { ConnectionInfo.UpdateLastTrafficTime(); //If we have read a single byte which is 0 and we are not expecting other data if (totalBytesRead == 1 && dataBuffer[0] == 0 && packetBuilder.TotalBytesExpected - packetBuilder.TotalBytesCached == 0) { if (NetworkComms.LoggingEnabled) { NetworkComms.Logger.Trace(" ... null packet removed in IncomingDataSyncWorker() from " + ConnectionInfo + "."); } } else { if (NetworkComms.LoggingEnabled) { NetworkComms.Logger.Trace(" ... " + totalBytesRead.ToString() + " bytes added to packetBuilder for connection with " + ConnectionInfo + ". Cached " + packetBuilder.TotalBytesCached.ToString() + "B, expecting " + packetBuilder.TotalBytesExpected.ToString() + "B."); } packetBuilder.AddPartialPacket(totalBytesRead, dataBuffer); } } else if (totalBytesRead == 0 && (!dataAvailable || ConnectionInfo.ConnectionState == ConnectionState.Shutdown)) { //If we read 0 bytes and there is no data available we should be shutting down CloseConnection(false, -10); break; } //If we have read some data and we have more or equal what was expected we attempt a data handoff if (packetBuilder.TotalBytesCached > 0 && packetBuilder.TotalBytesCached >= packetBuilder.TotalBytesExpected) { IncomingPacketHandleHandOff(packetBuilder); } } } //On any error here we close the connection catch (NullReferenceException) { CloseConnection(true, 7); } catch (IOException) { CloseConnection(true, 8); } catch (ObjectDisposedException) { CloseConnection(true, 9); } catch (SocketException) { CloseConnection(true, 10); } catch (InvalidOperationException) { CloseConnection(true, 11); } catch (Exception ex) { NetworkComms.LogError(ex, "Error_TCPConnectionIncomingPacketHandler"); CloseConnection(true, 39); } //Clear the listen thread object because the thread is about to end incomingDataListenThread = null; if (NetworkComms.LoggingEnabled) { NetworkComms.Logger.Trace("Incoming data listen thread ending for " + ConnectionInfo); } }