Beispiel #1
0
        public void SerializeDeserialize()
        {
            var stream = new MemoryStream(new byte[20480], true);
            var writer = new StreamValueWriter(stream);
            var reader = new StreamValueReader(stream);

            var channel = new ChannelInfo(1)
            {
                ParentChannelId = 2,
                Name            = "Name",
                Description     = "Description",
                ReadOnly        = false,
                UserLimit       = 2
            };

            channel.Serialize(null, writer);
            long length = stream.Position;

            stream.Position = 0;

            var deserializedChannel = new ChannelInfo();

            deserializedChannel.Deserialize(null, reader);

            Assert.AreEqual(length, stream.Position);
            AssertChanelsAreEqual(channel, deserializedChannel);
        }
Beispiel #2
0
        public void SerializeDeserialize()
        {
            const string logo = "logo";
            const string name = "name";
            const string desc = "description";

            ServerInfo info = new ServerInfo(new ServerSettings
            {
                ServerLogo     = logo,
                Name           = name,
                Description    = desc,
                ServerPassword = "******"
            }, new GuestUserProvider());

            var stream = new MemoryStream(new byte[20480], true);
            var writer = new StreamValueWriter(stream);
            var reader = new StreamValueReader(stream);

            info.Serialize(null, writer);
            long length = stream.Position;

            stream.Position = 0;

            info = new ServerInfo(null, reader);
            Assert.AreEqual(length, stream.Position);
            Assert.AreEqual(logo, info.Logo);
            Assert.AreEqual(name, info.Name);
            Assert.AreEqual(desc, info.Description);
            Assert.AreEqual(true, info.Passworded);
            Assert.AreEqual(UserRegistrationMode.None, info.RegistrationMode);
            Assert.IsNull(info.RegistrationContent);
        }
Beispiel #3
0
        public static T AssertLengthMatches <T> (this T self)
            where T : Message, new()
        {
            if (self == null)
            {
                throw new ArgumentNullException("self");
            }

            var stream = new MemoryStream(20480);
            var writer = new StreamValueWriter(stream);
            var reader = new StreamValueReader(stream);

            var msg     = new T();
            var context = SerializationContextTests.GetContext(msg.Protocol);

            self.WritePayload(context, writer);
            long len = stream.Position;

            stream.Position = 0;

            msg.ReadPayload(context, reader);
            Assert.AreEqual(len, stream.Position);

            return(msg);
        }
Beispiel #4
0
        public void SerializeDeserialize()
        {
            var stream = new MemoryStream(new byte[20480], true);
            var writer = new StreamValueWriter(stream);
            var reader = new StreamValueReader(stream);

            var source = GetTestSource();

            source.Serialize(null, writer);
            long length = stream.Position;

            stream.Position = 0;

            source = new AudioSource(null, reader);
            AssertSourcesMatch(GetTestSource(), source);
            Assert.AreEqual(length, stream.Position);
        }
Beispiel #5
0
        public void SerializeDeserialize()
        {
            var stream = new MemoryStream(new byte[20480], true);
            var writer = new StreamValueWriter(stream);
            var reader = new StreamValueReader(stream);

            var args = new AudioCodecArgs(Format, Bitrate, FrameSize, Complexity);

            args.Serialize(null, writer);
            long length = stream.Position;

            stream.Position = 0;

            args = new AudioCodecArgs(null, reader);
            Assert.AreEqual(length, stream.Position);
            Assert.AreEqual(Format.WaveEncoding, args.WaveEncoding);
            Assert.AreEqual(Format.Channels, args.Channels);
            Assert.AreEqual(Format.BitsPerSample, args.BitsPerSample);
            Assert.AreEqual(Bitrate, args.Bitrate);
            Assert.AreEqual(Format.SampleRate, args.SampleRate);
            Assert.AreEqual(FrameSize, args.FrameSize);
            Assert.AreEqual(Complexity, args.Complexity);
        }
Beispiel #6
0
        public void SerializeDeserialize()
        {
            var stream = new MemoryStream(new byte[20480], true);
            var writer = new StreamValueWriter(stream);
            var reader = new StreamValueReader(stream);

            var info = new UserInfo(Nickname, Phonetic, Username, UserId, ChanId, Muted, Comment, Status);

            info.Serialize(null, writer);
            long length = stream.Position;

            stream.Position = 0;

            info = new UserInfo(null, reader);
            Assert.AreEqual(length, stream.Position);
            Assert.AreEqual(UserId, info.UserId);
            Assert.AreEqual(ChanId, info.CurrentChannelId);
            Assert.AreEqual(Nickname, info.Nickname);
            Assert.AreEqual(Phonetic, info.Phonetic);
            Assert.AreEqual(Muted, info.IsMuted);
            Assert.AreEqual(Comment, info.Comment);
            Assert.AreEqual(Status, info.Status);
        }
