コード例 #1
0
ファイル: CStream.cs プロジェクト: pmespace/PMS.COMMON
 /// <summary>
 /// Disconnect a stream freeing resources.
 /// </summary>
 /// <param name="stream">the stream to disconnect</param>
 public static void Disconnect(CStreamIO stream)
 {
     if (null != stream)
     {
         stream.Close();
     }
 }
コード例 #2
0
ファイル: CStream.cs プロジェクト: pmespace/PMS.COMMON
 /// <summary>
 /// This function prevents using any size header, using CR+LF as an EOT
 /// </summary>
 /// <param name="stream"></param>
 /// <param name="request"></param>
 /// <param name="error"></param>
 /// <param name="EOT"></param>
 /// <returns></returns>
 public static string SendReceiveLine(CStreamIO stream, string request, out bool error, string EOT = CStreamIO.CRLF)
 {
     error = false;
     if (SendLine(stream, request, EOT))
     {
         return(ReceiveLine(stream, out error, EOT));
     }
     return(null);
 }
コード例 #3
0
ファイル: CStream.cs プロジェクト: pmespace/PMS.COMMON
 /// <summary>
 /// Send (<see cref="Send(CStreamIO, byte[], bool)"/> and <see cref="Send(CStreamIO, string)"/>),
 /// then receive data (<see cref="Receive(CStreamIO, out int, out bool, bool)"/>.
 /// The stream must pre-exist
 /// </summary>
 /// <param name="stream">The connected stream</param>
 /// <param name="request">A array of bytes to send</param>
 /// <param name="announcedSize">The size of the reply as announced by the sender</param>
 /// <param name="addSizeHeader">Indicates whether a buffer size block must be added before the buffer to send</param>
 /// <param name="error">True indicates the function ended up with a error, false otherwise</param>
 /// <returns></returns>
 public static byte[] SendReceive(CStreamIO stream, byte[] request, bool addSizeHeader, out int announcedSize, out bool error)
 {
     announcedSize = 0;
     error         = false;
     if (Send(stream, request, addSizeHeader))
     {
         return(Receive(stream, out announcedSize, out error, addSizeHeader));
     }
     return(null);
 }
コード例 #4
0
ファイル: CStream.cs プロジェクト: pmespace/PMS.COMMON
 /// <summary>
 /// Receive data on the indicated stream.
 /// The buffer MUST begin with a size header of <see cref="CStreamBase.LengthBufferSize"/>
 /// </summary>
 /// <param name="stream">The connected stream</param>
 /// <param name="announcedSize">The size of the reply as announced by the sender</param>
 /// <param name="sizeHeaderAdded">Indicates whether a buffer size header was natively or not inside the request.
 /// If TRUE then the size header has been added by the system meaning the application does not care about it and won't care about it inside the received buffer which will be returned without any size header.
 /// If FALSE then the size header was already part of the buffer meaning the application added it and cares about it. The received buffer must therefore contain the size header.</param>
 /// <param name="error">True indicates the function ended up with an error as no more data was available, false otherwise</param>
 /// <returns>An arry of bytes received in response or if an error occured. In case of a client only request, the function returns the request, as no reply can be returned, if everything went right</returns>
 public static byte[] Receive(CStreamIO stream, out int announcedSize, out bool error, bool sizeHeaderAdded)
 {
     byte[] reply = null;
     announcedSize = 0;
     error         = false;
     if (null == stream)
     {
         return(null);
     }
     try
     {
         // Read message from the server
         byte[] tmp = stream.Receive(out announcedSize);
         if (null != tmp)
         {
             CLog.DEBUG($"{MethodBase.GetCurrentMethod().Module.Name}.{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}", $"Received message of {(sizeHeaderAdded ? tmp.Length : tmp.Length - (int)stream.LengthBufferSize)} actual bytes (announcing {announcedSize} bytes)");
             // rebuild the buffer is required
             if (!sizeHeaderAdded)
             {
                 // the request natively contained a size header, meaningfull to the application, we therefore must reinsert the size header inside the received buffer
                 reply = new byte[tmp.Length + stream.LengthBufferSize];
                 byte[] bb = CMisc.SetBytesFromIntegralTypeValue(tmp.Length, stream.LengthBufferSize);
                 Buffer.BlockCopy(bb, 0, reply, 0, stream.LengthBufferSize);
                 Buffer.BlockCopy(tmp, 0, reply, stream.LengthBufferSize, tmp.Length);
             }
             else
             {
                 reply = tmp;
             }
         }
         else if (0 != announcedSize)
         {
             CLog.Add("No data has been received though expecting some (invalid announced length,...)", TLog.ERROR);
             error = true;
         }
         else
         {
             CLog.DEBUG($"{MethodBase.GetCurrentMethod().Module.Name}.{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}", $"No data has been received");
         }
     }
     catch (Exception ex)
     {
         CLog.AddException($"{MethodBase.GetCurrentMethod().Module.Name}.{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}", ex);
     }
     return(reply);
 }
