コード例 #1
0
ファイル: TcpSocketClient.cs プロジェクト: dut3062796s/Cowboy
        public void SendAsync(byte[] data, int offset, int count)
        {
            BufferValidator.ValidateBuffer(data, offset, count, "data");

            if (!Connected)
            {
                throw new InvalidProgramException("This client has not connected to server.");
            }

            try
            {
                if (_stream.CanWrite)
                {
                    var frame = _configuration.FrameBuilder.EncodeFrame(data, offset, count);
                    _stream.BeginWrite(frame, 0, frame.Length, HandleDataWritten, _tcpClient);
                }
            }
            catch (Exception ex)
            {
                if (ex is IOException &&
                    ex.InnerException != null &&
                    ex.InnerException is SocketException &&
                    (ex.InnerException as SocketException).SocketErrorCode == SocketError.TimedOut)
                {
                    _log.Error(ex.Message, ex);
                }
                else
                {
                    if (!CloseIfShould(ex))
                    {
                        throw;
                    }
                }
            }
        }
コード例 #2
0
        public async Task SendAsync(byte[] data, int offset, int count)
        {
            BufferValidator.ValidateBuffer(data, offset, count, "data");

            if (State != TcpSocketConnectionState.Connected)
            {
                throw new InvalidOperationException("This session has not connected.");
            }

            try
            {
                if (_stream.CanWrite)
                {
                    byte[] frameBuffer;
                    int    frameBufferOffset;
                    int    frameBufferLength;
                    _configuration.FrameBuilder.Encoder.EncodeFrame(data, offset, count, out frameBuffer, out frameBufferOffset, out frameBufferLength);

                    await _stream.WriteAsync(frameBuffer, frameBufferOffset, frameBufferLength);
                }
            }
            catch (Exception ex) when(!ShouldThrow(ex))
            {
            }
        }
コード例 #3
0
        public async Task SendAsync(byte[] data, int offset, int count)
        {
            BufferValidator.ValidateBuffer(data, offset, count, "data");

            if (State != TcpSocketConnectionState.Connected)
            {
                throw new InvalidOperationException("This client has not connected to server.");
            }

            try
            {
                byte[] frameBuffer;
                int    frameBufferOffset;
                int    frameBufferLength;
                _configuration.FrameBuilder.Encoder.EncodeFrame(data, offset, count, out frameBuffer, out frameBufferOffset, out frameBufferLength);

                var saea = _saeaPool.Take();
                saea.Saea.SetBuffer(frameBuffer, frameBufferOffset, frameBufferLength);

                await _socket.SendAsync(saea);

                _saeaPool.Return(saea);
            }
            catch (Exception ex) when(!ShouldThrow(ex))
            {
            }
        }
コード例 #4
0
        public IAsyncResult BeginSend(byte[] data, int offset, int count, AsyncCallback callback, object state)
        {
            BufferValidator.ValidateBuffer(data, offset, count, "data");

            if (this.State != TcpSocketConnectionState.Connected)
            {
                throw new InvalidProgramException("This client has not connected to server.");
            }

            try
            {
                byte[] frameBuffer;
                int    frameBufferOffset;
                int    frameBufferLength;
                _configuration.FrameBuilder.Encoder.EncodeFrame(data, offset, count, out frameBuffer, out frameBufferOffset, out frameBufferLength);

                return(_stream.BeginWrite(frameBuffer, frameBufferOffset, frameBufferLength, callback, state));
            }
            catch (Exception ex)
            {
                if (IsSocketTimeOut(ex))
                {
                    _log.Error(ex.Message, ex);
                }
                else
                {
                    if (!CloseIfShould(ex))
                    {
                        throw;
                    }
                }

                throw;
            }
        }
コード例 #5
0
        public void Send(byte[] data, int offset, int count)
        {
            BufferValidator.ValidateBuffer(data, offset, count, "data");

            if (!Active)
            {
                throw new InvalidProgramException("This session has been closed.");
            }

            try
            {
                if (_stream.CanWrite)
                {
                    var frame = _configuration.FrameBuilder.EncodeFrame(data, offset, count);
                    _stream.Write(frame, 0, frame.Length);
                }
            }
            catch (Exception ex)
            {
                if (ex is IOException &&
                    ex.InnerException != null &&
                    ex.InnerException is SocketException &&
                    (ex.InnerException as SocketException).SocketErrorCode == SocketError.TimedOut)
                {
                    _log.Error(ex.Message, ex);
                }
                else
                {
                    if (!CloseIfShould(ex))
                    {
                        throw;
                    }
                }
            }
        }
