Exemplo n.º 1
0
        void RoundTripTypeDefinition(Type t, TypeDefinitionVerifier verifier)
        {
            String typeDefinition = t.SosTypeDefinition();

            Console.WriteLine("Testing type definition: '{0}'", typeDefinition);
            SosTypes.ParseSosTypeDefinition(verifier, typeDefinition);
        }
Exemplo n.º 2
0
        public void TestObjectTypesWithSpace()
        {
            Assert.AreEqual("{Boolean:b,Int32:i}", typeof(TestClasses.BooleanAndInt).SosTypeDefinition());

            String [] definitions = new String[] {
                "{Boolean:b,Int32:i}",
                "{    Boolean:b,Int32:i}",
                "{    Boolean   :b,Int32:i}",
                "{    Boolean   :    b,Int32:i}",
                "{    Boolean   :    b    ,Int32:i}",
                "{    Boolean   :    b    ,    Int32:i}",
                "{    Boolean   :    b    ,    Int32    :i}",
                "{    Boolean   :    b    ,    Int32    :    i}",
                "{    Boolean   :    b    ,    Int32    :    i     }",
                "{    Boolean   :    b    ,    Int32    :    i     }  ",
            };

            SosObjectDefinition objectDefinition = new SosObjectDefinition(
                "Boolean", "b",
                "Int32", "i");

            for (int i = 0; i < definitions.Length; i++)
            {
                String definition = definitions[i];
                Console.WriteLine(definition);
                Assert.AreEqual(objectDefinition, SosTypes.ParseSosObjectTypeDefinition(definition, 0));
            }
        }
