예제 #1
0
        void Recieve(StreamTransmitState state, Action<StreamTransmitState> callback)
        {
            if (state.Stream == null || !state.Stream.CanWrite)
            {
                throw new Exception("用于接收的流实例,为空或不能被写入");
            }
            try
            {
                var actualBufferSize =
                    (int)Math.Min(state.Size, config.BufferSize);
                state.Buffer = new byte[actualBufferSize];
                state.AfterTransmitCallback = callback;

                config.Connection.BeginReceive(state.Buffer,
                    0, (int)Math.Min(config.BufferSize, state.Size), SocketFlags.None,
                    AfterReceiveStreamCallback, state);
            }
            catch (Exception e)
            {

                Logging.LogUsefulException(e);
                throw;
            }

        }
예제 #2
0
        /// <summary>
        /// 发送数据流
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="callback"></param>
        public void Send(Stream stream, Action <StreamTransmitState> callback)
        {
            if (stream == null || !stream.CanRead)
            {
                throw new Exception("需要传输的流实例,为空或不能被读取");
            }
            try
            {
                var actualBufferSize =
                    (int)Math.Min(stream.Length, config.BufferSize);
                var state = new StreamTransmitState()
                {
                    Stream = stream,
                    Buffer = new byte[actualBufferSize],
                    AfterTransmitCallback = callback
                };

                state.Stream.BeginRead(state.Buffer, 0,
                                       actualBufferSize, AfterReadStream, state);
            }
            catch (Exception e)
            {
                Logging.LogUsefulException(e);
                throw;
            }
        }
예제 #3
0
        void Recieve(StreamTransmitState state, Action <StreamTransmitState> callback)
        {
            if (state.Stream == null || !state.Stream.CanWrite)
            {
                throw new Exception("用于接收的流实例,为空或不能被写入");
            }
            try
            {
                var actualBufferSize =
                    (int)Math.Min(state.Size, config.BufferSize);
                state.Buffer = new byte[actualBufferSize];
                state.AfterTransmitCallback = callback;

                config.Connection.BeginReceive(state.Buffer,
                                               0, (int)Math.Min(config.BufferSize, state.Size), SocketFlags.None,
                                               AfterReceiveStreamCallback, state);
            }
            catch (Exception e)
            {
                Logging.LogUsefulException(e);
                throw;
            }
        }
예제 #4
0
        /// <summary>
        /// 发送数据流
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="callback"></param>
        public void Send(Stream stream, Action<StreamTransmitState> callback)
        {
            if (stream == null || !stream.CanRead)
            {
                throw new Exception("需要传输的流实例,为空或不能被读取");
            }
            try
            {
                var actualBufferSize =
                       (int)Math.Min(stream.Length, config.BufferSize);
                var state = new StreamTransmitState()
                {
                    Stream = stream,
                    Buffer = new byte[actualBufferSize],
                    AfterTransmitCallback = callback
                };

                state.Stream.BeginRead(state.Buffer, 0,
                    actualBufferSize, AfterReadStream, state);
            }
            catch (Exception e)
            {
                Logging.LogUsefulException(e);
                throw;

            }
        }