Beispiel #1
0
        /// <summary>
        /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
        /// </summary>
        /// <param name="buffer">An array of bytes.</param>
        /// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
        /// <param name="count">The number of bytes to be written to the current stream.</param>
        public override void Write(byte[] buffer, int offset, int count)
        {
            BinaryLogWriter binaryLogWriter = this._remote.ITransportContext.BinaryLogWriter;

            try
            {
                while (count > 0)
                {
                    int milliseconds = GenuineUtility.GetMillisecondsLeft(this._writeTimeout);
                    if (milliseconds <= 0)
                    {
                        throw GenuineExceptions.Get_Send_Timeout();
                    }

                    this._socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, milliseconds);

                    int bytesSent = this._socket.Send(buffer, offset, count, SocketFlags.None);

                    // LOG:
                    if (binaryLogWriter != null && binaryLogWriter[LogCategory.LowLevelTransport] > 0)
                    {
                        binaryLogWriter.WriteTransportContentEvent(LogCategory.LowLevelTransport, "SyncSocketWritingStream.Write",
                                                                   LogMessageType.LowLevelTransport_SyncSendingCompleted, null, null, this._remote,
                                                                   binaryLogWriter[LogCategory.LowLevelTransport] > 1 ? new MemoryStream(GenuineUtility.CutOutBuffer(buffer, offset, count)) : null,
                                                                   GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                                                   this._dbgConnectionId, count, this._socket.RemoteEndPoint.ToString(),
                                                                   null, null,
                                                                   "Immediately after Socket.Send(). Size: {0}. Sent: {1}.", count, bytesSent);
                    }

                    if (bytesSent == 0)
                    {
                        throw GenuineExceptions.Get_Send_TransportProblem();
                    }

                    offset += bytesSent;
                    count  -= bytesSent;
                    this._connectionManager.IncreaseBytesSent(bytesSent);
                }
            }
            catch (Exception ex)
            {
                // LOG:
                if (binaryLogWriter != null && binaryLogWriter[LogCategory.LowLevelTransport] > 0)
                {
                    binaryLogWriter.WriteTransportContentEvent(LogCategory.LowLevelTransport, "SyncSocketWritingStream.Write",
                                                               LogMessageType.LowLevelTransport_SyncSendingCompleted, ex, null, this._remote,
                                                               binaryLogWriter[LogCategory.LowLevelTransport] > 1 ? new MemoryStream(GenuineUtility.CutOutBuffer(buffer, offset, count)) : null,
                                                               GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                                               this._dbgConnectionId, count, null, null, null,
                                                               "Socket.Send(); ERROR.");
                }

                throw GenuineExceptions.Get_Send_TransportProblem();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
        /// </summary>
        /// <param name="buffer">An array of bytes.</param>
        /// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
        /// <param name="count">The number of bytes to be written to the current stream.</param>
        public override void Write(byte[] buffer, int offset, int count)
        {
            while (count > 0)
            {
                int bytesSent = this._socket.Send(buffer, offset, count, SocketFlags.None);
                if (bytesSent == 0)
                {
                    throw GenuineExceptions.Get_Send_TransportProblem();
                }

                this._connectionManager.IncreaseBytesSent(bytesSent);
                count  -= bytesSent;
                offset += bytesSent;
            }
        }
Beispiel #3
0
        /// <summary>
        /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
        /// </summary>
        /// <param name="buffer">An array of bytes.</param>
        /// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
        /// <param name="count">The number of bytes to be written to the current stream.</param>
        public override void Write(byte[] buffer, int offset, int count)
        {
            BinaryLogWriter binaryLogWriter = this.ITransportContext.BinaryLogWriter;
            int             milliseconds    = GenuineUtility.GetMillisecondsLeft(this._syncWritingTimeout);

            if (milliseconds <= 0)
            {
                throw GenuineExceptions.Get_Send_Timeout();
            }

            try
            {
                this.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, milliseconds);

                // LOG:
                if (binaryLogWriter != null && binaryLogWriter[LogCategory.Transport] > 0)
                {
                    binaryLogWriter.WriteTransportContentEvent(LogCategory.Transport, "TcpSocketInfo.Write",
                                                               LogMessageType.SynchronousSendingStarted, null, null, this.Remote == null ? null : this.Remote,
                                                               binaryLogWriter[LogCategory.Transport] > 1 ? new MemoryStream(GenuineUtility.CutOutBuffer(buffer, offset, count)) : null,
                                                               GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                                               this.DbgConnectionId, count, this.Remote == null || this.Remote.PhysicalAddress == null ? null : this.Remote.PhysicalAddress.ToString(),
                                                               null, null,
                                                               "The content is being sent synchronously. Size: {0}.", count);
                }

                for ( ; ;)
                {
                    int bytesSent = 0;
                    try
                    {
                        bytesSent = this.Socket.Send(buffer, offset, count, SocketFlags.None);

                        // LOG:
                        if (binaryLogWriter != null && binaryLogWriter[LogCategory.LowLevelTransport] > 0)
                        {
                            binaryLogWriter.WriteTransportContentEvent(LogCategory.LowLevelTransport, "TcpSocketInfo.Write",
                                                                       LogMessageType.LowLevelTransport_SyncSendingCompleted, null, null, this.Remote,
                                                                       binaryLogWriter[LogCategory.LowLevelTransport] > 1 ? new MemoryStream(GenuineUtility.CutOutBuffer(buffer, offset, bytesSent)) : null,
                                                                       GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                                                       this.DbgConnectionId, bytesSent, this.Socket.RemoteEndPoint.ToString(),
                                                                       null, null,
                                                                       "Socket.Send(). Size: {0}. Sent: {1}.", count, bytesSent);
                        }
                    }
                    catch (Exception ex)
                    {
                        // LOG:
                        if (binaryLogWriter != null && binaryLogWriter[LogCategory.LowLevelTransport] > 0)
                        {
                            binaryLogWriter.WriteTransportContentEvent(LogCategory.LowLevelTransport, "TcpSocketInfo.Write",
                                                                       LogMessageType.LowLevelTransport_SyncSendingCompleted, ex, null, this.Remote == null ? null : this.Remote,
                                                                       binaryLogWriter[LogCategory.LowLevelTransport] > 1 ? new MemoryStream(GenuineUtility.CutOutBuffer(buffer, offset, bytesSent)) : null,
                                                                       GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                                                       this.DbgConnectionId, bytesSent, this.Socket.RemoteEndPoint.ToString(),
                                                                       null, null,
                                                                       "Socket.Send() failed. Size: {0}. Sent: {1}.", count, bytesSent);
                        }
                    }

                    if (bytesSent == 0)
                    {
                        throw GenuineExceptions.Get_Send_TransportProblem();
                    }

                    offset += bytesSent;
                    count  -= bytesSent;

                    if (count <= 0)
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                // LOG:
                if (binaryLogWriter != null && binaryLogWriter[LogCategory.Transport] > 0)
                {
                    binaryLogWriter.WriteEvent(LogCategory.Transport, "TcpSocketInfo.Write",
                                               LogMessageType.SynchronousSendingStarted, ex, null, this.Remote == null ? null : this.Remote,
                                               binaryLogWriter[LogCategory.Transport] > 1 ? new MemoryStream(GenuineUtility.CutOutBuffer(buffer, offset, count)) : null,
                                               GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                               null, null, this.DbgConnectionId, 0, 0, 0, count.ToString(), null, null, null,
                                               "An exception is raised during synchronous sending.");
                }

                throw GenuineExceptions.Get_Send_TransportProblem();
            }
        }