Пример #1
0
        /// <summary>
        /// Helper method for Gem.Network.Tests.MessageEncodingTests.SuccessfulDecodeTest()
        /// Set's up a server and sends a message for decoding
        /// This is done in a seperate project because a socket exception is thrown
        /// because client / server use the same port
        /// </summary>
        public static void MessageDecodingTestHelper()
        {
            var server = new NetworkServer();
            //server.Connect(new ServerConfig { Name = "local", Port = 14241 });

            NetIncomingMessage inc;

            while (true)
            {
                if ((inc = server.ReadMessage()) != null)
                {
                    switch (inc.MessageType)
                    {
                    case NetIncomingMessageType.ConnectionApproval:
                        Console.WriteLine("Incoming LOGIN");
                        inc.SenderConnection.Approve();
                        break;

                    case NetIncomingMessageType.Data:
                        var outgoingmessage = server.CreateMessage();
                        var obj             = new ClassToSerialize();
                        obj.StringProperty = "SomeString";
                        MessageSerializer.Encode(obj, ref outgoingmessage);
                        server.SendToAll(outgoingmessage);

                        Console.WriteLine("Sended a package with some string");
                        break;
                    }
                }
            }
        }
Пример #2
0
        public void SendNotification(Notification notification)
        {
            var msg = client.CreateMessage();

            MessageSerializer.Encode(notification, ref msg);
            client.SendMessage(msg, PackageConfig.DeliveryMethod, PackageConfig.SequenceChannel);
        }
Пример #3
0
        public void SendMessage <T>(T message)
        {
            var msg = client.CreateMessage();

            MessageSerializer.Encode(message, ref msg);
            client.SendMessage(msg, PackageConfig.DeliveryMethod, PackageConfig.SequenceChannel);
        }
Пример #4
0
        public ServerCommandHost(IServer commandHost)
        {
            Guard.That(commandHost, "Command host must not be null").IsNotNull();

            this.commandHost = commandHost;
            executioners     = new Stack <ICommandExecutioner>();
            commandTable     = new Dictionary <string, CommandInfo>();

            EchoToAll = new DebugHost();
            EchoToAll.RegisterAppender(new ActionAppender(commandHost.NotifyAll));

            //Setup echo to respond only to the defined connection
            Echo = (msg, connection) =>
            {
                var om = commandHost.CreateMessage();
                var serverNotification = new Notification(msg, NotificationType.Message);
                MessageSerializer.Encode(serverNotification, ref om);
                GemNetworkDebugger.Append.Info(msg);
                commandHost.SendOnlyTo(om, connection);
            };

            RegisterHelpComand();
            RegisterSetPasswordCommand();
            RegisterEchoCommand();
            RegisterKickCommand();
            RegisterViewClientsCommand();

            //the default password
            Password = "******";
        }
Пример #5
0
        public void SendNotification(Notification notification)
        {
            GemNetworkDebugger.Append.Info(String.Format("Sent to all :  {0}  of type {1}", notification.Message, notification.Type));
            var om = netServer.CreateMessage();

            MessageSerializer.Encode(notification, ref om);
            SendToAll(om);
        }
Пример #6
0
        public void SendMessage <T>(T message, byte id)
        {
            var msg = netServer.CreateMessage();

            MessageSerializer.Encode(message, ref msg);
            msg.Write(id);
            netServer.SendToAll(msg, packageConfig.DeliveryMethod);
        }
Пример #7
0
        public void SendMessage <T>(object[] args)
        {
            var om  = client.CreateMessage();
            var msg = (T)Activator.CreateInstance(typeof(T), args);

            MessageSerializer.Encode(msg, ref om);
            client.SendMessage(om, PackageConfig.DeliveryMethod, PackageConfig.SequenceChannel);
        }
Пример #8
0
        public void NotifyAllExcept(string message, NetConnection client, string type = NotificationType.Message)
        {
            GemNetworkDebugger.Append.Info(String.Format("Sent to all :  {0}", message));
            var serverNotification = new Notification(message, type);
            var om = netServer.CreateMessage();

            MessageSerializer.Encode(serverNotification, ref om);
            SendAndExclude(om, client);
        }
Пример #9
0
        public void NotifyAll(string message)
        {
            GemNetworkDebugger.Append.Info(String.Format("Sent to all :  {0}", message));
            var serverNotification = new Notification(message, NotificationType.Message);
            var om = netServer.CreateMessage();

            MessageSerializer.Encode(serverNotification, ref om);
            SendToAll(om);
        }
Пример #10
0
        public void SendMessageWithLocalTime <T>(T message, byte id)
        {
            var msg = client.CreateMessage();

            MessageSerializer.Encode(message, ref msg);
            msg.Write(id);
            msg.WriteTime(NetTime.Now, true);
            client.SendMessage(msg, PackageConfig.DeliveryMethod, PackageConfig.SequenceChannel);
        }
Пример #11
0
        public void SendMessage <T>(NetConnection sender, T message, byte id)
        {
            var msg = netServer.CreateMessage();

            MessageSerializer.Encode(message, ref msg);
            msg.Write(id);

            netServer.SendToAll(msg,
                                sender,
                                packageConfig.DeliveryMethod,
                                packageConfig.SequenceChannel);
        }
Пример #12
0
 public void NotifyOnly(string message, NetConnection client, string type = NotificationType.Message)
 {
     if (client != null)
     {
         GemNetworkDebugger.Append.Info(String.Format("{0}  :  {1}", client, message));
         var serverNotification = new Notification(message, type);
         var om = netServer.CreateMessage();
         MessageSerializer.Encode(serverNotification, ref om);
         SendOnlyTo(om, client);
     }
     else
     {
         GemNetworkDebugger.Append.Info(String.Format("Server  :  {0}", message));
     }
 }
Пример #13
0
        public static void DynamicMessageDecodingTestHelper()
        {
            #region Dynamic Type

            var propertyList = new List <RuntimePropertyInfo>
            {
                new RuntimePropertyInfo {
                    PropertyName = "Name",
                    PropertyType = typeof(string)
                }
            };
            IPocoBuilder PocoBuilder = new ReflectionEmitBuilder();
            Type         myNewType   = PocoBuilder.Build("POCO", propertyList);
            dynamic      myObject    = Activator.CreateInstance(myNewType);
            myObject.Name = "DynamicType";

            #endregion

            NetworkServer server;
            server = new NetworkServer();
            //server.Connect(new ServerConfig { Name = "local", Port = 14241 });

            NetIncomingMessage inc;

            while (true)
            {
                if ((inc = server.ReadMessage()) != null)
                {
                    switch (inc.MessageType)
                    {
                    case NetIncomingMessageType.ConnectionApproval:
                        Console.WriteLine("Incoming LOGIN");
                        inc.SenderConnection.Approve();
                        break;

                    case NetIncomingMessageType.Data:
                        var outgoingmessage = server.CreateMessage();
                        MessageSerializer.Encode(myObject, ref outgoingmessage);
                        server.SendToAll(outgoingmessage);

                        Console.WriteLine("Sended a package with some string");
                        break;
                    }
                }
            }
        }