Пример #1
0
    private static void ServerThread(object data)
    {
        NamedPipeServerStream pipeServer =
            new NamedPipeServerStream(PipeName, PipeDirection.InOut, numThreads);

        int threadId = Thread.CurrentThread.ManagedThreadId;

        logger.Log(string.Format("Start Telegram thread[{0}].", threadId));

        // Wait for a client to connect
        pipeServer.WaitForConnection();

        logger.Log(string.Format("Client connected on Telegram thread[{0}].", threadId));
        try
        {
            // Read the request from the client. Once the client has
            // written to the pipe its security token will be available.

            StreamString ss = new StreamString(pipeServer);

            // Verify our identity to the connected client using a
            // string that the client anticipates.

            string content = ss.ReadString();
            logger.Log(string.Format("Get Telegram message:\n{0}", content));

            logger.Log(string.Format("GetIncomeMessages {0}", content.Contains("GetIncomeMessages()")));

            if (content.Contains("GetIncomeMessages()"))
            {
                var msgs = botClient.GetIncomeMessages();
                if (msgs == null)
                {
                    msgs = "{[===[No new messages]===]}";
                }

                ReadMessagesToStream msgsReader = new ReadMessagesToStream(ss, msgs);
                ss.ClearMessages += new OnReplyHandler(BotClient.ClearIncomeMessages);

                pipeServer.RunAsClient(msgsReader.Start);
            }
            else
            {
                botClient.Send(content);
            }
        }
        // Catch the IOException that is raised if the pipe is broken
        // or disconnected.
        catch (IOException e)
        {
            logger.Log(string.Format("Telegram ServerThread ERROR: {0}", e.Message), true);
        }
        pipeServer.Close();
    }