SylixOS嵌入式操作系统|论坛

 找回密码
 注册
搜索
查看: 1001|回复: 0
打印 上一主题 下一主题

采用vsoa架构的 client示例代码

[复制链接]

7

主题

8

帖子

47

积分

新手上路

Rank: 1

积分
47
跳转到指定楼层
楼主
发表于 2022-12-22 19:11:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef SYLIXOS
#include <sys/vproc.h>
#endif
#include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "vsoa_client.h"
#include "vsoa_position.h"
#include "vsoa_platform.h"
#include "foo_jstruct.h"

/* My server password */
#define MY_SERVER_PASSWD "123456"

/* My client */
static vsoa_client_t *client;

/*
* On message
*/
static void onmessage (void *arg, struct vsoa_client *client, vsoa_url_t *url, vsoa_payload_t *payload)
{
    printf("On message, URL: %.*s payload: %.*s\n",
           (int)url->url_len, url->url, (int)payload->param_len, payload->param);
}

/*
* Get foo callback
*/
static void get_foo (void *arg, struct vsoa_client *client, vsoa_header_t *vsoa_hdr, vsoa_payload_t *payload)
{
    struct data data;

    if (!vsoa_hdr) {
        fprintf(stderr, "VSOA server /foo reply timeout!\n");
        return;
    }

    if (!foo_json_parse(&data, payload->param, payload->param_len)) {
        fprintf(stderr, "VSOA server /foo reply format error!\n");
        return;
    }

    printf("RPC /foo reply foo: %d\n", data.foo);

    foo_json_parse_free(&data);
}

/*
* main function
*/
int main (int argc, char **argv)
{
    int max_fd, cnt;
    fd_set fds;
    char info[256];
    time_t now, last;
    vsoa_url_t url;
    socklen_t serv_len = sizeof(struct sockaddr_in);
    struct sockaddr_in addr;
    struct timespec timeout = { 1, 0 };

#ifdef SYLIXOS
    vprocExitModeSet(getpid(), LW_VPROC_EXIT_FORCE);
#endif

    if (!vsoa_position_lookup(AF_INET, "c_server",
                              (struct sockaddr *)&addr, &serv_len, NULL, &timeout)) {
        fprintf(stderr, "Can not found VSOA server!\n");
        return  (-1);
    }

    client = vsoa_client_create(onmessage, NULL);
    if (!client) {
        fprintf(stderr, "Can not create VSOA client!\n");
        return  (-1);
    }

    if (!vsoa_client_connect(client, (struct sockaddr *)&addr, sizeof(struct sockaddr_in),
                             &timeout, MY_SERVER_PASSWD, info, sizeof(info))) {
        vsoa_client_close(client);
        fprintf(stderr, "Can not connect to VSOA server!\n");
        return  (-1);
    }

    printf("Server (%s) connected\n", info);

    url.url     = "/foo";
    url.url_len = strlen(url.url);
    vsoa_client_subscribe(client, &url, NULL, NULL, NULL);

    time(&last);

    while (1) {
        FD_ZERO(&fds);
        max_fd = vsoa_client_fds(client, &fds);

        cnt = pselect(max_fd + 1, &fds, NULL, NULL, &timeout, NULL);
        if (cnt > 0) {
            if (!vsoa_client_input_fds(client, &fds)) {
                fprintf(stderr, "Connection lost!\n");
                return  (-1);
            }
        }

        time(&now);
        if (now - last >= 5) {
            last = now;
            if (!vsoa_client_call(client, VSOA_CLIENT_RPC_METHOD_GET,
                                  &url, NULL, get_foo, NULL, &timeout)) {
                vsoa_client_close(client);
                fprintf(stderr, "Can not RPC call to VSOA server!\n");
                return  (-1);
            }
        }
    }
}
/*
* end
*/
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|Acoinfo Inc. All Rights Reserved. ( 京ICP备15059934号

GMT+8, 2024-4-26 14:04 , Processed in 0.022425 second(s), 21 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表