Пример #1
0
 /// <summary>
 /// Returns the simples form of a string
 /// </summary>
 /// <param name="s"></param>
 /// <returns>a trimmed and set to lowercase string or null if string is empty</returns>
 public static string Lowered(string s)
 {
     s = CMisc.Trimmed(s);
     if (string.IsNullOrEmpty(s))
     {
         return(null);
     }
     return(s.ToLower());
 }
Пример #2
0
 /// <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);
 }