Thrift第七课C#语言测试C++服务器

场景

网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、微信平台小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了响水免费建站欢迎大家使用!

目前通过Thrift框架生成了C#代码,由于没有怎么接触过这门语言,因此直接启动程序模拟多个用户进行测试。在无法修改代码进行负载性测试的情况下,可以使用system函数启动程序,模拟多个用户的实际操作。

#include <windows.h>

int main()

{

while (1)

{

::system("E:/thriftserver/thrift-csharp/ThriftTest/bin/Debug/ThriftTest.exe");

::Sleep(1000);

}

return 0;

}

C#的实际代码如下

       static void ProcessThread(TProtocol protocol)

        {

            TProcessor processor = new PushMessageService.Processor(new CRecvMessageSErviceImpl());

            while (true)

            {

                try

                {

                    while (processor.Process(protocol, protocol)) { };

                    return;

                }

                catch (System.NullReferenceException tt)

                {

                    return;

                }

                catch (Thrift.Transport.TTransportException tt)

                {

                    //服务器没有启动,会抛出此异常

                    return;

                }

            }

        }

        static void MonitorThread(TTransport trans, Action<string> callback)

        {

            while (true)

            {

                try

                {

                    if (!trans.Peek())

                    {

                        callback("connect close\n");

                    }

                    Thread.Sleep(200);

                }

                catch (Thrift.TApplicationException ex)

                {

                    callback(ex.Message);

                    return;

                }

            }

        }

        static void Main(string[] args)

        {

            TTransport transport = new TBufferedTransport(new TSocket("192.168.1.110", 7001));

            TProtocol protocol = new TBinaryProtocol(transport);

            UploadMessageService.Client client = new UploadMessageService.Client(protocol);

            Action<TProtocol> processAction = new Action<TProtocol>(ProcessThread);

            Action<TTransport, Action<string>> monitorAction = new Action<TTransport, Action<string>>(MonitorThread);

            try

            {

                transport.Open();

                processAction.BeginInvoke(protocol, (result) =>

                {

                    processAction.EndInvoke(result);

                }, null);

                monitorAction.BeginInvoke(transport, (msg) =>

                {

                    Console.WriteLine("connect stop", msg);

                }, (result) =>

                {

                }, null);

                info.StrUserId = "admin";

                info.StrPassword = "admin";

                client.Login(info);

                }

            }

            catch (System.NullReferenceException  ttx)

            {

                //服务器停止运行,会抛出此异常

                return;

            }

            catch (System.IO.IOException ttx)

            {

                return;

            }

            catch (Thrift.Transport.TTransportException tt)

            {

                //服务器没有启动,会抛出此异常

                return;

            }

            //transport.Close();

//在这里模拟异常情况,没有正常关闭

        }

    }

分享标题:Thrift第七课C#语言测试C++服务器
文章链接:https://www.cdcxhl.com/article20/jisoco.html

成都网站建设公司_创新互联,为您提供面包屑导航微信公众号外贸建站动态网站网站制作Google

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都定制网站建设