コード例 #6
0
        public void BeginSend(byte[] data, int offset, int count)
        {
            BufferValidator.ValidateBuffer(data, offset, count, "data");

            if (this.State != TcpSocketConnectionState.Connected)
            {
                throw new InvalidProgramException("This client has not connected to server.");
            }

            try
            {
                byte[] frameBuffer;
                int    frameBufferOffset;
                int    frameBufferLength;
                _configuration.FrameBuilder.Encoder.EncodeFrame(data, offset, count, out frameBuffer, out frameBufferOffset, out frameBufferLength);

                _stream.BeginWrite(frameBuffer, frameBufferOffset, frameBufferLength, HandleDataWritten, _stream);
            }
            catch (Exception ex)
            {
                if (IsSocketTimeOut(ex))
                {
                    _log.Error(ex.Message, ex);
                }
                else
                {
                    if (!CloseIfShould(ex))
                    {
                        throw;
                    }
                }
            }
        }
コード例 #7
0
        public void Send(byte[] data, int offset, int count)
        {
            BufferValidator.ValidateBuffer(data, offset, count, "data");

            if (State != TcpSocketConnectionState.Connected)
            {
                throw new InvalidProgramException("This session has been closed.");
            }

            try
            {
                byte[] frameBuffer;
                int    frameBufferOffset;
                int    frameBufferLength;
                _configuration.FrameBuilder.Encoder.EncodeFrame(data, offset, count, out frameBuffer, out frameBufferOffset, out frameBufferLength);

                _stream.Write(frameBuffer, frameBufferOffset, frameBufferLength);
            }
            catch (Exception ex)
            {
                if (IsSocketTimeOut(ex))
                {
                    Loger.Error(ex.Message, ex);
                }
                else
                {
                    if (!CloseIfShould(ex))
                    {
                        throw;
                    }
                }
            }
        }
コード例 #8
0
        public void SendAsync(byte[] data, int offset, int count)
        {
            BufferValidator.ValidateBuffer(data, offset, count, "data");

            if (!Active)
            {
                throw new InvalidProgramException("This session has been closed.");
            }

            try
            {
                byte[] frameBuffer;
                int    frameBufferOffset;
                int    frameBufferLength;
                _configuration.FrameBuilder.Encoder.EncodeFrame(data, offset, count, out frameBuffer, out frameBufferOffset, out frameBufferLength);

                _stream.BeginWrite(frameBuffer, frameBufferOffset, frameBufferLength, HandleDataWritten, _stream);
            }
            catch (Exception ex)
            {
                if (IsSocketTimeOut(ex))
                {
                    _log.Error(ex.Message, ex);
                }
                else
                {
                    if (!CloseIfShould(ex))
                    {
                        throw;
                    }
                }
            }
        }
コード例 #9
0
        public async Task SendAsync(byte[] data, int offset, int count)
        {
            BufferValidator.ValidateBuffer(data, offset, count, "data");

            if (State != TcpSocketConnectionState.Connected)
            {
                throw new InvalidOperationException("This client has not connected to server.");
            }

            try
            {
                if (_stream.CanWrite)
                {
                    var frame = _configuration.FrameBuilder.EncodeFrame(data, offset, count);
                    await _stream.WriteAsync(frame, 0, frame.Length);
                }
            }
            catch (Exception ex) when(!ShouldThrow(ex))
            {
            }
        }
