예제 #1
0
        public void Setup(
            string startFilePath)
        {
            if (Interlocked.Exchange(
                    ref this.setupIf1,
                    one) == one)
            {
                return;
            }

            IOBitter bitter = this;
            var      w      = this.web;

            w.RegisterDependency(
                new BinaryTranslator());
            w.RegisterDependency(
                new FileIOBitterSettings(),
                bitter.Name);
            w.Run <FileIOBitterSettings>(
                settings =>
            {
                settings.FilePath = startFilePath;
            },
                bitter.Name);
            w.RegisterDependency(
                this,
                bitter.Name);
        }
예제 #2
0
        IEnumerable <bool> IOBitter.Read()
        {
            if (Interlocked.Read(
                    ref this.setupIf1) != one)
            {
                return(Empty <bool>());
            }

            var w = this.web;
            IEnumerable <bool> bits   = Empty <bool>();
            IOBitter           bitter = this;

            w.Run <FileIOBitterSettings, BinaryTranslator>(
                (settings, bt) =>
            {
                IEnumerable <byte> bytesToRead;
                try
                {
                    bytesToRead = File.ReadAllBytes(
                        settings.FilePath);
                }
                catch
                {
                    return;
                }

                bits = bt.GetBits(bytesToRead);
            },
                bitter.Name);

            return(bits);
        }
예제 #3
0
        public void Setup(
            string host,
            int port)
        {
            if (Interlocked.Exchange(
                    ref this.setupIf1,
                    1) == 1)
            {
                return;
            }

            IOBitter bitter = this;
            var      w      = this.web;

            w.RegisterDependency(
                new BinaryTranslator());
            w.RegisterDependency(
                new TcpIpIOBitterSettings(),
                bitter.Name);
            w.Run <TcpIpIOBitterSettings>(
                settings =>
            {
                settings.ReadBufferLength    = 1024 * 1024 * 7;
                settings.IPAddressOrHostName = host;
                settings.Port = port;
                settings.ReadTimeoutMilliseconds    = 2000;
                settings.WriteTimeoutMilliseconds   = 2500;
                settings.ConnectTimeoutMilliseconds = 2000;
            },
                bitter.Name);
            w.RegisterDependency(
                this,
                bitter.Name);
        }
예제 #4
0
 public virtual IOBitter NewLot(
     IOBitter bitter)
 {
     return(new LotIOBitter(
                new LinkedListLotter(),
                bitter));
 }
예제 #5
0
 public LotIOBitter(
     Lotter lotter,
     IOBitter bitter)
 {
     this.lotter = lotter;
     this.bitter = bitter;
 }
예제 #6
0
        void IOBitter.Write(
            IEnumerable <bool> bits,
            out bool succeeded)
        {
            if (bits == default(IEnumerable <bool>))
            {
                succeeded = true;
                return;
            }

            IOBitter bitter          = this;
            var      w               = this.web;
            var      reallySucceeded = false;

            w.Run <TcpIpIOBitterSettings, BinaryTranslator>(
                (settings, bt) =>
            {
                using (var client = new TcpClient())
                {
                    try
                    {
                        if (client
                            .BeginConnect(
                                settings.IPAddressOrHostName,
                                settings.Port,
                                result => { },
                                new object())
                            .AsyncWaitHandle
                            .WaitOne(2000))
                        {
                            var bytes = ToArray(bt
                                                .GetBytes(bits));
                            using (var stream = client.GetStream())
                            {
                                stream.WriteTimeout =
                                    settings.WriteTimeoutMilliseconds;
                                stream.Write(
                                    bytes,
                                    0,
                                    bytes.Length);
                                reallySucceeded = true;
                            }
                        }
                    }
                    catch
                    {
                        reallySucceeded = false;
                    }
                }
            },
                bitter.Name);

            succeeded = reallySucceeded;
        }
예제 #7
0
        IEnumerable <bool> IOBitter.Read()
        {
            IOBitter bitter = this;
            var      w      = this.web;
            var      bits   = Empty <bool>();

            w.Run <TcpIpIOBitterSettings, BinaryTranslator>(
                (settings, bt) =>
            {
                using (var client = new TcpClient())
                {
                    try
                    {
                        if (client
                            .BeginConnect(
                                settings.IPAddressOrHostName,
                                settings.Port,
                                result => { },
                                new object())
                            .AsyncWaitHandle
                            .WaitOne(settings.ConnectTimeoutMilliseconds))
                        {
                            using (var stream = client.GetStream())
                            {
                                stream.ReadTimeout
                                           = settings.ReadTimeoutMilliseconds;
                                var buffer = new byte[
                                    settings.ReadBufferLength];
                                var numberOfBytesRead = stream.Read(
                                    buffer,
                                    0,
                                    buffer.Length);

                                var readBytes = new byte[
                                    numberOfBytesRead];
                                Array.Copy(buffer,
                                           readBytes,
                                           numberOfBytesRead);
                                bits = bt.GetBits(readBytes);
                            }
                        }
                    }
                    catch
                    {
                        bits = Empty <bool>();
                    }
                }
            },
                bitter.Name);

            return(bits);
        }
예제 #8
0
        public virtual IOBitter NewFile(
            string filePath,
            string bitterName)
        {
            var fileBitter = new FileIOBitter(
                this.web);
            IOBitter bitter = fileBitter;

            bitter.Name = bitterName;
            fileBitter.Setup(filePath);

            return(bitter);
        }
예제 #9
0
        public virtual IOBitter NewTcpIp(
            string host,
            int port,
            string bitterName)
        {
            var tcpBitter = new TcpIpIOBitter(
                this.web);
            IOBitter bitter = tcpBitter;

            bitter.Name = bitterName;
            tcpBitter.Setup(host, port);

            return(bitter);
        }
예제 #10
0
        void IOBitter.Write(
            IEnumerable <bool> bits,
            out bool succeeded)
        {
            if (Interlocked.Read(
                    ref this.setupIf1) != one)
            {
                succeeded = false;
                return;
            }

            if (bits == default(IEnumerable <bool>))
            {
                succeeded = true;
                return;
            }

            var      w = this.web;
            var      reallySucceeded = false;
            IOBitter bitter          = this;

            w.Run <FileIOBitterSettings, BinaryTranslator>(
                (settings, bt) =>
            {
                var bytesToWrite = bt.GetBytes(bits);
                var array        = ToArray(bytesToWrite);
                try
                {
                    File.WriteAllBytes(
                        settings.FilePath,
                        array);
                }
                catch
                {
                    reallySucceeded = false;
                    goto end;
                }

                reallySucceeded = true;
                end:;
            },
                bitter.Name);

            succeeded = reallySucceeded;
        }