コード例 #1
0
ファイル: block.c プロジェクト: Droppe/nanomsg
int main ()
{
    int rc;
    char buf [3];
    struct nn_thread thread;

    sb = nn_socket (AF_SP, NN_PAIR);
    errno_assert (sb != -1);
    rc = nn_bind (sb, SOCKET_ADDRESS);
    errno_assert (rc >= 0);
    sc = nn_socket (AF_SP, NN_PAIR);
    errno_assert (sc != -1);
    rc = nn_connect (sc, SOCKET_ADDRESS);
    errno_assert (rc >= 0);

    nn_thread_init (&thread, worker, NULL);

    rc = nn_recv (sb, buf, sizeof (buf), 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);
    rc = nn_recv (sb, buf, sizeof (buf), 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);

    nn_thread_term (&thread);

    rc = nn_close (sc);
    errno_assert (rc == 0);
    rc = nn_close (sb);
    errno_assert (rc == 0);

    return 0;
}
コード例 #2
0
/*returns 0 if success, otherwise __LINE__*/
static int stop_module(int publish_socket, BROKER_MODULEINFO* module_info)
{
    int  quit_result, close_result, thread_result, result;

    /*Codes_SRS_BROKER_17_021: [ This function shall send a quit signal to the worker thread by sending BROKER_MODULEINFO::quit_message_guid to the publish_socket. ]*/
    /* send the unique quite id for this module */
    if ((quit_result = nn_send(publish_socket, STRING_c_str(module_info->quit_message_guid), BROKER_GUID_SIZE, 0)) < 0)
    {
        /*Codes_SRS_BROKER_17_015: [ This function shall close the BROKER_MODULEINFO::receive_socket. ]*/
        /* at the cost of a data race, we will close the socket to terminate the thread */
        nn_close(module_info->receive_socket);
        LogError("unable to peacefully close thread for module [%p], nn_send error [%d], taking harsher methods", module_info, quit_result);
    }
    else
    {
        /*Codes_SRS_BROKER_02_001: [ Broker_RemoveModule shall lock BROKER_MODULEINFO::socket_lock. ]*/
        if (Lock(module_info->socket_lock) != LOCK_OK)
        {
            /*Codes_SRS_BROKER_17_015: [ This function shall close the BROKER_MODULEINFO::receive_socket. ]*/
            /* at the cost of a data race, we will close the socket to terminate the thread */
            nn_close(module_info->receive_socket);
            LogError("unable to peacefully close thread for module [%p], Lock error, taking harsher methods", module_info );
        }
        else
        {
            /*Codes_SRS_BROKER_17_015: [ This function shall close the BROKER_MODULEINFO::receive_socket. ]*/
            close_result = nn_close(module_info->receive_socket);
            if (close_result < 0)
            {
                LogError("Receive socket close failed for module at  item [%p] failed", module_info);
            }
            else
            {
                /*all is fine, thread will eventually stop and be joined*/
            }
            /*Codes_SRS_BROKER_02_003: [ After closing the socket, Broker_RemoveModule shall unlock BROKER_MODULEINFO::info_lock. ]*/
            if (Unlock(module_info->socket_lock) != LOCK_OK)
            {
                LogError("unable to unlock socket lock");
            }
        }
    }
    /*Codes_SRS_BROKER_13_104: [The function shall wait for the module's thread to exit by joining BROKER_MODULEINFO::thread via ThreadAPI_Join. ]*/
    if (ThreadAPI_Join(module_info->thread, &thread_result) != THREADAPI_OK)
    {
        result = __LINE__;
        LogError("ThreadAPI_Join() returned an error.");
    }
    else
    {
        result = 0;
    }
    return result;
}
コード例 #3
0
ファイル: device_demo.c プロジェクト: SEI-AMS/nanomsg
/*  The client runs just once, and then returns. */
int client (const char *url, const char *username)
{
    int fd;
    int rc;
    char *greeting;
    char *msg;

    fd = nn_socket (AF_SP, NN_REQ);
    if (fd < 0) {
        fprintf (stderr, "nn_socket: %s\n", nn_strerror (nn_errno ()));
        return (-1);
    }

    if (nn_connect (fd, url) < 0) {
        fprintf (stderr, "nn_socket: %s\n", nn_strerror (nn_errno ()));
        nn_close (fd);
        return (-1);        
    }

    usleep(1000);

    if (nn_send (fd, username, strlen (username), 0) < 0) {
        fprintf (stderr, "nn_send: %s\n", nn_strerror (nn_errno ()));
        nn_close (fd);
        return (-1);
    }

    /*  Here we ask the library to allocate response buffer for us (NN_MSG). */
    rc = nn_recv (fd, &msg, NN_MSG, 0);
    if (rc < 0) {
        fprintf (stderr, "nn_recv: %s\n", nn_strerror (nn_errno ()));
        nn_close (fd);
        return (-1);
    }

    nn_close (fd);

    /*  Response is not ASCIIZ terminated. */
    greeting = calloc (rc + 1, 1);
    if (greeting == NULL) {
        fprintf (stderr, "calloc: %s\n", strerror (errno));
        return (-1);
    }
    memcpy(greeting, msg, rc);

    nn_freemsg (msg);
    printf ("%s\n", greeting); 
    free (greeting);
    return (0);
}
コード例 #4
0
ファイル: test_nnxx_ext.cpp プロジェクト: Bigpet/nanomsgxx
int main() {
  struct nn_msgctl ctl1;
  struct nn_msgctl ctl2;
  int s1 = nn_socket (AF_SP_RAW, NN_REP);
  int s2 = nn_socket (AF_SP, NN_REQ);
  int s3 = nn_socket (AF_SP, NN_REQ);
  void *buf1 = nullptr;
  void *buf2 = nullptr;

  nnxx_assert (s1 >= 0);
  nnxx_assert (s2 >= 0);

  /*  Connecting sockets. */
  nnxx_assert (nn_bind (s1, "inproc://test") >= 0);
  nnxx_assert (nn_connect (s2, "inproc://test") >= 0);
  nnxx_assert (nn_connect (s3, "inproc://test") >= 0);

  /*  Sending requests. */
  nnxx_assert (nn_send (s2, "Hello World! (1)", 16, 0) == 16);
  nnxx_assert (nn_send (s3, "Hello World! (2)", 16, 0) == 16);

  /*  Recieving requests. */
  nnxx_assert (nn_recvfrom (s1, &buf1, NN_MSG, 0, &ctl1) == 16);
  nnxx_assert (nn_recvfrom (s1, &buf2, NN_MSG, 0, &ctl2) == 16);

  /*  Making sure we have the correct data. */
  nnxx_assert (memcmp (buf1, "Hello World! (1)", 16) == 0);
  nnxx_assert (memcmp (buf2, "Hello World! (2)", 16) == 0);

  /*  Sending responses back in reverse order. */
  nnxx_assert (nn_sendto (s1, &buf2, NN_MSG, 0, &ctl2) == 16);
  nnxx_assert (nn_sendto (s1, &buf1, NN_MSG, 0, &ctl1) == 16);

  /*  Recieving responses. */
  nnxx_assert (nn_recv (s2, &buf1, NN_MSG, 0) == 16);
  nnxx_assert (nn_recv (s3, &buf2, NN_MSG, 0) == 16);

  /*  Making sure the clients got the right responses. */
  nnxx_assert (memcmp (buf1, "Hello World! (1)", 16) == 0);
  nnxx_assert (memcmp (buf2, "Hello World! (2)", 16) == 0);

  /*  Releasing resources. */
  nn_freemsg (buf2);
  nn_freemsg (buf1);
  nn_close (s2);
  nn_close (s1);
  return nnxx::unittest::result;
}
コード例 #5
0
ファイル: pubsub.c プロジェクト: Neopallium/nanomsg
int main ()
{
    int rc;
    int pub;
    int sub1;
    int sub2;
    char buf [3];

    pub = nn_socket (AF_SP, NN_PUB);
    errno_assert (pub != -1);
    rc = nn_bind (pub, SOCKET_ADDRESS);
    errno_assert (rc >= 0);
    sub1 = nn_socket (AF_SP, NN_SUB);
    errno_assert (sub1 != -1);
    rc = nn_setsockopt (sub1, NN_SUB, NN_SUBSCRIBE, "", 0);
    errno_assert (rc == 0);
    rc = nn_connect (sub1, SOCKET_ADDRESS);
    errno_assert (rc >= 0);
    sub2 = nn_socket (AF_SP, NN_SUB);
    errno_assert (sub2 != -1);
    rc = nn_setsockopt (sub2, NN_SUB, NN_SUBSCRIBE, "", 0);
    errno_assert (rc == 0);
    rc = nn_connect (sub2, SOCKET_ADDRESS);
    errno_assert (rc >= 0);

    /*  Wait till connections are established to prevent message loss. */
    nn_sleep (10);

    rc = nn_send (pub, "ABC", 3, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);

    rc = nn_recv (sub1, buf, sizeof (buf), 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);
    rc = nn_recv (sub2, buf, sizeof (buf), 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);

    rc = nn_close (pub);
    errno_assert (rc == 0);
    rc = nn_close (sub1);
    errno_assert (rc == 0);    
    rc = nn_close (sub2);
    errno_assert (rc == 0);

    return 0;
}
コード例 #6
0
ファイル: iovec.c プロジェクト: Droppe/nanomsg
int main ()
{
    int rc;
    int sb;
    int sc;
    struct nn_iovec iov [2];
    struct nn_msghdr hdr;
    char buf [6];

    sb = nn_socket (AF_SP, NN_PAIR);
    errno_assert (sb != -1);
    rc = nn_bind (sb, SOCKET_ADDRESS);
    errno_assert (rc >= 0);
    sc = nn_socket (AF_SP, NN_PAIR);
    errno_assert (sc != -1);
    rc = nn_connect (sc, SOCKET_ADDRESS);
    errno_assert (rc >= 0);

    iov [0].iov_base = "AB";
    iov [0].iov_len = 2;
    iov [1].iov_base = "CDEF";
    iov [1].iov_len = 4;
    memset (&hdr, 0, sizeof (hdr));
    hdr.msg_iov = iov;
    hdr.msg_iovlen = 2;
    rc = nn_sendmsg (sc, &hdr, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 6);

    iov [0].iov_base = buf;
    iov [0].iov_len = 4;
    iov [1].iov_base = buf + 4;
    iov [1].iov_len = 2;
    memset (&hdr, 0, sizeof (hdr));
    hdr.msg_iov = iov;
    hdr.msg_iovlen = 2;
    rc = nn_recvmsg (sb, &hdr, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 6);
    nn_assert (memcmp (buf, "ABCDEF", 6) == 0);

    rc = nn_close (sc);
    errno_assert (rc == 0);
    rc = nn_close (sb);
    errno_assert (rc == 0);

    return 0;
}
コード例 #7
0
ファイル: zerocopy.c プロジェクト: prenaux/nanomsg
void test_reallocmsg_reqrep ()
{
    int rc;
    int req;
    int rep;
    void *p;
    void *p2;

    /*  Create sockets. */
    req = nn_socket (AF_SP, NN_REQ);
    rep = nn_socket (AF_SP, NN_REP);
    rc = nn_bind (rep, "inproc://test");
    errno_assert (rc >= 0);
    rc = nn_connect (req, "inproc://test");
    errno_assert (rc >= 0);

    /*  Create message, make sure we handle overflow. */
    p = nn_allocmsg (100, 0);
    nn_assert (p);
    p2 = nn_reallocmsg (p, -1000);
    errno_assert (nn_errno () == ENOMEM);
    nn_assert (p2 == NULL);

    /*  Realloc to fit data size. */
    memcpy (p, "Hello World!", 12);
    p = nn_reallocmsg (p, 12);
    nn_assert (p);
    rc = nn_send (req, &p, NN_MSG, 0);
    errno_assert (rc == 12);

    /*  Receive request and send response. */
    rc = nn_recv (rep, &p, NN_MSG, 0);
    errno_assert (rc == 12);
    rc = nn_send (rep, &p, NN_MSG, 0);
    errno_assert (rc == 12);

    /*  Receive response and free message. */
    rc = nn_recv (req, &p, NN_MSG, 0);
    errno_assert (rc == 12);
    rc = memcmp (p, "Hello World!", 12);
    nn_assert (rc == 0);
    rc = nn_freemsg (p);
    errno_assert (rc == 0);

    /*  Clean up. */
    nn_close (req);
    nn_close (rep);
}
コード例 #8
0
ファイル: tcp_shutdown.c プロジェクト: Droppe/nanomsg
int main ()
{
    int rc;
    int sb;
    int i;
    int j;
    struct nn_thread threads [THREAD_COUNT];

    /*  Stress the shutdown algorithm. */

    sb = nn_socket (AF_SP, NN_PUB);
    errno_assert (sb >= 0);
    rc = nn_bind (sb, SOCKET_ADDRESS);
    errno_assert (rc >= 0);

    for (j = 0; j != 10; ++j) {
        for (i = 0; i != THREAD_COUNT; ++i)
            nn_thread_init (&threads [i], routine, NULL);
        for (i = 0; i != THREAD_COUNT; ++i)
            nn_thread_term (&threads [i]);
    }

    rc = nn_close (sb);
    errno_assert (rc == 0);

    return 0;
}
コード例 #9
0
ファイル: shutdown.c プロジェクト: Inzaghi2012/nanomsg
int main ()
{
    int rc;
    int s;
    struct nn_thread thread;

    /*  Close the socket with no associated endpoints. */
    s = nn_socket (AF_SP, NN_PAIR);
    errno_assert (s != -1);
    rc = nn_close (s);
    errno_assert (rc == 0);

    /*  Test nn_term() before nn_close(). */
    nn_thread_init (&thread, worker, NULL);
    nn_sleep (10);
    nn_term ();

    /*  Check that it's not possible to create new sockets after nn_term(). */
    rc = nn_socket (AF_SP, NN_PAIR);
    nn_assert (rc == -1 && nn_errno () == ETERM);
    
    /*  Wait till worker thread terminates. */
    nn_thread_term (&thread);

    return 0;
}
コード例 #10
0
ファイル: ipc_stress.c プロジェクト: 4ker/nanomsg
static void client(void *arg)
{
    intptr_t id = (intptr_t)arg;
    int bytes;
    char msg[3];
    size_t sz_msg;
    int i;

    msg[0] = 'A' + id%26;
    msg[1] = 'a';
    msg[2] = '\0';
    /*  '\0' too */
    sz_msg = strlen (msg) + 1;

    for (i = 0; i < TEST_LOOPS; i++) {
        int cli_sock = nn_socket (AF_SP, NN_PUSH);
        msg[1] = 'a' + i%26;
        nn_assert (cli_sock >= 0);
        nn_assert (nn_connect (cli_sock, SOCKET_ADDRESS) >= 0);
        /*  Give time to allow for connect to establish. */
        nn_sleep (50);
        bytes = nn_send (cli_sock, msg, sz_msg, 0);
        /*  This would better be handled via semaphore or condvar. */
        nn_sleep (100);
        nn_assert (bytes == sz_msg);
        nn_close (cli_sock);
    }
}
コード例 #11
0
ファイル: ipc_stress.c プロジェクト: 4ker/nanomsg
static void server(NN_UNUSED void *arg)
{
    int bytes;
    int count;
    int sock = nn_socket(AF_SP, NN_PULL);
    int res[TEST_LOOPS];
    nn_assert(sock >= 0);
    nn_assert(nn_bind(sock, SOCKET_ADDRESS) >= 0);
    count = THREAD_COUNT * TEST_LOOPS;
    memset(res, 0, sizeof (res));
    while (count > 0)
    {
        char *buf = NULL;
        int tid;
        int num;
        bytes = nn_recv(sock, &buf, NN_MSG, 0);
        nn_assert(bytes >= 0);
        nn_assert(bytes >= 2);
        nn_assert(buf[0] >= 'A' && buf[0] <= 'Z');
        nn_assert(buf[1] >= 'a' && buf[0] <= 'z');
        tid = buf[0]-'A';
        num = buf[1]-'a';
        nn_assert(tid < THREAD_COUNT);
        nn_assert(res[tid] == num);
        res[tid]=num+1;
        nn_freemsg(buf);
        count--;
    }
    nn_close(sock);
}
コード例 #12
0
ファイル: inproc_lat.c プロジェクト: panliang/nanomsg
void worker (NN_UNUSED void *arg)
{
    int rc;
    int s;
    int i;
    char *buf;

    s = nn_socket (AF_SP, NN_PAIR);
    assert (s != -1);
    rc = nn_connect (s, "inproc://inproc_lat");
    assert (rc >= 0);

    buf = malloc (message_size);
    assert (buf);

    for (i = 0; i != roundtrip_count; i++) {
        rc = nn_recv (s, buf, message_size, 0);
        assert (rc == (int)message_size);
        rc = nn_send (s, buf, message_size, 0);
        assert (rc == (int)message_size);
    }

    free (buf);
    rc = nn_close (s);
    assert (rc == 0);
}
コード例 #13
0
int ftw_socket_inbox_shutdown(struct ftw_socket_inbox ** const sock)
{
    uint64_t time;
    double elapsed_time;
    int rc;

    /*  Preconditions expected of LabVIEW. */
    ftw_assert(sock);

    if (*sock == NULL) {
        errno = EBADF;
        return -1;
    }

    time = uv_hrtime();
    rc = nn_close((*sock)->id);
    nn_sem_post(&(*sock)->deinitialized);
    nn_thread_term(&(*sock)->async_recv_thread);
    nn_sem_term(&(*sock)->msg_acknowledged);
    nn_sem_term(&(*sock)->initialized);
    nn_sem_term(&(*sock)->deinitialized);
    time = uv_hrtime() - time;
    elapsed_time = time / 1000000000.0;
    ftw_debug("Inbox Shutdown time: %.3fsec", elapsed_time);

    return rc;
}
コード例 #14
0
static void broker_decrement_ref(BROKER_HANDLE broker)
{
    /*Codes_SRS_BROKER_13_058: [If `broker` is NULL the function shall do nothing.]*/
    if (broker != NULL)
    {
        /*Codes_SRS_BROKER_13_111: [Otherwise, Broker_Destroy shall decrement the internal ref count of the message.]*/
        /*Codes_SRS_BROKER_13_112: [If the ref count is zero then the allocated resources are freed.]*/
        if (DEC_REF(BROKER_HANDLE_DATA, broker) == DEC_RETURN_ZERO)
        {
            BROKER_HANDLE_DATA* broker_data = (BROKER_HANDLE_DATA*)broker; 
            if (singlylinkedlist_get_head_item(broker_data->modules) != NULL)
            {
                LogError("WARNING: There are still active modules attached to the broker and the broker is being destroyed.");
            }
            /* May want to do nn_shutdown first for cleanliness. */
            nn_close(broker_data->publish_socket);
            STRING_delete(broker_data->url);
            singlylinkedlist_destroy(broker_data->modules);
            Lock_Deinit(broker_data->modules_lock);
            free(broker_data);
        }
    }
    else
    {
        LogError("broker handle is NULL");
    }
}
コード例 #15
0
void act_client(std::string &addr, size_t sze) {
	int sock = nn_socket(AF_SP, NN_REQ);
	std::string host = "tcp://" + addr + ":12345";
	printf("host: %s\n", host.c_str());
	if (nn_connect(sock, host.c_str()) < 0) {
            printf("%s\nn", nn_strerror(errno));
            return;
	}

	std::vector<size_t> lens;
	make_package(sze, lens);
	std::vector<void *> data;
	for (auto len : lens)
		data.push_back((void *)new char[len]);

	MDL::Timer timer;
	nn_send(sock, NULL, 0, 0);
	nn_recv(sock, NULL, 0, 0);

	timer.start();
	MDL::net::send_all(sock, data, lens);
	timer.end();
	printf("send %f\n", timer.second());
	nn_close(sock);

	for (auto p : data) {
		char *pp = (char *)p;
		delete []pp;
	}
}
コード例 #16
0
ファイル: zmq.c プロジェクト: Droppe/nanomsg
int zmq_close (void *s)
{
    int fd;

    fd = (int) (((uint8_t*) s) - ((uint8_t*) 0) - 1);
    return nn_close (fd);
}
コード例 #17
0
ファイル: inproc_thr.c プロジェクト: Droppe/nanomsg
void worker (void *arg)
{
    int rc;
    int s;
    int i;
    char *buf;

    s = nn_socket (AF_SP, NN_PAIR);
    assert (s != -1);
    rc = nn_connect (s, "inproc://inproc_thr");
    assert (rc >= 0);

    buf = malloc (message_size);
    assert (buf);
    memset (buf, 111, message_size);

    rc = nn_send (s, NULL, 0, 0);
    assert (rc == 0);

    for (i = 0; i != message_count; i++) {
        rc = nn_send (s, buf, message_size, 0);
        assert (rc == message_size);
    }

    free (buf);
    rc = nn_close (s);
    assert (rc == 0);
}
コード例 #18
0
int ftw_publisher_construct(struct ftw_socket_callsite **callsite, const char *addr,
    int linger, struct ftw_socket **sock)
{
    struct ftw_socket *inst;
    int rcs;
    int rco;
    int rcb;

    /*  Preconditions expected of LabVIEW. */
    ftw_assert(*callsite && addr);
    nn_mutex_lock(&(*callsite)->sync);

    rcs = nn_socket(AF_SP, NN_PUB);

    /*  Socket creation failure? */
    if (rcs < 0) {
        *sock = NULL;
        nn_mutex_unlock(&(*callsite)->sync);
        return rcs;
    }

    rco = nn_setsockopt(rcs, NN_SOL_SOCKET, NN_LINGER, &linger, sizeof(linger));
    if (rco < 0) {
        *sock = NULL;
        nn_mutex_unlock(&(*callsite)->sync);
        return rco;
    }

    rcb = nn_bind(rcs, addr);

    /*  Endpoint creation failure? */
    if (rcb < 0) {
        nn_close(rcs);
        *sock = NULL;
        nn_mutex_unlock(&(*callsite)->sync);
        return rcb;
    }

    inst = ftw_malloc(sizeof(struct ftw_socket));
    ftw_assert(inst);

    memset(inst, 0, sizeof(*inst));

    inst->id = rcs;
    inst->callsite = *callsite;

    nn_list_item_init(&inst->item);
    nn_list_insert(&(*callsite)->active_sockets, &inst->item,
        nn_list_end(&(*callsite)->active_sockets));

    *sock = inst;

    (*callsite)->lifetime_sockets++;
    nn_mutex_unlock(&(*callsite)->sync);

    return 0;

}
コード例 #19
0
ファイル: remote_lat.c プロジェクト: 4ker/nanomsg
int main (int argc, char *argv [])
{
    const char *connect_to;
    size_t sz;
    int rts;
    char *buf;
    int nbytes;
    int s;
    int rc;
    int i;
    int opt;
    struct nn_stopwatch sw;
    uint64_t total;
    double lat;


    if (argc != 4) {
        printf ("usage: remote_lat <connect-to> <msg-size> <roundtrips>\n");
        return 1;
    }
    connect_to = argv [1];
    sz = atoi (argv [2]);
    rts = atoi (argv [3]);

    s = nn_socket (AF_SP, NN_PAIR);
    assert (s != -1);
    opt = 1;
    rc = nn_setsockopt (s, NN_TCP, NN_TCP_NODELAY, &opt, sizeof (opt));
    assert (rc == 0);
    rc = nn_connect (s, connect_to);
    assert (rc >= 0);

    buf = malloc (sz);
    assert (buf);
    memset (buf, 111, sz);

    nn_stopwatch_init (&sw);
    for (i = 0; i != rts; i++) {
        nbytes = nn_send (s, buf, sz, 0);
        assert (nbytes == (int)sz);
        nbytes = nn_recv (s, buf, sz, 0);
        assert (nbytes == (int)sz);
    }
    total = nn_stopwatch_term (&sw);

    lat = (double) total / (rts * 2);
    printf ("message size: %d [B]\n", (int) sz);
    printf ("roundtrip count: %d\n", (int) rts);
    printf ("average latency: %.3f [us]\n", (double) lat);

    free (buf);

    rc = nn_close (s);
    assert (rc == 0);

    return 0;
}
コード例 #20
0
static int nn_really_close(int s)
{
    int result;
    do
    {
        result = nn_close(s);
    } while (result == -1 && nn_errno() == EINTR);
    return result;
}
コード例 #21
0
int ftw_socket_inbox_construct(struct ftw_socket_inbox **inst, LVUserEventRef *msg_to_lv_event,
    struct ftw_socket_inbox **sock, const LStrHandleArray **addresses, int linger, int max_recv_size)
{
    char *addr;
    int rcb;
    int rcs;
    int rco;
    int i;