コード例 #10
0
        public void Send(byte[] data, int offset, int count)
        {
            BufferValidator.ValidateBuffer(data, offset, count, "data");

            if (!Connected)
            {
                throw new InvalidProgramException("This client has not connected to server.");
            }

            try
            {
                if (_stream.CanWrite)
                {
                    byte[] frameBuffer;
                    int    frameBufferOffset;
                    int    frameBufferLength;
                    _configuration.FrameBuilder.Encoder.EncodeFrame(data, offset, count, out frameBuffer, out frameBufferOffset, out frameBufferLength);

                    _stream.Write(frameBuffer, frameBufferOffset, frameBufferLength);
                }
            }
            catch (Exception ex)
            {
                if (ex is IOException &&
                    ex.InnerException != null &&
                    ex.InnerException is SocketException &&
                    (ex.InnerException as SocketException).SocketErrorCode == SocketError.TimedOut)
                {
                    _log.Error(ex.Message, ex);
                }
                else
                {
                    if (!CloseIfShould(ex))
                    {
                        throw;
                    }
                }
            }
        }
コード例 #11
0
ファイル: TcpSocketSession.cs プロジェクト: peters/Cowboy
        public void SendAsync(byte[] data, int offset, int count)
        {
            BufferValidator.ValidateBuffer(data, offset, count, "data");

            if (!Active)
            {
                throw new InvalidProgramException("This session has been closed.");
            }

            try
            {
                if (_stream.CanWrite)
                {
                    byte[] frameBuffer;
                    int    frameBufferOffset;
                    int    frameBufferLength;
                    _configuration.FrameBuilder.Encoder.EncodeFrame(data, offset, count, out frameBuffer, out frameBufferOffset, out frameBufferLength);

                    _stream.BeginWrite(frameBuffer, frameBufferOffset, frameBufferLength, HandleDataWritten, _tcpClient);
                }
            }
            catch (Exception ex)
            {
                if (ex is IOException &&
                    ex.InnerException != null &&
                    ex.InnerException is SocketException &&
                    (ex.InnerException as SocketException).SocketErrorCode == SocketError.TimedOut)
                {
                    _log.Error(ex.Message, ex);
                }
                else
                {
                    if (!CloseIfShould(ex))
                    {
                        throw;
                    }
                }
            }
        }
コード例 #12
0
        public async Task SendAsync(byte[] data, int offset, int count)
        {
            BufferValidator.ValidateBuffer(data, offset, count, "data");

            if (State != TcpSocketConnectionState.Connected)
            {
                throw new InvalidOperationException("This client has not connected to server.");
            }

            try
            {
                byte[] frameBuffer;
                int    frameBufferOffset;
                int    frameBufferLength;
                _configuration.FrameBuilder.Encoder.EncodeFrame(data, offset, count, out frameBuffer, out frameBufferOffset, out frameBufferLength);

                await _stream.WriteAsync(frameBuffer, frameBufferOffset, frameBufferLength);
            }
            catch (Exception ex)
            {
                await HandleSendOperationException(ex);
            }
        }
コード例 #13
0
        public IAsyncResult BeginSend(byte[] data, int offset, int count, AsyncCallback callback, object state)
        {
            BufferValidator.ValidateBuffer(data, offset, count, "data");

            if (this.State != TcpSocketConnectionState.Connected)
            {
                throw new InvalidProgramException("This client has not connected to server.");
            }

            try
            {
                byte[] frameBuffer;
                int    frameBufferOffset;
                int    frameBufferLength;
                _configuration.FrameBuilder.Encoder.EncodeFrame(data, offset, count, out frameBuffer, out frameBufferOffset, out frameBufferLength);

                return(_stream.BeginWrite(frameBuffer, frameBufferOffset, frameBufferLength, callback, state));
            }
            catch (Exception ex) // catch exceptions then log then re-throw
            {
                HandleSendOperationException(ex);
                throw;
            }
        }
コード例 #14
0
        public void BeginSend(byte[] data, int offset, int count)
        {
            BufferValidator.ValidateBuffer(data, offset, count, "data");

            if (State != TcpSocketConnectionState.Connected)
            {
                throw new InvalidProgramException("This session has been closed.");
            }

            try
            {
                byte[] frameBuffer;
                int    frameBufferOffset;
                int    frameBufferLength;
                _configuration.FrameBuilder.Encoder.EncodeFrame(data, offset, count, out frameBuffer, out frameBufferOffset, out frameBufferLength);

                _stream.BeginWrite(frameBuffer, frameBufferOffset, frameBufferLength, HandleDataWritten, _stream);
            }
            catch (Exception ex) // catch exceptions then log then re-throw
            {
                HandleSendOperationException(ex);
                throw;
            }
        }