⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rpccli.c

📁 PB 熟悉的哥们希望大家可以互相学习一下
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include "rpcsds.h"
#include "test_intf.h"

#ifdef _VXWORKS
    #include <ioLib.h>
    #define MSG_NOSIGNAL 0
#endif 

#define DATA_THROUGH_A_NETWORK
//#define DATA_THROUGH_A_FILE

#define SERVER_ADDRESS  "127.0.0.1"
//#define SERVER_ADDRESS  "192.168.255.1"

#ifdef DATA_THROUGH_A_FILE
    typedef struct tag_test_params
    {
        FILE* w;
        FILE* r;
    } test_params_t, * test_params_p;

    int test_intf_write_stream(void* buf_, unsigned int buf_sz, void* param, unsigned int network_order)
    {

        char* buf = buf_;
        unsigned int i;
        for (i = 0; i < buf_sz; i++)
        {
            printf("%02hhx ", buf[i]);
        };
        printf("\n");

        fwrite(buf_, 1, buf_sz, ((test_params_p)param)->w);

        return 0;
    };

    int test_intf_read_stream(void* buf_, unsigned int buf_sz, void* param, unsigned int network_order, unsigned int*
                              read_sz)
    {

        char* buf = buf_;
        unsigned int i;

        *read_sz = fread(buf_, 1, buf_sz, ((test_params_p)param)->r);

        for (i = 0; i < buf_sz; i++)
        {
            printf("%02hhx ", buf[i]);
        };
        printf("\n");

        return 0;
    };
#endif 

#ifdef DATA_THROUGH_A_NETWORK

    #ifndef WIN32
        #include <sys/socket.h>
        #include <sys/ioctl.h>
        #include <netinet/tcp.h>
        #include <netinet/in.h>
        #include <arpa/inet.h>
        #if defined(_HPUX) || defined(_SOLARIS) || defined(_QNX)
            #define MSG_NOSIGNAL 0
        #endif 
    #else 
        #include <winsock2.h>
        #define MSG_NOSIGNAL 0
    #endif 



    /*
    #include <netinet/tcp.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <sys/ioctl.h>
    #include <netinet/in.h>
     */

    typedef struct tag_test_params
    {

        int tl;

    } test_params_t, * test_params_p;

    int mco_init_network_client_interface(test_params_p nctx, char* address, unsigned short port)
    {

        struct sockaddr_in sin;

        #ifdef WIN32
            WSADATA wsa;
            int r = WSAStartup(MAKEWORD(2, 0), &wsa);
            if (r != 0)
            {
                WSACleanup();
                return 1;
            };
        #endif 

        /* initialize a socket */
        nctx->tl = (int)socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

        /* make a connection */
        sin.sin_family = AF_INET;
        sin.sin_port = htons(port);
        sin.sin_addr.s_addr = inet_addr(address);

        if (connect(nctx->tl, (struct sockaddr*) &sin, sizeof(sin)) < 0)
        {
            return 1;
        }

        return 0;
    };

    int mco_clear_network_client_interface(test_params_p nctx)
    {

        #ifndef WIN32  
            #ifdef _VXWORKS
                shutdown(nctx->tl, 0);
            #else 
                shutdown(nctx->tl, SHUT_RDWR);
            #endif 
            close(nctx->tl);
        #else /* WIN32 */
            shutdown(nctx->tl, SD_BOTH);
            closesocket(nctx->tl);
        #endif /* WIN32 */

        return 0;
    };

    int test_intf_write_stream(void* buf_, unsigned int buf_sz, void* param, unsigned int network_order)
    {

        test_params_p nctx = (test_params_p)param;
        char* buf = buf_;
        unsigned int i;

        for (i = 0; i < buf_sz; i++)
        {
            printf("%02hhx ", buf[i]);
        };
        printf("\n");

        if ( - 1 == send(nctx->tl, buf_, buf_sz, MSG_NOSIGNAL))
        {
            return 1;
        }

        return 0;
    };

    int test_intf_read_stream(void* buf_, unsigned int buf_sz, void* param, unsigned int network_order, unsigned int*
                              read_sz)
    {

        test_params_p nctx = (test_params_p)param;
        char* buf = buf_;
        unsigned int i;

        if ( - 1 == ((*read_sz) = recv(nctx->tl, buf_, buf_sz, MSG_NOSIGNAL)))
        {
            return 1;
        }

        for (i = 0; i < buf_sz; i++)
        {
            printf("%02hhx ", buf[i]);
        };
        printf("\n");

        return 0;
    };

#endif 

void mco_rpc_fatal_error(int e)
{

    printf("Fatal error: %d\n", e);
    exit(1);
};

extern mco_rpc_context_t test_intf_ctx;

int main(int argc, char* argv[])
{

    unsigned short usa[4] = 
    {
        0x10, 0x20, 0x30, 0x40
    };
    unsigned short usb[3] = 
    {
        0x2000, 0x3000, 0x4000
    };
    int result = 0;
    test_params_t params;

    int5 ints = 
    {
        1, 2, 3, 4, 5
    };
    test_struct_t st;
    test_struct_variable_t vs;

    test_struct_union_t ua;
    test_struct_union_t ub;

    ua.tp = 0;
    ua.dt.a = 0x12345678;

    ub.tp = 1;
    ub.dt.b = "12345678";

    test_intf_ctx.param = &params;

    vs.a = usa;
    vs.b = usb;
    vs.a_sz = sizeof(usa);
    vs.b_len = sizeof(usb) / sizeof(usb[0]);

    st.a = 10;
    st.b = 'a';
    st.c = "ABCDEF";
    st.d = 0xF00F;

    st.e[0] =  - 1;
    st.e[1] = 0;
    st.e[2] = 1;
    st.f[0].a = 'b';
    st.f[0].b = 0xABCD;
    st.f[0].c = 0xFFFFAAAA;
    st.f[0].d = 10000000;

    st.f[1].a = 'x';
    st.f[1].b = 0xEFAB;
    st.f[1].c = 0x55555555;
    st.f[1].d = 0;

    #ifdef DATA_THROUGH_A_FILE
        params.r = fopen("test1_r.bin", "r");
        params.w = fopen("test1_w.bin", "w");
    #endif 

    #ifdef DATA_THROUGH_A_NETWORK
        /* put here the desired server IP address
        for example:
        if ( 0 != mco_init_network_client_interface( &params, "XXXX.XXXX.XXXX.XXXX", 8082 ) ) {
         */
        if (0 != mco_init_network_client_interface(&params, SERVER_ADDRESS, 8082))
        {
            printf("Unable to connect\n");
            return 1;
        };
    #endif 

    test_int(10);

    test_void_int(20);

    test_int_string("12345");

    test_int_int5(ints);

    test_int_pstruct(&st);

    test_int_variable_len(&vs);

    test_int_union(&ua);

    test_int_union(&ub);

    #ifdef DATA_THROUGH_A_NETWORK
        mco_clear_network_client_interface(&params);
    #endif 

    #ifdef DATA_THROUGH_A_FILE
        fclose(params.w);
        if (params.r)
        {
            fclose(params.r);
        }
    #endif 
    printf("End of the test\n");

    return result;
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -