示例#1
0
        /// <summary>
        /// Constructs a Link object.
        /// Uses the already connected TcpClient.
        /// </summary>
        /// <param name="client">The already connected client</param>
        /// <param name="injector">The injector to pass arguments to incoming messages</param>
        public WritableLink(TcpClient client, IInjector injector)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }

            Client         = client;
            _stream        = Client.GetStream();
            _localEndpoint = GetLocalEndpoint();
            _disposed      = false;
            _reader        = new StreamDataReader(_stream);
            _writer        = new StreamDataWriter(_stream);
            _injector      = injector;
        }
示例#2
0
        /// <summary>
        /// Constructs a Link object.
        /// Uses the already connected TcpClient.
        /// </summary>
        /// <param name="client">The already connected client</param>
        /// <param name="streamingCodec">Streaming codec</param>
        internal StreamingLink(TcpClient client, IStreamingCodec <T> streamingCodec)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }

            _client = client;
            var stream = _client.GetStream();

            _localEndpoint  = GetLocalEndpoint();
            _disposed       = false;
            _reader         = new StreamDataReader(stream);
            _writer         = new StreamDataWriter(stream);
            _streamingCodec = streamingCodec;
        }
示例#3
0
        /// <summary>
        /// Constructs a Link object.
        /// Connects to the specified remote endpoint.
        /// </summary>
        /// <param name="remoteEndpoint">The remote endpoint to connect to</param>
        /// <param name="streamingCodec">Streaming codec</param>
        /// <param name="tcpClientFactory">TcpClient factory</param>
        internal StreamingLink(IPEndPoint remoteEndpoint, IStreamingCodec <T> streamingCodec, ITcpClientConnectionFactory tcpClientFactory)
        {
            if (remoteEndpoint == null)
            {
                throw new ArgumentNullException("remoteEndpoint");
            }

            _client = tcpClientFactory.Connect(remoteEndpoint);
            var stream = _client.GetStream();

            _localEndpoint  = GetLocalEndpoint();
            _disposed       = false;
            _reader         = new StreamDataReader(stream);
            _writer         = new StreamDataWriter(stream);
            _streamingCodec = streamingCodec;
        }
示例#4
0
        /// <summary>
        /// Constructs a Link object.
        /// Uses the already connected TcpClient.
        /// </summary>
        /// <param name="client">The already connected client</param>
        /// <param name="streamingCodec">Streaming codec</param>
        internal StreamingLink(TcpClient client, IStreamingCodec <T> streamingCodec)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }

            _client = client;
            var stream = _client.GetStream();

            _localEndpoint  = GetLocalEndpoint();
            _disposed       = false;
            _reader         = new StreamDataReader(stream);
            _writer         = new StreamDataWriter(stream);
            _streamingCodec = streamingCodec;
            Logger.Log(Level.Info, string.Format("Remote IP is: {0} with port: {1}", RemoteEndpoint.Address, RemoteEndpoint.Port));
        }
