Permalink
Browse files

Prevented duplicate packet type strings for send receive methods. Pre…

…vented log file name being too large when errors are thrown for RPC methods. Added some usefull get methods to packet headers for commonly used options.
  • Loading branch information...
MarcF
MarcF committed Mar 7, 2014
1 parent d71ea32 commit 69255d2a2142463066d89f95d75970944fcb0ae4
@@ -29,6 +29,7 @@
using InTheHand.Net;
using NetworkCommsDotNet.Connections;
using NetworkCommsDotNet.Connections.Bluetooth;
using NetworkCommsDotNet.Connections.TCP;
namespace DebugTests
{
@@ -40,23 +41,19 @@ static class DebugTest
public static void RunExample()
{
//Get the serializer and data processors
DataSerializer dataSerializer = DPSManager.GetDataSerializer<ProtobufSerializer>();
List<DataProcessor> dataProcessors = new List<DataProcessor>() { DPSManager.GetDataProcessor<SharpZipLibCompressor.SharpZipLibGzipCompressor>() };
Dictionary<string, string> processorOptions = new Dictionary<string,string>();
NetworkComms.AppendGlobalIncomingPacketHandler<string>("Data", (header, connection, message) =>
{
Console.WriteLine("Server received - " + message);
connection.SendObject(header.RequestedReturnPacketType, "replyMessage");
});
//Recreate the issue when sending a null object
//We initialise an empty stream to pass through the serialisation stages
MemoryStream emptyStream = new MemoryStream(new byte[0], 0, 0, false, true);
object objectToSerialise = new StreamTools.StreamSendWrapper(new StreamTools.ThreadSafeStream(emptyStream, true));
Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Any, 10000));
//Serializer as we do we a real send
StreamTools.StreamSendWrapper result = dataSerializer.SerialiseDataObject(objectToSerialise, dataProcessors, processorOptions);
Connection conn = TCPConnection.GetConnection(new ConnectionInfo(IPTools.ParseEndPointFromString("::1:10000")));
var reply = conn.SendReceiveObject<string, string>("Data", "Data-Response", 1000, "hello server");
//Get the bytes and deserialize
byte[] bytes = result.ThreadSafeStream.ToArray();
//If this works we expect the original string to be default(string)
string originalString = dataSerializer.DeserialiseDataObject<string>(bytes, dataProcessors, processorOptions);
Console.WriteLine("Client done!");
Console.ReadKey();
}
}
}
@@ -55,7 +55,7 @@ static void Main(string[] args)
DistributedFileSystem.DFS.EnableLogging(logger);
}
//DebugTest.RunExample();
DebugTest.RunExample();
//DebugTest.Test();
//BasicSend.RunExample();
//AliveTest.RunExample();
@@ -66,7 +66,7 @@ static void Main(string[] args)
//NumConnectionLoadTest.RunExample();
//ThreadPoolTest.RunExample();
//SSLTest.RunExample();
NestedPacketTest.RunExample();
//NestedPacketTest.RunExample();
//PeerDiscoveryTest.RunExample();
//SelfConnectTest.RunExample();
@@ -94,7 +94,8 @@ public static class DFS
static DateTime lastChunkCacheCleanup = DateTime.Now;
static object chunkDataCacheLocker = new object();
/// <summary>
/// Temporary storage for chunk data which is awaiting info
/// Temporary storage for chunk data which is awaiting info.
/// This stores data based on the peer guid and packet sequence number
/// </summary>
static Dictionary<ShortGuid, Dictionary<long, ChunkDataWrapper>> chunkDataCache = new Dictionary<ShortGuid, Dictionary<long, ChunkDataWrapper>>();
@@ -1424,7 +1425,7 @@ private static void IncomingChunkInterestReplyInfo(PacketHeader packetHeader, Co
}
else if (incomingReply.ReplyState == ChunkReplyState.DataIncluded)
{
//We have beaten the data, we will add the chunkavailability reply instead and wait, letting the incoming data trigger the handle
//We have beaten the data, we will add the chunk availability reply instead and wait, letting the incoming data trigger the handle
if (!chunkDataCache.ContainsKey(incomingReply.SourceNetworkIdentifier))
chunkDataCache.Add(incomingReply.SourceNetworkIdentifier, new Dictionary<long,ChunkDataWrapper>());
@@ -78,20 +78,21 @@
</ComponentConfigurations>
<NamespaceSummaries>
<NamespaceSummaryItem name="ExamplesConsole" isDocumented="False">Console examples demonstrating the most common uses of the NetworkComms.Net feature set.</NamespaceSummaryItem>
<NamespaceSummaryItem name="NetworkCommsDotNet" isDocumented="True">Base library providing methods to track connections, global configurations, send and receive objects.</NamespaceSummaryItem>
<NamespaceSummaryItem name="QuickLZCompressor" isDocumented="True">Library implementing a DataProcessor which compresses data using QuickLZ.</NamespaceSummaryItem>
<NamespaceSummaryItem name="SharpZipLibCompressor" isDocumented="True">Library implementing a DataProcessor which compresses data using SharpZipLib</NamespaceSummaryItem>
<NamespaceSummaryItem name="RemoteProcedureCalls" isDocumented="True">Contains methods for setting up objects to be called from remote clients as well as the methods to access those objects client side.</NamespaceSummaryItem>
<NamespaceSummaryItem name="(global)" isDocumented="False" />
<NamespaceSummaryItem name="DistributedFileSystem" isDocumented="True">An extension that provides high performance torrent style data distribution.</NamespaceSummaryItem>
<NamespaceSummaryItem name="Examples.ExamplesConsole" isDocumented="False" />
<NamespaceSummaryItem name="NetworkCommsDotNet.Connections" isDocumented="True">Provides base connection functionality and defines structure of connection listeners.</NamespaceSummaryItem>
<NamespaceSummaryItem name="NetworkCommsDotNet.Connections.TCP" isDocumented="True">Implements all aspects of TCP connections.</NamespaceSummaryItem>
<NamespaceSummaryItem name="NetworkCommsDotNet.Connections.UDP" isDocumented="True">Implements all aspects of UDP connections.</NamespaceSummaryItem>
<NamespaceSummaryItem name="NetworkCommsDotNet.DPSBase" isDocumented="True">Provides base functionality required to convert custom objects into byte streams. Includes data processing interfaces which can be used to modify the serialised byte streams.</NamespaceSummaryItem>
<NamespaceSummaryItem name="NetworkCommsDotNet.Tools" isDocumented="True">Large number of tools which can be used to simplify adding network related functionality to your own application</NamespaceSummaryItem>
<NamespaceSummaryItem name="NetworkCommsDotNet.Connections.Bluetooth" isDocumented="True">Implements all aspects of Bluetooth connections.</NamespaceSummaryItem>
<NamespaceSummaryItem name="NetworkCommsDotNet.DPSBase.SevenZipLZMACompressor" isDocumented="True">Data processor that implements the 7Zip LZMA algorithm, usefull for compressioning data.</NamespaceSummaryItem></NamespaceSummaries>
<NamespaceSummaryItem name="NetworkCommsDotNet" isDocumented="True">Base library providing methods to track connections, global configurations, send and receive objects.</NamespaceSummaryItem>
<NamespaceSummaryItem name="QuickLZCompressor" isDocumented="True">Library implementing a DataProcessor which compresses data using QuickLZ.</NamespaceSummaryItem>
<NamespaceSummaryItem name="SharpZipLibCompressor" isDocumented="True">Library implementing a DataProcessor which compresses data using SharpZipLib</NamespaceSummaryItem>
<NamespaceSummaryItem name="RemoteProcedureCalls" isDocumented="True">Contains methods for setting up objects to be called from remote clients as well as the methods to access those objects client side.</NamespaceSummaryItem>
<NamespaceSummaryItem name="(global)" isDocumented="False" />
<NamespaceSummaryItem name="DistributedFileSystem" isDocumented="True">An extension that provides high performance torrent style data distribution.</NamespaceSummaryItem>
<NamespaceSummaryItem name="Examples.ExamplesConsole" isDocumented="False" />
<NamespaceSummaryItem name="NetworkCommsDotNet.Connections" isDocumented="True">Provides base connection functionality and defines structure of connection listeners.</NamespaceSummaryItem>
<NamespaceSummaryItem name="NetworkCommsDotNet.Connections.TCP" isDocumented="True">Implements all aspects of TCP connections.</NamespaceSummaryItem>
<NamespaceSummaryItem name="NetworkCommsDotNet.Connections.UDP" isDocumented="True">Implements all aspects of UDP connections.</NamespaceSummaryItem>
<NamespaceSummaryItem name="NetworkCommsDotNet.DPSBase" isDocumented="True">Provides base functionality required to convert custom objects into byte streams. Includes data processing interfaces which can be used to modify the serialised byte streams.</NamespaceSummaryItem>
<NamespaceSummaryItem name="NetworkCommsDotNet.Tools" isDocumented="True">Large number of tools which can be used to simplify adding network related functionality to your own application</NamespaceSummaryItem>
<NamespaceSummaryItem name="NetworkCommsDotNet.Connections.Bluetooth" isDocumented="True">Implements all aspects of Bluetooth connections.</NamespaceSummaryItem>
<NamespaceSummaryItem name="NetworkCommsDotNet.DPSBase.SevenZipLZMACompressor" isDocumented="True">Data processor that implements the 7Zip LZMA algorithm, usefull for compressioning data.</NamespaceSummaryItem>
</NamespaceSummaries>
<FeedbackEMailAddress>helpfeedback%40networkcomms.net</FeedbackEMailAddress>
<ApiFilter />
<HelpAttributes />
@@ -229,6 +229,9 @@ public void SendObject(string sendingPacketType, out long packetSequenceNumber)
/// <returns>The requested return object</returns>
public returnObjectType SendReceiveObject<sendObjectType, returnObjectType>(string sendingPacketTypeStr, string expectedReturnPacketTypeStr, int returnPacketTimeOutMilliSeconds, sendObjectType sendObject, SendReceiveOptions sendOptions, SendReceiveOptions receiveOptions, out long sentPacketSequenceNumber)
{
if (sendingPacketTypeStr == expectedReturnPacketTypeStr)
throw new ArgumentException("The provided sendingPacketTypeStr and expectedReturnPacketTypeStr parameters must be different.");
returnObjectType returnObject = default(returnObjectType);
bool remotePeerDisconnectedDuringWait = false;
@@ -42,14 +42,14 @@ public enum PacketHeaderLongItems
SerializerProcessors,
/// <summary>
/// The creation time of the packet header.
/// The sequence number for this packet. Each connection maintains a unique counter which is increments on each sent packet. This is a compulsory option.
/// </summary>
PacketCreationTime,
PacketSequenceNumber,
/// <summary>
/// The sequence number for this packet. Each connection maintains a unique counter which is increments on each sent packet.
/// The creation time of the packet header.
/// </summary>
PacketSequenceNumber,
PacketCreationTime,
}
/// <summary>
@@ -121,6 +121,9 @@ public sealed class PacketHeader : IExplicitlySerialize
/// <param name="checkSumHash">An optional field representing the payload checksum</param>
public PacketHeader(string packetTypeStr, long payloadPacketSize, SendReceiveOptions sendReceiveOptions = null, string requestedReturnPacketTypeStr = null, string checkSumHash = null)
{
if (packetTypeStr == requestedReturnPacketTypeStr)
throw new ArgumentException("The provided packetTypeStr and requestedReturnPacketTypeStr parameters must be different.");
longItems = new Dictionary<PacketHeaderLongItems, long>();
stringItems = new Dictionary<PacketHeaderStringItems, string>();
@@ -198,6 +201,71 @@ public string PacketType
get { return stringItems[PacketHeaderStringItems.PacketType]; }
}
/// <summary>
/// The sequence number for this packet
/// </summary>
public long PacketSequenceNumber
{
get { return longItems[PacketHeaderLongItems.PacketSequenceNumber]; }
}
/// <summary>
/// The packet type which should be used for any return packet type. If no return packet type is set returns null.
/// </summary>
public string RequestedReturnPacketType
{
get
{
if (stringItems.ContainsKey(PacketHeaderStringItems.RequestedReturnPacketType))
return stringItems[PacketHeaderStringItems.RequestedReturnPacketType];
else
return null;
}
}
/// <summary>
/// Optional packet identifier. If no packet identifier is set returns null.
/// </summary>
public string PacketIdentifier
{
get
{
if (stringItems.ContainsKey(PacketHeaderStringItems.PacketIdentifier))
return stringItems[PacketHeaderStringItems.PacketIdentifier];
else
return null;
}
}
/// <summary>
/// The network identifier of the packets source peer. If no source network identifier is set returns null.
/// Also see <see cref="Connection.ConnectionInfo.NetworkIdentifier"/>.
/// </summary>
public string SourceNetworkIdentifier
{
get
{
if (stringItems.ContainsKey(PacketHeaderStringItems.SourceNetworkIdentifier))
return stringItems[PacketHeaderStringItems.SourceNetworkIdentifier];
else
return null;
}
}
/// <summary>
/// A checksum corresponding to the payload data. If no checksum is set returns null.
/// </summary>
public string CheckSumHash
{
get
{
if (stringItems.ContainsKey(PacketHeaderStringItems.CheckSumHash))
return stringItems[PacketHeaderStringItems.CheckSumHash];
else
return null;
}
}
/// <summary>
/// Check if a string option has been set.
/// </summary>
@@ -30,6 +30,10 @@ public static void AppendStringToLogFile(string fileName, string logString)
{
try
{
//Catch filenames that are too long
if (fileName.Length > 50)
fileName = fileName.Substring(0, 50);
lock (errorLocker)
{
#if NETFX_CORE
@@ -68,7 +72,9 @@ public static string LogException(Exception ex, string fileName, string optional
lock (errorLocker)
{
//Catch filenames that are too long
if (fileName.Length > 40)
fileName = fileName.Substring(0, 40);
#if iOS
//We need to ensure we add the correct document path for iOS
entireFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), fileName + " " + DateTime.Now.Hour.ToString() + "." + DateTime.Now.Minute.ToString() + "." + DateTime.Now.Second.ToString() + "." + DateTime.Now.Millisecond.ToString() + " " + DateTime.Now.ToString("dd-MM-yyyy" + " [" + Thread.CurrentThread.ManagedThreadId.ToString() + "]"));

0 comments on commit 69255d2

Please sign in to comment.