    /*  Preconditions expected of LabVIEW. */
    ftw_assert(inst && *inst && addresses && *addresses && msg_to_lv_event && sock);

    rcs = nn_socket(AF_SP_RAW, NN_REP);

    /*  Socket creation failure? */
    if (rcs < 0) {
        *sock = NULL;
        return rcs;
    }

    rco = nn_setsockopt(rcs, NN_SOL_SOCKET, NN_LINGER, &linger, sizeof(linger));
    if (rco < 0) {
        *sock = NULL;
        return rco;
    }

    rco = nn_setsockopt(rcs, NN_SOL_SOCKET, NN_RCVMAXSIZE, &max_recv_size, sizeof(max_recv_size));
    if (rco < 0) {
        *sock = NULL;
        return rco;
    }

    for (i = 0; i < (*addresses)->dimsize; i++) {
        addr = ftw_support_LStrHandle_to_CStr((*addresses)->element[i]);
        rcb = nn_bind(rcs, addr);
        ftw_free (addr);
        if (rcb < 0) {
            nn_close(rcs);
            *sock = NULL;
            return rcb;
        }
    }

    (*inst)->incoming_msg_notifier_event = *msg_to_lv_event;
    (*inst)->id = rcs;

    /*  Launch thread and wait for it to initialize. */
    nn_sem_init(&(*inst)->deinitialized);
    nn_sem_init(&(*inst)->initialized);
    nn_sem_init(&(*inst)->msg_acknowledged);
    nn_thread_init(&(*inst)->async_recv_thread, ftw_socket_inbox_async_recv_worker, *inst);
    nn_sem_wait(&(*inst)->initialized);