示例#5
0
        /// <summary>
        /// Constructs a Link object.
        /// Connects to the specified remote endpoint.
        /// </summary>
        /// <param name="remoteEndpoint">The remote endpoint to connect to</param>
        /// <param name="injector">The injector to pass arguments to incoming messages</param>
        public WritableLink(IPEndPoint remoteEndpoint, IInjector injector)
        {
            if (remoteEndpoint == null)
            {
                throw new ArgumentNullException("remoteEndpoint");
            }

            Client = new TcpClient();
            Client.Connect(remoteEndpoint);

            _stream        = Client.GetStream();
            _localEndpoint = GetLocalEndpoint();
            _disposed      = false;
            _reader        = new StreamDataReader(_stream);
            _writer        = new StreamDataWriter(_stream);
            _injector      = injector;
        }
        public async Task TestCommonStreamingCodecs()
        {
            IInjector injector = TangFactory.GetTang().NewInjector();
            IStreamingCodec<int> intCodec = injector.GetInstance<IntStreamingCodec>();
            IStreamingCodec<double> doubleCodec = injector.GetInstance<DoubleStreamingCodec>();
            IStreamingCodec<float> floatCodec = injector.GetInstance<FloatStreamingCodec>();

            IStreamingCodec<int[]> intArrCodec = injector.GetInstance<IntArrayStreamingCodec>();
            IStreamingCodec<double[]> doubleArrCodec = injector.GetInstance<DoubleArrayStreamingCodec>();
            IStreamingCodec<float[]> floatArrCodec = injector.GetInstance<FloatArrayStreamingCodec>();

            IStreamingCodec<string> stringCodec = injector.GetInstance<StringStreamingCodec>();

            CancellationToken token = new CancellationToken();

            int obj = 5;
            int[] intArr = {1, 2};
            double[] doubleArr = { 1, 2 };
            float[] floatArr = { 1, 2 };
            string stringObj = "hello";

            var stream = new MemoryStream();
            IDataWriter writer = new StreamDataWriter(stream);
            intCodec.Write(obj, writer);
            await intCodec.WriteAsync(obj + 1, writer, token);
            doubleCodec.Write(obj + 2, writer);
            await doubleCodec.WriteAsync(obj + 3, writer, token);
            floatCodec.Write(obj + 4, writer);
            await floatCodec.WriteAsync(obj + 5, writer, token);
            intArrCodec.Write(intArr, writer);
            await intArrCodec.WriteAsync(intArr, writer, token);
            doubleArrCodec.Write(doubleArr, writer);
            await doubleArrCodec.WriteAsync(doubleArr, writer, token);
            floatArrCodec.Write(floatArr, writer);
            await floatArrCodec.WriteAsync(floatArr, writer, token);
            stringCodec.Write(stringObj, writer);
            await stringCodec.WriteAsync(stringObj, writer, token);

            stream.Position = 0;
            IDataReader reader = new StreamDataReader(stream);
            int res1 = intCodec.Read(reader);
            int res2 = await intCodec.ReadAsync(reader, token);
            double res3 = doubleCodec.Read(reader);
            double res4 = await doubleCodec.ReadAsync(reader, token);
            float res5 = floatCodec.Read(reader);
            float res6 = await floatCodec.ReadAsync(reader, token);
            int[] resArr1 = intArrCodec.Read(reader);
            int[] resArr2 = await intArrCodec.ReadAsync(reader, token);
            double[] resArr3 = doubleArrCodec.Read(reader);
            double[] resArr4 = await doubleArrCodec.ReadAsync(reader, token);
            float[] resArr5 = floatArrCodec.Read(reader);
            float[] resArr6 = await floatArrCodec.ReadAsync(reader, token);
            string resArr7 = stringCodec.Read(reader);
            string resArr8 = await stringCodec.ReadAsync(reader, token);

            Assert.AreEqual(obj, res1);
            Assert.AreEqual(obj + 1, res2);
            Assert.AreEqual(obj + 2, res3);
            Assert.AreEqual(obj + 3, res4);
            Assert.AreEqual(obj + 4, res5);
            Assert.AreEqual(obj + 5, res6);
            Assert.AreEqual(stringObj, resArr7);
            Assert.AreEqual(stringObj, resArr8);

            for (int i = 0; i < intArr.Length; i++)
            {
                Assert.AreEqual(intArr[i], resArr1[i]);
                Assert.AreEqual(intArr[i], resArr2[i]);
            }

            for (int i = 0; i < doubleArr.Length; i++)
            {
                Assert.AreEqual(doubleArr[i], resArr3[i]);
                Assert.AreEqual(doubleArr[i], resArr4[i]);
            }

            for (int i = 0; i < floatArr.Length; i++)
            {
                Assert.AreEqual(floatArr[i], resArr5[i]);
                Assert.AreEqual(floatArr[i], resArr6[i]);
            }
        }
        public async Task TestCodecToStreamingCodec()
        {
            var config = TangFactory.GetTang().NewConfigurationBuilder()
                .BindImplementation(GenericType<ICodec<int>>.Class, GenericType<IntCodec>.Class)
                .BindImplementation(GenericType<IStreamingCodec<int>>.Class,
                    GenericType<CodecToStreamingCodec<int>>.Class)
                .Build();

            IStreamingCodec<int> streamingCodec =
                TangFactory.GetTang().NewInjector(config).GetInstance<IStreamingCodec<int>>();
           
            CancellationToken token = new CancellationToken();

            int obj = 5;
            var stream = new MemoryStream();
            IDataWriter writer = new StreamDataWriter(stream);
            streamingCodec.Write(obj, writer);
            await streamingCodec.WriteAsync(obj + 1, writer, token);

            stream.Position = 0;
            IDataReader reader = new StreamDataReader(stream);
            int res1 = streamingCodec.Read(reader);
            int res2 = await streamingCodec.ReadAsync(reader, token);
            Assert.AreEqual(obj, res1);
            Assert.AreEqual(obj + 1, res2);
        }