注册 登录
自由的生活_软路由 返回首页

心想事成的个人空间 https://bbs.routerclub.com/?681 [收藏] [复制] [分享] [RSS]

日志

esp8266 使用asynctcp的实例

已有 3046 次阅读2016-12-25 10:41

ansynctcp这个库不错,可惜在官方上没有例子,我也搞了好久才正常运行。
下面是具体的实例函数。
 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
void client::runAsyncClient()
{


    tcpclient = new AsyncClient();

    if(!tcpclient)
    {
        DBGmsg("Creating AsyncClient class failed!\r\n");
        return;
    }

    tcpclient->onDisconnect([](void *obj, AsyncClient * c)
    {
        client *cc = (client *)obj;
        // if (cc->tcpclient)
        // {
        // c->free();
        // delete c;
        // cc->tcpclient = NULL;
        // }
        DBGmsg("Server Disconnect\r\n");
    }, this);

    tcpclient->onConnect(std::bind([](client * cc , AsyncClient * tcp)
    {
        DBGmsg("Server Connected\r\n");

    }, this, std::placeholders::_2));

    tcpclient->onData([](void *arg, AsyncClient * c, void *data, size_t len)
    {
        client *cc = (client *)arg;
        memcpy(cc->g_buf, data, len); //把随机数种复制过去

        cc->tcpbuffer = new AsyncTCPbuffer(c);
        if(! cc->tcpbuffer)
        {
            DBGmsg("creating Network class failed!\r\n");
            return;
        }
        DBGmsg("Connected\r\n");
        cc->zuangtai = CC_STAT_CONNETED;
        cc->data_call_back();

    }, this);


    tcpclient->onTimeout([](void *arg, AsyncClient * c , uint32_t time)
    {
        client *cc = (client *)arg;

        DBGmsg("Time Out\r\n");
    }, this);



    tcpclient->onError(std::bind([](client * cc , AsyncClient * tcp)
    {
        DBGmsg("CCcam server onError\r\n");


        // if (cc->tcpbuffer)
        // {
        // delete cc->tcpbuffer;
        // cc->tcpbuffer = NULL;
        // }

        // if (cc->tcpclient)
        // {
        // cc->tcpclient->free();
        // delete cc->tcpclient;
        // cc->tcpclient = NULL;
        // }

        // cc->runAsyncClient();// 重连
        cc->cc_disconnect_srv();// 重连
    }, this, std::placeholders::_2));



    if(!tcpclient->connect(server, CC_port))
    {
        DBGmsg("Connecte Error\r\n");
        delete tcpclient;
        tcpclient = NULL;
    }
}

路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

QQ|Archiver|手机版|小黑屋|软路由 ( 渝ICP备15001194号-1|渝公网安备 50011602500124号 )

GMT+8, 2024-3-29 12:56 , Processed in 0.082513 second(s), 5 queries , Gzip On, Redis On.

Powered by Discuz! X3.5 Licensed

© 2001-2023 Discuz! Team.

返回顶部