/// <summary>
        /// Sends a message synchronously. Does not process exceptions!
        /// </summary>
        /// <param name="content">The content being sent to the remote host.</param>
        /// <param name="timeout">The sending must be completed before this moment.</param>
        public void LowLevel_SendSync(Stream content, int timeout)
        {
            // apply the connection-level security session
            if (this.ConnectionLevelSecurity != null)
            {
                GenuineChunkedStream outputStream = new GenuineChunkedStream(true);
                this.ConnectionLevelSecurity.Encrypt(content, outputStream);
                content = outputStream;
            }

            for ( ; ;)
            {
                if (!this.IsValid)
                {
                    throw GenuineExceptions.Get_Processing_TransportConnectionFailed();
                }
                if (!GenuineUtility.WaitOne(_namedEventRemoteReadCompleted.ManualResetEvent, GenuineUtility.GetMillisecondsLeft(timeout)))
                {
                    throw GenuineExceptions.Get_Send_Timeout();
                }
                if (this._closed != 0)
                {
                    throw GenuineExceptions.Get_Receive_ConnectionClosed();
                }
                this._namedEventRemoteReadCompleted.ManualResetEvent.Reset();

                // read the next portion
                int bytesRead = content.Read(this._sendBuffer, 0, this._sendSpaceSize);

                // LOG:
                BinaryLogWriter binaryLogWriter = this.ITransportContext.BinaryLogWriter;
                if (binaryLogWriter != null && binaryLogWriter[LogCategory.LowLevelTransport] > 0)
                {
                    binaryLogWriter.WriteTransportContentEvent(LogCategory.LowLevelTransport, "GenuineSaredMemoryConnection.LowLevel_SendSync",
                                                               LogMessageType.LowLevelTransport_AsyncSendingInitiating, null, null, this.Remote,
                                                               binaryLogWriter[LogCategory.LowLevelTransport] > 1 ? new MemoryStream(this._sendBuffer, 0, bytesRead) : null,
                                                               GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                                               this.DbgConnectionId, bytesRead,
                                                               null, null, null,
                                                               "Content sending.");
                }

                Marshal.Copy(this._sendBuffer, 0,
                             (IntPtr)(this._pointer.ToInt32() + this._sendOffset + SHARE_CONTENT_OFFSET),
                             bytesRead);
                this._MessageToSendTotalSize  = bytesRead;
                this._MessageToSendFinishFlag = bytesRead < this._sendSpaceSize ? 1 : 0;

                this._namedEventWriteCompleted.ManualResetEvent.Set();

                if (bytesRead < this._sendSpaceSize)
                {
                    this.UpdateLastTimeAMessageWasSent();
                    return;
                }
            }
        }
Esempio n. 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)
        {
            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();
            }
        }
        /// <summary>
        /// Synchronously reads the next network packet if it is available.
        /// </summary>
        private void ReadNextPortion()
        {
            if (!this.IsValid)
            {
                throw GenuineExceptions.Get_Processing_TransportConnectionFailed();
            }

            this._namedEventReadCompleted.ManualResetEvent.Set();
            if (!GenuineUtility.WaitOne(this._namedEventRemoteWriteCompleted.ManualResetEvent, GenuineUtility.GetMillisecondsLeft(this._readDeadline)))
            {
                throw GenuineExceptions.Get_Send_Timeout();
            }
            this._namedEventRemoteWriteCompleted.ManualResetEvent.Reset();

            this._validLength     = this._MessageToReceiveTotalSize;
            this._currentPosition = 0;
            this._messageRead     = this._MessageToReceiveFinishFlag != 0;

            // LOG:
            BinaryLogWriter binaryLogWriter = this.ITransportContext.BinaryLogWriter;

            if (binaryLogWriter != null && binaryLogWriter[LogCategory.LowLevelTransport] > 0)
            {
                bool   writeContent = binaryLogWriter[LogCategory.LowLevelTransport] > 1;
                byte[] receivedData = new byte[this._validLength];
                Marshal.Copy((IntPtr)(this._pointer.ToInt32() + this._receiveOffset + this._currentPosition + SHARE_CONTENT_OFFSET),
                             receivedData, 0, this._validLength);

                binaryLogWriter.WriteTransportContentEvent(LogCategory.LowLevelTransport, "GenuineSaredMemoryConnection.ReadNextPortion",
                                                           LogMessageType.LowLevelTransport_SyncReceivingCompleted, null, null, this.Remote,
                                                           writeContent ? new MemoryStream(receivedData, 0, this._validLength) : null,
                                                           GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                                           this.DbgConnectionId, this._validLength,
                                                           null, null, null,
                                                           "Content received.");
            }
        }
Esempio n. 4
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();
            }
        }
        /// <summary>
        /// Sends the message through the specified connection.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="sharedMemoryConnection">The connection.</param>
        private void SendSync(Message message, SharedMemoryConnection sharedMemoryConnection)
        {
            BinaryLogWriter binaryLogWriter = this.ITransportContext.BinaryLogWriter;

            try
            {
                // LOG:
                if (binaryLogWriter != null && binaryLogWriter[LogCategory.Connection] > 0)
                {
                    binaryLogWriter.WriteEvent(LogCategory.Connection, "SharedMemoryConnectionManager.SendSync",
                                               LogMessageType.MessageIsSentSynchronously, null, message, sharedMemoryConnection.Remote, null,
                                               GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                               sharedMemoryConnection.ConnectionLevelSecurity,
                                               sharedMemoryConnection.ConnectionLevelSecurity == null ? null : sharedMemoryConnection.ConnectionLevelSecurity.Name,
                                               sharedMemoryConnection.DbgConnectionId, 0, 0, 0, null, null, null, null,
                                               "Message is sent synchronously through the Shared Memory connection.");
                }

                // now that we have the connection, obtain the write access
                int sendTimeout = GenuineUtility.GetTimeout(this._sendTimeoutSpan);
                if (GenuineUtility.IsTimeoutExpired(message.FinishTime, sendTimeout))
                {
                    sendTimeout = message.FinishTime;
                }

                try
                {
                    if (!Monitor.TryEnter(sharedMemoryConnection.WriteAccess, GenuineUtility.GetMillisecondsLeft(sendTimeout)))
                    {
                        throw GenuineExceptions.Get_Send_Timeout();
                    }
                }
                catch
                {
                    throw GenuineExceptions.Get_Send_Timeout();
                }

                // the write access was obtained, send the message
                try
                {
                    sharedMemoryConnection.LowLevel_SendSync(message.SerializedContent, sendTimeout);
                }
                catch (Exception ex)
                {
                    this.ConnectionFailed(ex, sharedMemoryConnection);
                    throw;
                }
                finally
                {
                    Monitor.Exit(sharedMemoryConnection.WriteAccess);
                }

                message.Dispose();
            }
            catch (Exception ex)
            {
                // LOG:
                if (binaryLogWriter != null && binaryLogWriter[LogCategory.Connection] > 0)
                {
                    binaryLogWriter.WriteEvent(LogCategory.Connection, "SharedMemoryConnectionManager.SendSync",
                                               LogMessageType.MessageIsSentSynchronously, ex, message, sharedMemoryConnection.Remote, null,
                                               GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                               null, null, sharedMemoryConnection.DbgConnectionId, 0, 0, 0, null, null, null, null,
                                               "The message cannot be delivered.");
                }

                throw;
            }
            finally
            {
                message.Dispose();
            }
        }