Beispiel #7
0
        private static RSAAsymmetricKey GetKey(string keypath)
        {
            if (!File.Exists(keypath))
            {
                var           rsa        = new RSACrypto();
                RSAParameters parameters = rsa.ExportKey(true);

                using (var stream = File.OpenWrite(keypath)) {
                    var writer = new StreamValueWriter(stream);
                    RSAParametersSerializer.Serialize(writer, parameters);
                }
            }

            RSAAsymmetricKey key;

            using (var stream = File.OpenRead(keypath)) {
                var           reader     = new StreamValueReader(stream);
                RSAParameters parameters = RSAParametersSerializer.Deserialize(reader);
                key = new RSAAsymmetricKey(parameters);
            }

            return(key);
        }
Beispiel #8
0
        public static Task <RSAAsymmetricKey> GetCryptoKeyAsync(CancellationToken cancelToken = default(CancellationToken))
        {
            return(Task.Run(() => {
                RSAAsymmetricKey key = null;
                if (KeyFile.Exists)
                {
                    try {
                        using (var stream = File.OpenRead(KeyFile.FullName)) {
                            var reader = new StreamValueReader(stream);
                            RSAParameters parameters = RSAParametersSerializer.Deserialize(reader);
                            key = new RSAAsymmetricKey(parameters);
                        }
                    } catch (Exception ex) {
                        Trace.TraceWarning("Failed to read key: {0}", ex);
                        KeyFile.Delete();
                    }
                }

                cancelToken.ThrowIfCancellationRequested();

                if (!KeyFile.Exists)
                {
                    var rsa = new RSACrypto();
                    RSAParameters parameters = rsa.ExportKey(true);
                    key = new RSAAsymmetricKey(parameters);

                    cancelToken.ThrowIfCancellationRequested();

                    using (var stream = File.OpenWrite(KeyFile.FullName)) {
                        var writer = new StreamValueWriter(stream);
                        RSAParametersSerializer.Serialize(writer, parameters);
                    }
                }

                return key;
            }, cancelToken));
        }
Beispiel #9
0
 public void MessageSetup()
 {
     stream = new MemoryStream(new byte[20480], true);
     writer = new StreamValueWriter(stream);
     reader = new StreamValueReader(stream);
 }
Beispiel #10
0
        public static void Main()
        {
            Trace.Listeners.Add(new ConsoleTraceListener());

            log4net.Config.XmlConfigurator.Configure();

            var serverConfig = (BarrelConfiguration)ConfigurationManager.GetSection("barrel");

            if (serverConfig == null)
            {
                LogManager.GetLogger("Barrel").Fatal("Section 'barrel' not found in configuration.");
                return;
            }

            var log = LogManager.GetLogger(serverConfig.Name.Remove(" "));

            log.Info("Checking configuration");
            if (!serverConfig.CheckConfiguration(log))
            {
                log.Fatal("Errors found in configuration, shutting down.");
                return;
            }

            ServerProviders providers = serverConfig.GetProviders(log);

            if (providers == null)
            {
                log.Fatal("Errors loading server configuration, shutting down.");
                return;
            }

            log.Info("Setting up");

            GablarskiServer server = new GablarskiServer(new ServerSettings
            {
                Name           = serverConfig.Name,
                Description    = serverConfig.Description,
                ServerPassword = serverConfig.Password,
                ServerLogo     = serverConfig.LogoURL
            }, providers.Users, providers.Permissions, providers.Channels);

            if (serverConfig.Network)
            {
                var keyFile          = new FileInfo("server.key");
                RSAAsymmetricKey key = null;
                if (keyFile.Exists)
                {
                    try {
                        using (var stream = File.OpenRead(keyFile.FullName)) {
                            var           reader     = new StreamValueReader(stream);
                            RSAParameters parameters = RSAParametersSerializer.Deserialize(reader);
                            key = new RSAAsymmetricKey(parameters);
                        }
                    } catch (Exception ex) {
                        Trace.TraceWarning("Failed to read key: {0}", ex);
                        keyFile.Delete();
                    }
                }

                if (!keyFile.Exists)
                {
                    var           rsa        = new RSACrypto();
                    RSAParameters parameters = rsa.ExportKey(true);
                    key = new RSAAsymmetricKey(parameters);

                    using (var stream = File.OpenWrite(keyFile.FullName)) {
                        var writer = new StreamValueWriter(stream);
                        RSAParametersSerializer.Serialize(writer, parameters);
                    }
                }

                server.AddConnectionProvider(new UdpConnectionProvider(serverConfig.Port, GablarskiProtocol.Instance, key), ExecutionMode.GlobalOrder);
            }

            foreach (IConnectionProvider provider in providers.ConnectionProviders)
            {
                server.AddConnectionProvider(provider, ExecutionMode.GlobalOrder);
            }

            server.Start();

            log.Info("Server started");

            while (true)
            {
                Console.ReadKey();
            }
        }
        protected override IValueReader GetReader(IValueWriter writer)
        {
            StreamValueWriter streamWriter = (StreamValueWriter)writer;

            return(new StreamValueReader(streamWriter.stream));
        }