    *sock = *inst;

    return 0;
}
コード例 #22
0
ファイル: fanin.c プロジェクト: Droppe/nanomsg
int main ()
{
    int rc;
    int sink;
    int source1;
    int source2;
    char buf [3];

    sink = nn_socket (AF_SP, NN_SINK);
    errno_assert (sink != -1);
    rc = nn_bind (sink, SOCKET_ADDRESS);
    errno_assert (rc >= 0);
    source1 = nn_socket (AF_SP, NN_SOURCE);
    errno_assert (source1 != -1);
    rc = nn_connect (source1, SOCKET_ADDRESS);
    errno_assert (rc >= 0);
    source2 = nn_socket (AF_SP, NN_SOURCE);
    errno_assert (source2 != -1);
    rc = nn_connect (source2, SOCKET_ADDRESS);
    errno_assert (rc >= 0);

    rc = nn_send (source1, "ABC", 3, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);
    rc = nn_send (source2, "DEF", 3, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);

    rc = nn_recv (sink, buf, sizeof (buf), 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);
    rc = nn_recv (sink, buf, sizeof (buf), 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);

    rc = nn_close (sink);
    errno_assert (rc == 0);
    rc = nn_close (source1);
    errno_assert (rc == 0);    
    rc = nn_close (source2);
    errno_assert (rc == 0);

    return 0;
}
コード例 #23
0
ファイル: nn_queue.c プロジェクト: megahall/sdn_sensor
int ss_nn_queue_destroy(nn_queue_t* nn_queue) {
    if (!nn_queue) return 0;
    if (nn_queue->conn >= 0) { nn_close(nn_queue->conn); nn_queue->conn = -1; }
    nn_queue->remote_id = -1;
    nn_queue->format    = (nn_queue_format_t) -1;
    nn_queue->content   = (nn_content_type_t) -1;
    nn_queue->type      = -1;
    memset(nn_queue->url, 0, sizeof(nn_queue->url));
    return 0;
}
コード例 #24
0
ファイル: subscribe_socket.cpp プロジェクト: GYGit/SFrame
void subscribe_socket::close() {
  if (z_socket != -1) {
    nn_close(z_socket);
    z_socket = -1;
  }
  if (shutting_down == false) {
    shutting_down = true;
    thr.join();
  }
}
コード例 #25
0
ファイル: inproc_lat.c プロジェクト: panliang/nanomsg
int main (int argc, char *argv [])
{
    int rc;
    int s;
    int i;
    char *buf;
    struct nn_thread thread;
    struct nn_stopwatch stopwatch;
    uint64_t elapsed;
    double latency;

    if (argc != 3) {
        printf ("usage: inproc_lat <message-size> <roundtrip-count>\n");
        return 1;
    }

    message_size = atoi (argv [1]);
    roundtrip_count = atoi (argv [2]);

    s = nn_socket (AF_SP, NN_PAIR);
    assert (s != -1);
    rc = nn_bind (s, "inproc://inproc_lat");
    assert (rc >= 0);

    buf = malloc (message_size);
    assert (buf);
    memset (buf, 111, message_size);

    /*  Wait a bit till the worker thread blocks in nn_recv(). */
    nn_thread_init (&thread, worker, NULL);
    nn_sleep (100);

    nn_stopwatch_init (&stopwatch);

    for (i = 0; i != roundtrip_count; i++) {
        rc = nn_send (s, buf, message_size, 0);
        assert (rc == (int)message_size);
        rc = nn_recv (s, buf, message_size, 0);
        assert (rc == (int)message_size);
    }

    elapsed = nn_stopwatch_term (&stopwatch);

    latency = (double) elapsed / (roundtrip_count * 2);
    printf ("message size: %d [B]\n", (int) message_size);
    printf ("roundtrip count: %d\n", (int) roundtrip_count);
    printf ("average latency: %.3f [us]\n", (double) latency);

    nn_thread_term (&thread);
    free (buf);
    rc = nn_close (s);
    assert (rc == 0);

    return 0;
}
コード例 #26
0
ファイル: nanomsg2.c プロジェクト: tempbottle/nanomsg4py
static PyObject *sockClose(sock_t *self)
{
    if (self->sock == -1)
        return NULL;
    if (nn_close(self->sock) != 0){
        ERR;
        return NULL;
    }
    self->sock = self->endpoint = -1;
    Py_RETURN_NONE;
}
コード例 #27
0
ファイル: test_network.cpp プロジェクト: fionser/MDLHELib
void act_client(int socket) {
    FHEcontext context(gM, gP, gR);
    buildModChain(context, gL);
    FHESecKey sk(context);
    sk.GenSecKey(64);
    FHEPubKey &pk = sk;
    send_pk(socket, pk);

    std::vector<Ctxt> ctxts;
    receive_ctxt(socket, pk, ctxts);
    nn_close(socket);
}
コード例 #28
0
ファイル: test_network.cpp プロジェクト: fionser/MDLHELib
void act_server(int socket) {
    FHEcontext context(gM, gP, gR);
    buildModChain(context, gL);
    FHEPubKey pk(context);
    receive_pk(socket, pk);

    std::vector<Ctxt> ctxts(gC, pk);
    for (long i = 0; i < gC; i++)
        pk.Encrypt(ctxts[i], NTL::to_ZZX(i));
    send_ctxts(socket, ctxts);
    nn_close(socket);
}
コード例 #29
0
ファイル: tcp_shutdown.c プロジェクト: Droppe/nanomsg
static void routine (void *arg)
{
    int rc;
    int s;

    s = nn_socket (AF_SP, NN_SUB);
    if (s < 0 && nn_errno () == EMFILE)
        return;
    errno_assert (s >= 0);
    rc = nn_connect (s, SOCKET_ADDRESS);
    errno_assert (rc >= 0);
    rc = nn_close (s);
    errno_assert (rc == 0);
}
コード例 #30
0
ファイル: clinano.c プロジェクト: danburkert/rust-nanomsg
int main ()
{
  int rc;
  int sb;
  int sc;
  int i;
  char buf [4];
  int opt;
  size_t sz;
  char msg[256];
  char* pRecvd = 0;

  // client, running on windows

  sc = nn_socket (AF_SP, NN_PAIR);
  errno_assert (sc >= 0);

  // connect
  rc = nn_connect (sc, SOCKET_ADDRESS);
  errno_assert (rc > 0);

  // send
  memcpy(buf, "WHY\0", 4);
  rc = nn_send (sc, buf, 3, 0);
  printf("client: I sent '%s'\n", buf);
  errno_assert (rc >= 0);
  nn_assert (rc == 3);

  // receive
  rc = nn_recv (sc, &pRecvd, NN_MSG, 0);
  errno_assert (rc >= 0);
  nn_assert (rc == 3);

  sprintf(msg, "client: I received: '%s'\n\0", buf);
  printf(msg);

  // free
  rc = nn_freemsg(pRecvd);
  errno_assert (rc == 0);

  // close
  rc = nn_close (sc);
  errno_assert (rc == 0);

  return 0;
}