コード例 #5
0
ファイル: CStream.cs プロジェクト: pmespace/PMS.COMMON
 /// <summary>
 /// Refer to <see cref="CStreamIO.ReceiveLine(string)"/>
 /// The string does not need to begin by a size header of <see cref="CStreamBase.LengthBufferSize"/> which will be ignored.
 /// The string MUST however finish (or at least contain) a CR+LF sequence (or contain it) marking the EOT.
 /// </summary>
 /// <param name="stream"></param>
 /// <param name="error">True indicates the function ended up with a error, false otherwise</param>
 /// <param name="EOT">A string which if found marks the end of transmission</param>
 /// <returns></returns>
 public static string ReceiveLine(CStreamIO stream, out bool error, string EOT = CStreamIO.CRLF)
 {
     error = false;
     if (null == stream)
     {
         return(null);
     }
     try
     {
         string s = stream.ReceiveLine(EOT);
         CLog.DEBUG($"{MethodBase.GetCurrentMethod().Module.Name}.{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}", $"Received string message of {(string.IsNullOrEmpty(s) ? 0 : s.Length)} characters");
         return(s);
     }
     catch (Exception ex)
     {
         error = true;
         CLog.AddException($"{MethodBase.GetCurrentMethod().Module.Name}.{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}", ex);
     }
     return(null);
 }
コード例 #6
0
ファイル: CStream.cs プロジェクト: pmespace/PMS.COMMON
 /// <summary>
 /// Refer to <see cref="CStreamIO.SendLine(string, string)"/>
 /// This function prevents using any size header, using CR+LF as an EOT
 /// </summary>
 /// <param name="stream"></param>
 /// <param name="request"></param>
 /// <param name="EOT"></param>
 /// <returns></returns>
 public static bool SendLine(CStreamIO stream, string request, string EOT = CStreamIO.CRLF)
 {
     if (null == stream || string.IsNullOrEmpty(request))
     {
         return(false);
     }
     try
     {
         CLog.DEBUG($"{MethodBase.GetCurrentMethod().Module.Name}.{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}", $"About to send string message of {request.Length} characters");
         if (stream.SendLine(request, EOT))
         {
             return(true);
         }
         CLog.Add($"An error has occurred while sending the string message", TLog.ERROR);
     }
     catch (Exception ex)
     {
         CLog.AddException($"{MethodBase.GetCurrentMethod().Module.Name}.{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}", ex);
     }
     return(false);
 }
コード例 #7
0
ファイル: CStream.cs プロジェクト: pmespace/PMS.COMMON
 /// <summary>
 /// Send data on the given stream.
 /// </summary>
 /// <param name="stream">The connected stream</param>
 /// <param name="request">A array of bytes to send</param>
 /// <param name="addSizeHeader">Indicates whether a buffer size block must be added before the buffer to send</param>
 /// <returns>An arry of bytes received in response or if an error occured. In case of a client only request, the function returns the request, as no reply can be returned, if everything went right</returns>
 public static bool Send(CStreamIO stream, byte[] request, bool addSizeHeader)
 {
     if (null == stream || request.IsNullOrEmpty())
     {
         return(false);
     }
     try
     {
         // Send message to the server
         CLog.DEBUG($"{MethodBase.GetCurrentMethod().Module.Name}.{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}", $"About to send message of {(addSizeHeader ? request.Length : request.Length - (int)stream.LengthBufferSize)} bytes");
         if (stream.Send(request, addSizeHeader))
         {
             return(true);
         }
         // arrived here the message hasn't been sent
         CLog.Add($"An error has occurred while sending the message", TLog.ERROR);
     }
     catch (Exception ex)
     {
         CLog.AddException($"{MethodBase.GetCurrentMethod().Module.Name}.{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}", ex);
     }
     return(false);
 }
コード例 #8
0
ファイル: CStream.cs プロジェクト: pmespace/PMS.COMMON
 /// <summary>
 /// Refer to <see cref="SendReceive(CStreamIO, byte[], bool, out int, out bool)"/>
 /// </summary>
 /// <param name="stream"></param>
 /// <param name="request"></param>
 /// <param name="announcedSize"></param>
 /// <param name="error"></param>
 /// <returns></returns>
 public static string SendReceive(CStreamIO stream, string request, out int announcedSize, out bool error)
 {
     byte[] reply = SendReceive(stream, null != request ? Encoding.UTF8.GetBytes(request) : null, true, out announcedSize, out error);
     return(null != reply ? Encoding.UTF8.GetString(reply) : null);
 }
コード例 #9
0
ファイル: CStream.cs プロジェクト: pmespace/PMS.COMMON
 /// <summary>
 /// Refer to <see cref="Receive(CStreamIO, out int, out bool, bool)"/>
 /// </summary>
 /// <param name="stream"></param>
 /// <param name="announcedSize"></param>
 /// <param name="error"></param>
 /// <returns></returns>
 public static string Receive(CStreamIO stream, out int announcedSize, out bool error)
 {
     byte[] reply = Receive(stream, out announcedSize, out error, true);
     return(null != reply ? Encoding.UTF8.GetString(reply) : null);
 }
コード例 #10
0
ファイル: CStream.cs プロジェクト: pmespace/PMS.COMMON
 /// <summary>
 /// Refer to <see cref="Send(CStreamIO, byte[], bool)"/>
 /// </summary>
 /// <param name="stream"></param>
 /// <param name="request"></param>
 /// <returns></returns>
 public static bool Send(CStreamIO stream, string request)
 {
     byte[] brequest = string.IsNullOrEmpty(request) ? null : Encoding.UTF8.GetBytes(request);
     return(Send(stream, brequest, true));
 }