private void loadMapsFromDLLList() { // Load command processor map for (int i = 0; i < dllList.Length; i++) { // Attempt to load the DLL assemblyList[i] = Assembly.LoadFrom(dllList[i]); // If we successfully loaded the file, then attempt to load interfaces if (null != assemblyList[i]) { // Get the list of types exported from the DLL Type[] allTypes = assemblyList[i].GetTypes(); foreach (Type type in allTypes) { // If this is an ICommandProcessor object, add it to the processor map if (!type.IsAbstract && typeof(ICommandProcessor).IsAssignableFrom(type)) { // Create a instance of the class ICommandProcessor proc = (ICommandProcessor)Activator.CreateInstance(type); // Add the instance to the processorMap if (null != proc) { processorMap.Add(proc.requestType, proc); } } else if (!type.IsAbstract && typeof(ICommandMessageFactory).IsAssignableFrom(type)) { // Create a instance of the class ICommandMessageFactory factory = (ICommandMessageFactory)Activator.CreateInstance(type); // Add the instance to the message factory map if (null != factory) { for (int j = 0; j < factory.supportedMessages.Length; j++) { msgFactoryMap.Add(factory.supportedMessages[j], factory); } } } else if (!type.IsAbstract && typeof(CollectionAgentMessage).IsAssignableFrom(type)) { knownTypeList.Add(type); } } } } }
private CollectionAgentMessage ReadMessage(SslStream sslStream) { CollectionAgentMessage deserializedMsg = null; // Read the message sent by the client. // The client signals the end of the message using the // "<EOF>" marker. byte[] buffer = new byte[2048]; StringBuilder messageData = new StringBuilder(); int bytes = -1; do { // Read the client's test message. bytes = sslStream.Read(buffer, 0, buffer.Length); // Use Decoder class to convert from bytes to UTF8 // in case a character spans two buffers. Decoder decoder = Encoding.UTF8.GetDecoder(); char[] chars = new char[decoder.GetCharCount(buffer, 0, bytes)]; decoder.GetChars(buffer, 0, bytes, chars, 0); messageData.Append(chars); if (messageData.ToString().IndexOf("<EOF>") != -1) { break; } } while (bytes != 0); String strJSON = messageData.ToString(); // If there is a trailing <EOF> character, strip it so that JSON // deserialization will work correctly int index = (strJSON.IndexOf("<EOF>")); if (index != -1) { strJSON = strJSON.Substring(0, index); } // Read String data into a MemoryStream so it can be deserialized MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strJSON)); // Deserialize the stream into an object DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(CollectionAgentMessage)); CollectionAgentMessage baseMsg = ser.ReadObject(ms) as CollectionAgentMessage; ms.Close(); if (null != baseMsg) { ICommandMessageFactory factory = msgFactoryMap[baseMsg.requestType]; if (null != factory) { deserializedMsg = factory.constructMessageFromJSON(baseMsg.requestType, strJSON); } } // Return the new object return(deserializedMsg); }