public Client(Thread mainThread, CThread thread, TcpClient client, CStreamServerSettings settings)
 {
     Tcp        = client;
     Settings   = settings;
     StreamIO   = new CStreamServerIO(client, Settings);
     MainThread = mainThread;
     Thread     = thread;
 }
Esempio n. 2
0
 /// <summary>
 /// Start a client thread to send and receive data.
 /// The caller is warned when a reply is received by a call to the specified function (<see cref="SendAsyncType"/>).
 /// </summary>
 /// <param name="sendAsync">Settings to start the thread</param>
 /// <param name="request">The request as bytes array</param>
 /// <param name="addSizeHeader">Indicates whether or not adding a size header when sending the request</param>
 /// <param name="lineExchanges">Indicates whether (true) or not (false) the exchanges complete by a new line, not using the size header.
 /// If set to true no buffer size is never used during the exchanges (present or not) and the EOT is always represented by a CR+LF.
 /// Setting this parameter to true supersedes the addSizeHeader one</param>
 /// <param name="EOT"></param>
 /// <returns>A <see cref="CThread"/> object if the thread has been started, null otherwise</returns>
 public static CThread SendAsync(SendAsyncType sendAsync, byte[] request, bool addSizeHeader, bool lineExchanges = false, string EOT = CStreamIO.CRLF)
 {
     if (null == sendAsync ||
         null == sendAsync.Settings ||
         !sendAsync.Settings.IsValid ||
         null == request ||
         0 == request.Length)
     {
         return(null);
     }
     try
     {
         // prepare working settings
         ClientThreadType threadParams = new ClientThreadType()
         {
             SendAsync     = sendAsync,
             Request       = request,
             AddSizeHeader = addSizeHeader,
             ClientOnly    = null == sendAsync.OnReply,
             LineExchanges = lineExchanges,
             EOT           = EOT,
         };
         // prepare the thread object
         CThread thread = new CThread();
         //if (thread.Start(SendAsyncThreadMethod, sendAsync.ThreadData, threadParams, null, sendAsync.OnTerminate, true))
         if (thread.Start(SendAsyncThreadMethod, sendAsync.ThreadData, threadParams, null, true))
         {
             return(thread);
         }
         else
         {
             thread = null;
         }
     }
     catch (Exception ex)
     {
         CLog.AddException($"{MethodBase.GetCurrentMethod().Module.Name}.{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}", ex);
     }
     return(null);
 }
 /// <summary>
 /// Start a server thread to receive messages
 /// </summary>
 /// <param name="type">The settings to use to start the server</param>
 /// <param name="sync">Indicates whether the server must act synchronously or not.
 /// A synchronous server forwards replies inside the same thread, preventing receiving messages in the meantime.
 /// An asynchronous server forwards replies inside another thread, allowing receiving messages at the same time.</param>
 /// <returns>A <see cref="CThread"/> object if the thread has been started, null otherwise</returns>
 public static CThread StartServer(StartServerType type, bool sync)
 {
     if (null == type || !type.IsValid)
     {
         return(null);
     }
     try
     {
         // prepare the thread object
         CThread thread = new CThread();
         bool    f;
         if (sync)
         {
             f = thread.Start(SyncStreamServerMethod, type.ThreadData, new StreamServerMethodType()
             {
                 startServerType = type,
             });
         }
         else
         {
             f = thread.Start(AsyncStreamServerMainMethod, type.ThreadData, new StreamServerMethodType()
             {
                 startServerType = type,
             });
         }
         if (f)
         {
             return(thread);
         }
     }
     catch (Exception ex)
     {
         CLog.AddException(MethodBase.GetCurrentMethod().Name, ex);
     }
     return(null);
 }
Esempio n. 4
0
        private static int SendAsyncThreadMethod(CThread thread, object o)
        {
            SendAsyncEnum    res          = SendAsyncEnum.KO;
            ClientThreadType threadParams = (ClientThreadType)o;

            if (null != threadParams.SendAsync.OnReply)
            {
                // send & receive
                if (threadParams.LineExchanges)
                {
                    string reply = ConnectSendReceiveLine(threadParams.SendAsync.Settings, Encoding.UTF8.GetString(threadParams.Request), out bool error, threadParams.EOT);
                    if (string.IsNullOrEmpty(reply))
                    {
                        res = SendAsyncEnum.NoData;
                    }
                    else
                    {
                        // forward reply to the caller
                        if (threadParams.SendAsync.OnReply(Encoding.UTF8.GetBytes(reply), error, thread, threadParams.SendAsync.Parameters))
                        {
                            res = SendAsyncEnum.OK;
                        }
                        else
                        {
                            res = SendAsyncEnum.ReceiveError;
                        }
                    }
                }
                else
                {
                    byte[] reply = ConnectSendReceive(threadParams.SendAsync.Settings, threadParams.Request, threadParams.AddSizeHeader, out int announcedSize, out bool error);
                    if (null == reply || 0 == reply.Length)
                    {
                        res = SendAsyncEnum.NoData;
                    }
                    else if (reply.Length != announcedSize)
                    {
                        res = SendAsyncEnum.ReceiveError;
                    }
                    else
                    {
                        // forward reply to the caller
                        if (threadParams.SendAsync.OnReply(reply, error, thread, threadParams.SendAsync.Parameters))
                        {
                            res = SendAsyncEnum.OK;
                        }
                        else
                        {
                            res = SendAsyncEnum.ReceiveError;
                        }
                    }
                }
            }
            else
            {
                // send only
                if (threadParams.LineExchanges)
                {
                    if (ConnectSendLine(threadParams.SendAsync.Settings, Encoding.UTF8.GetString(threadParams.Request), threadParams.EOT))
                    {
                        res = SendAsyncEnum.OK;
                    }
                    else
                    {
                        res = SendAsyncEnum.SendError;
                    }
                }
                else
                {
                    if (ConnectSend(threadParams.SendAsync.Settings, threadParams.Request, threadParams.AddSizeHeader))
                    {
                        res = SendAsyncEnum.OK;
                    }
                    else
                    {
                        res = SendAsyncEnum.SendError;
                    }
                }
            }
            return((int)res);
        }