Exemplo n.º 3
0
        void ParseValidTypeNameTest(String expected, String testString)
        {
            String actual;
            Int32  offset = SosTypes.ParseTypeName(testString, 0, ':', out actual);

            Assert.AreEqual(offset, testString.Length - 1);
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 4
0
 void TestBadTypeDefinition(String typeDefinition)
 {
     try
     {
         SosTypes.ParseSosTypeDefinition(NullCallback.Instance, typeDefinition);
         Assert.Fail("Expected FormatException but did not get one for '{0}'", typeDefinition);
     }
     catch (FormatException e)
     {
         Console.WriteLine("Expected Format Exception: {0}", e.Message);
     }
 }
Exemplo n.º 5
0
 public void TestFailedObjectDefinitionVerification(Type type, String incorrectTypeDefinitino)
 {
     try
     {
         SosTypes.ParseSosObjectTypeDefinition(incorrectTypeDefinitino, 0).VerifyType(type);
         Assert.Fail("Expected InvalidOperationException");
     }
     catch (InvalidOperationException e)
     {
         Console.WriteLine(e.Message);
     }
 }
Exemplo n.º 6
0
        void ParseInvalidTypeNameTest(String testString)
        {
            String actual;

            try
            {
                SosTypes.ParseTypeName(testString, 0, ':', out actual);
                Assert.Fail("Expected FormatException");
            }
            catch (FormatException e)
            {
                Console.WriteLine(e.Message);
            }
        }
Exemplo n.º 7
0
        public void TestObjectDefinitionVerification()
        {
            Type[] objectTypes = new Type[] {
                typeof(Object),
            };

            for (int i = 0; i < objectTypes.Length; i++)
            {
                Type objectType = objectTypes[i];

                String typeDefinition = objectType.SosTypeDefinition();
                Console.WriteLine("Verifying " + typeDefinition);
                Assert.AreEqual(objectType,
                                SosTypes.ParseSosObjectTypeDefinition(typeDefinition, 0).GetAndVerifyType(objectType.FullName));
            }
        }
Exemplo n.º 8
0
        public void TestMethodDefinition(String methodDefinitionString, SosMethodDefinition expectedMethodDefinition)
        {
            Console.WriteLine("Testing '{0}'", methodDefinitionString);
            SosMethodDefinition methodDefinition = SosTypes.ParseMethodDefinition(methodDefinitionString, 0);
            String diff = expectedMethodDefinition.Diff(methodDefinition);

            if (diff != null)
            {
                Assert.Fail("Expected diff to be null but was '{0}'", diff);
            }
            if (!expectedMethodDefinition.Equals(methodDefinition))
            {
                Assert.Fail(String.Format("Diff was null but Equals failed Expected '{0}' Actual '{1}'",
                                          expectedMethodDefinition.Definition(), methodDefinition.Definition()));
            }
        }
Exemplo n.º 9
0
        public void TestEnumDefinitionVerification()
        {
            Type[] enumTypes = new Type[] {
                typeof(DayOfWeek),
                typeof(ConsoleColor),
                typeof(ConsoleKey),
                typeof(TwoEnum),
                typeof(System.IO.FileAttributes),
            };

            for (int i = 0; i < enumTypes.Length; i++)
            {
                Type enumType = enumTypes[i];

                String typeDefinition = enumType.SosTypeDefinition();
                Console.WriteLine("Verifying " + typeDefinition);
                Assert.AreEqual(enumType,
                                SosTypes.ParseSosEnumTypeDefinition(typeDefinition, 4).GetAndVerifyType(enumType.AssemblyQualifiedName, 0));
            }
        }
Exemplo n.º 10
0
        public void TestMethodParameterDefinition(String methodParametersDefinition, params String[] expectedTypesAndNames)
        {
            Console.WriteLine("Testing '{0}'", methodParametersDefinition);
            List <String> actualTypesAndNames = SosTypes.ParseMethodDefinitionParameters(methodParametersDefinition, 0);

            if (actualTypesAndNames == null || actualTypesAndNames.Count <= 0)
            {
                Assert.IsTrue(expectedTypesAndNames == null || expectedTypesAndNames.Length <= 0);
            }
            else
            {
                Assert.AreEqual(expectedTypesAndNames.Length, actualTypesAndNames.Count,
                                String.Format("Expected '{0}' types and names but got '{1}' from method definition '{2}'",
                                              expectedTypesAndNames.Length, actualTypesAndNames.Count, methodParametersDefinition));
                for (int i = 0; i < expectedTypesAndNames.Length; i++)
                {
                    Assert.AreEqual(expectedTypesAndNames[i], actualTypesAndNames[i],
                                    String.Format("Method Definition '{0}' expected '{1}' at index {2} but got '{2}'",
                                                  methodParametersDefinition, expectedTypesAndNames[i], i, actualTypesAndNames[i]));
                }
            }
        }
Exemplo n.º 11
0
 public void TestEnumTypeDefinition(String typeDefinition, SosEnumDefinition expected)
 {
     SosTypes.ParseSosTypeDefinition(new TypeDefinitionVerifier(new CallbackObject(expected)), typeDefinition);
 }
Exemplo n.º 12
0
        public static List <RemoteNpcObject> GetServerInterface(SocketLineReader socketLineReader,
                                                                out Dictionary <String, RemoteNpcInterface> serverInterfaces)
        {
            socketLineReader.socket.Send(Encoding.ASCII.GetBytes(":interface\n"));

            serverInterfaces = new Dictionary <String, RemoteNpcInterface>();
            List <SosMethodDefinition> methodDefinitionList = new List <SosMethodDefinition>();

            while (true)
            {
                String interfaceName = socketLineReader.ReadLine();
                if (interfaceName == null)
                {
                    throw UnexpectedClose(socketLineReader);
                }
                if (interfaceName.Length <= 0)
                {
                    break;
                }

                // Get parent interfaces
                String[] parentInterfaceNames = null;
                Int32    spaceIndex           = interfaceName.IndexOf(' ');
                if (spaceIndex >= 0)
                {
                    parentInterfaceNames = interfaceName.Substring(spaceIndex + 1).Split(' ');
                    interfaceName        = interfaceName.Remove(spaceIndex);
                }

                while (true)
                {
                    String methodDefinitionLine = socketLineReader.ReadLine();
                    if (methodDefinitionLine == null)
                    {
                        throw UnexpectedClose(socketLineReader);
                    }
                    if (methodDefinitionLine.Length <= 0)
                    {
                        break;
                    }

                    SosMethodDefinition methodDefinition = SosTypes.ParseMethodDefinition(methodDefinitionLine, 0);
                    methodDefinitionList.Add(methodDefinition);
                }
                serverInterfaces.Add(interfaceName, new RemoteNpcInterface(interfaceName, parentInterfaceNames, methodDefinitionList.ToArray()));
                methodDefinitionList.Clear();
            }

            List <RemoteNpcObject> serverObjects = new List <RemoteNpcObject>();

            while (true)
            {
                String objectLine = socketLineReader.ReadLine();
                if (objectLine == null)
                {
                    throw UnexpectedClose(socketLineReader);
                }
                if (objectLine.Length <= 0)
                {
                    break;
                }

                String               objectName     = objectLine.Peel(out objectLine);
                String[]             interfaceNames = objectLine.Split(RemoteNpcObject.SplitChars, StringSplitOptions.RemoveEmptyEntries);
                RemoteNpcInterface[] interfaces     = new RemoteNpcInterface[interfaceNames.Length];
                for (int i = 0; i < interfaceNames.Length; i++)
                {
                    String             interfaceName = interfaceNames[i];
                    RemoteNpcInterface npcInterface;
                    if (!serverInterfaces.TryGetValue(interfaceName, out npcInterface))
                    {
                        throw new FormatException(String.Format("The NPC server returned interface '{0}' in the :objects command but not in the :interfaces command",
                                                                interfaceName));
                    }
                    interfaces[i] = npcInterface;
                }
                serverObjects.Add(new RemoteNpcObject(objectName, interfaces));
            }

            return(serverObjects);
        }
Exemplo n.º 13
0
        public void UpdateAndVerifyEnumAndObjectTypes(NpcVerifyCriteria criteria)
        {
            if (threadSafe)
            {
                Monitor.Enter(serverEndPoint);
            }
            try
            {
                //
                // The reason for the retry logic is because if the underlying socket is disconnected, it may not
                // fail until after a send and a receive...so the socket should be reconnected and the request should
                // be repeated only once.
                //
                for (UInt32 attempt = 0; ; attempt++)
                {
                    try
                    {
                        Connect();
                        socketLineReader.socket.Send(Encoding.UTF8.GetBytes(":type\n"));

                        enumAndObjectTypes.Clear();

                        while (true)
                        {
                            String typeDefinitionLine = socketLineReader.ReadLine();
                            if (typeDefinitionLine == null)
                            {
                                if (attempt == 0)
                                {
                                    Dispose();
                                    continue; // Retry
                                }
                                throw UnexpectedClose();
                            }
                            if (typeDefinitionLine.Length == 0)
                            {
                                break;                                 // empty line
                            }
                            Int32  spaceIndex     = typeDefinitionLine.IndexOf(' ');
                            String sosTypeName    = typeDefinitionLine.Remove(spaceIndex);
                            String typeDefinition = typeDefinitionLine.Substring(spaceIndex + 1);

                            Type type = GetTypeFromSosTypeName(sosTypeName);
                            if (typeDefinition.StartsWith("Enum"))
                            {
                                SosEnumDefinition enumDefinition = SosTypes.ParseSosEnumTypeDefinition(typeDefinition, 4);
                                enumDefinition.VerifyType(type, (SosVerifyCriteria)criteria);
                            }
                            else
                            {
                                SosObjectDefinition objectDefinition = SosTypes.ParseSosObjectTypeDefinition(typeDefinition, 0);
                                objectDefinition.VerifyType(type);
                            }
                            enumAndObjectTypes.Add(type);
                        }
                        return;
                    }
                    catch (SocketException)
                    {
                        if (socketLineReader != null)
                        {
                            socketLineReader.Dispose();
                            socketLineReader = null;
                        }
                        if (attempt == 0)
                        {
                            continue; // Retry
                        }
                        throw;
                    }
                }
            }
            finally
            {
                if (threadSafe)
                {
                    Monitor.Exit(serverEndPoint);
                }
            }
        }