Esempio n. 1
0
int nn_device_mvmsg (struct nn_device_recipe *device,
    int from, int to, int flags)
{
    int rc;
    void *body;
    void *control;
    struct nn_iovec iov;
    struct nn_msghdr hdr;

    iov.iov_base = &body;
    iov.iov_len = NN_MSG;
    memset (&hdr, 0, sizeof (hdr));
    hdr.msg_iov = &iov;
    hdr.msg_iovlen = 1;
    hdr.msg_control = &control;
    hdr.msg_controllen = NN_MSG;
    rc = nn_recvmsg (from, &hdr, flags);
    if (nn_slow (rc < 0 && nn_errno () == ETERM))
        return -1;
    errno_assert (rc >= 0);
    
    rc = device->nn_device_rewritemsg(device,from,to,flags,&hdr,rc);
    if (nn_slow(rc==-1)) return -1;
    else if (rc==0) return 0;
    nn_assert(rc==1);

    rc = nn_sendmsg (to, &hdr, flags);
    if (nn_slow (rc < 0 && nn_errno () == ETERM))
        return -1;
    errno_assert (rc >= 0);
    return 0;
}
Esempio n. 2
0
void send_ctxts(int socket, const std::vector<Ctxt> &ctxts) {
    std::vector<void *> data;
    std::vector<size_t> lens;
    std::stringstream sstream;
    MDL::Timer timer;

    for (auto &ctxt : ctxts) {
        sstream.str("");
        sstream << ctxt;
        auto str = sstream.str();
        auto len = str.size();
        auto tmp = nn_allocmsg(len, 0);
        std::memcpy(tmp, str.c_str(), len);
        data.push_back(tmp);
        lens.push_back(len);
    }

    MDL::net::msg_header *hdr;
    MDL::net::make_header(&hdr, lens);
    nn_send(socket, hdr, MDL::net::header_size(hdr), 0); // send lens
    nn_recv(socket, NULL, 0, 0); // recv ok

    struct nn_msghdr nn_hdr;
    MDL::net::make_nn_header(&nn_hdr, data, lens);
    timer.start();
    nn_sendmsg(socket, &nn_hdr, 0); // send data
    timer.end();
    printf("sent %zd ctxt %f s\n", ctxts.size(), timer.second());
    nn_recv(socket, NULL, 0, 0);
    MDL::net::free_header(&nn_hdr, true);
}
Esempio n. 3
0
pint32 pnet_send(pnet_socket *socket, char *buffer)
{
    if (unlikely(!socket)) {
        plog_error("pnet_send() нет объекта socket");
        return -1;
    }

    if (unlikely(!buffer)) {
        plog_error("pnet_send() нет buffer");
        return -1;
    }

    //int bytes = nn_send(socket->socket_fd, buffer, strlen(buffer), NN_DONTWAIT);
    struct nn_msghdr hdr;
    struct nn_iovec iov [2];
    puint32 buf0 = ++rra | 0x80000000;

    char buf1 [10] = { 0 };
    strcpy(buf1, buffer);
    iov [0].iov_base = &buf0;
    iov [0].iov_len = 4;
    iov [1].iov_base = buf1;
    iov [1].iov_len = sizeof (buf1);
    memset (&hdr, 0, sizeof (hdr));
    hdr.msg_iov = iov;
    hdr.msg_iovlen = 2;
    int bytes = nn_sendmsg (socket->socket_fd, &hdr, NN_DONTWAIT);


    plog_dbg("pnet_send() send %d", bytes);
    //int rc = nn_freemsg(buffer);
     // plog_dbg("rc == %d", rc);

    return bytes;
}
Esempio n. 4
0
File: enm_drv.c Progetto: basho/enm
static int
enm_do_send(EnmData* d, const struct nn_msghdr* msghdr, int* err)
{
    int rc;

    if (msghdr->msg_iovlen == 0)
        return 0;
    *err = 0;
    do {
        rc = nn_sendmsg(d->fd, msghdr, NN_DONTWAIT);
        if (rc < 0) {
            *err = errno;
            switch (*err) {
            case EINTR:
            case EFSM:
                /* do nothing */
                break;
            case EAGAIN:
                d->b.writable = 0;
                break;
            default:
                enm_write_select(d, 0);
                enm_read_select(d, 0);
                driver_failure(d->port, *err);
            }
        }
    } while (*err == EINTR);
    return rc;
}
Esempio n. 5
0
coroutine void route_work(chan workers, int back_router, chan jobs)
{
    while(1){

        char *buf = chr(jobs, char*);
        printf("do i have a worker for my work??\n");
        char *worker_header = chr(workers, char*);

        printf("i've got a worker available\n");
        struct nn_msghdr hdr; 
        hdr.msg_control = worker_header;
        hdr.msg_controllen = 64;

//        printf("sending msg: %s w/ len %d\n", buf, strlen(buf));
        struct nn_iovec iovec;
        iovec.iov_base = buf;
        iovec.iov_len = strlen(buf);

        hdr.msg_iov = &iovec;
        hdr.msg_iovlen = 1;

        nn_sendmsg(back_router, &hdr, NN_DONTWAIT);
        printf("work sent\n");
    }
}
Esempio n. 6
0
int nn_ws_send (int s, const void *msg, size_t len, uint8_t msg_type, int flags)
{
    int rc;
    struct nn_iovec iov;
    struct nn_msghdr hdr;
    struct nn_cmsghdr *cmsg;
    size_t cmsgsz;

    iov.iov_base = (void*) msg;
    iov.iov_len = len;
    
    cmsgsz = NN_CMSG_SPACE (sizeof (msg_type));
    cmsg = nn_allocmsg (cmsgsz, 0);
    if (cmsg == NULL)
        return -1;

    cmsg->cmsg_level = NN_WS;
    cmsg->cmsg_type = NN_WS_HDR_OPCODE;
    cmsg->cmsg_len = NN_CMSG_LEN (sizeof (msg_type));
    memcpy (NN_CMSG_DATA (cmsg), &msg_type, sizeof (msg_type));

    hdr.msg_iov = &iov;
    hdr.msg_iovlen = 1;
    hdr.msg_control = &cmsg;
    hdr.msg_controllen = NN_MSG;

    rc = nn_sendmsg (s, &hdr, flags);

    return rc;
}
Esempio n. 7
0
 inline int sendmsg (const struct nn_msghdr *msghdr, int flags)
 {
     int rc = nn_sendmsg (s, msghdr, flags);
     if (nn_slow (rc < 0)) {
         if (nn_slow (nn_errno () != EAGAIN))
             throw nn::exception ();
         return -1;
     }
     return rc;
 }
Esempio n. 8
0
void test_allocmsg_reqrep ()
{
    int rc;
    int req;
    void *p;
    struct nn_iovec iov;
    struct nn_msghdr hdr;

    /*  Try to create an oversized message. */
    p = nn_allocmsg (-1, 0);
    nn_assert (!p && nn_errno () == ENOMEM);
    p = nn_allocmsg (-1000, 0);
    nn_assert (!p && nn_errno () == ENOMEM);

    /*  Try to create a message of unknown type. */
    p = nn_allocmsg (100, 333);
    nn_assert (!p && nn_errno () == EINVAL);

    /*  Create a socket. */
    req = test_socket (AF_SP_RAW, NN_REQ);

    /*  Make send fail and check whether the zero-copy buffer is left alone
        rather than deallocated. */
    p = nn_allocmsg (100, 0);
    nn_assert (p);
    rc = nn_send (req, &p, NN_MSG, NN_DONTWAIT);
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EAGAIN);
    memset (p, 0, 100);
    rc = nn_freemsg (p);
    errno_assert (rc == 0);

    /*  Same thing with nn_sendmsg(). */
    p = nn_allocmsg (100, 0);
    nn_assert (p);
    iov.iov_base = &p;
    iov.iov_len = NN_MSG;
    memset (&hdr, 0, sizeof (hdr));
    hdr.msg_iov = &iov;
    hdr.msg_iovlen = 1;
    nn_sendmsg (req, &hdr, NN_DONTWAIT);
    errno_assert (nn_errno () == EAGAIN);
    memset (p, 0, 100);
    rc = nn_freemsg (p);
    errno_assert (rc == 0);

    /*  Clean up. */
    test_close (req);
}
Esempio n. 9
0
int nn_send (int s, const void *buf, size_t len, int flags)
{
    struct nn_iovec iov;
    struct nn_msghdr hdr;

    iov.iov_base = (void*) buf;
    iov.iov_len = len;

    hdr.msg_iov = &iov;
    hdr.msg_iovlen = 1;
    hdr.msg_control = NULL;
    hdr.msg_controllen = 0;

    return nn_sendmsg (s, &hdr, flags);
}
Esempio n. 10
0
static void send_picture(int socket, const PictureHeader& header, const SkData& skp) {
    // Vectored IO lets us send header and skp contiguously without first
    // copying them to a contiguous buffer.
    struct nn_iovec iov[] = {
        create_iov(&header, sizeof(header)),
        create_iov(skp.data(), skp.size()),
    };

    struct nn_msghdr msg;
    sk_bzero(&msg, sizeof(msg));
    msg.msg_iov    = iov;
    msg.msg_iovlen = SK_ARRAY_COUNT(iov);

    nn_sendmsg(socket, &msg, 0/*flags*/);
}
Esempio n. 11
0
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;
}
Esempio n. 12
0
int ftw_nanomsg_send (int s, const void *buf, size_t length, int timeout, int flags)
{
    struct nn_iovec iov;
    struct nn_msghdr hdr;
	
    iov.iov_base = (void*) buf;
    iov.iov_len = NN_MSG;

    hdr.msg_iov = &iov;
    hdr.msg_iovlen = 1;
    hdr.msg_control = NULL;
    hdr.msg_controllen = 0;

	nn_setsockopt(s, NN_SOL_SOCKET, NN_SNDTIMEO, &timeout, sizeof(timeout));

    return nn_sendmsg (s, &hdr, flags);
}
Esempio n. 13
0
File: msg.c Progetto: 4ker/nanomsg
int main (int argc, const char *argv[])
{
    int rc;
    int sb;
    int sc;
    unsigned char *buf1, *buf2;
    int i;
    struct nn_iovec iov;
    struct nn_msghdr hdr;
    char socket_address_tcp[128];

    test_addr_from(socket_address_tcp, "tcp", "127.0.0.1",
            get_test_port(argc, argv));

    sb = test_socket (AF_SP, NN_PAIR);
    test_bind (sb, SOCKET_ADDRESS);
    sc = test_socket (AF_SP, NN_PAIR);
    test_connect (sc, SOCKET_ADDRESS);

    buf1 = nn_allocmsg (256, 0);
    alloc_assert (buf1);
    for (i = 0; i != 256; ++i)
        buf1 [i] = (unsigned char) i;
    rc = nn_send (sc, &buf1, NN_MSG, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 256);

    buf2 = NULL;
    rc = nn_recv (sb, &buf2, NN_MSG, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 256);
    nn_assert (buf2);
    for (i = 0; i != 256; ++i)
        nn_assert (buf2 [i] == (unsigned char) i);
    rc = nn_freemsg (buf2);
    errno_assert (rc == 0);

    buf1 = nn_allocmsg (256, 0);
    alloc_assert (buf1);
    for (i = 0; i != 256; ++i)
        buf1 [i] = (unsigned char) i;
    iov.iov_base = &buf1;
    iov.iov_len = NN_MSG;
    memset (&hdr, 0, sizeof (hdr));
    hdr.msg_iov = &iov;
    hdr.msg_iovlen = 1;
    rc = nn_sendmsg (sc, &hdr, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 256);

    buf2 = NULL;
    iov.iov_base = &buf2;
    iov.iov_len = NN_MSG;
    memset (&hdr, 0, sizeof (hdr));
    hdr.msg_iov = &iov;
    hdr.msg_iovlen = 1;
    rc = nn_recvmsg (sb, &hdr, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 256);
    nn_assert (buf2);
    for (i = 0; i != 256; ++i)
        nn_assert (buf2 [i] == (unsigned char) i);
    rc = nn_freemsg (buf2);
    errno_assert (rc == 0);

    test_close (sc);
    test_close (sb);

    /*  Test receiving of large message  */

    sb = test_socket (AF_SP, NN_PAIR);
    test_bind (sb, socket_address_tcp);
    sc = test_socket (AF_SP, NN_PAIR);
    test_connect (sc, socket_address_tcp);

    for (i = 0; i < (int) sizeof (longdata); ++i)
        longdata[i] = '0' + (i % 10);
    longdata [sizeof (longdata) - 1] = 0;
    test_send (sb, longdata);

    rc = nn_recv (sc, &buf2, NN_MSG, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == sizeof (longdata) - 1);
    nn_assert (buf2);
    for (i = 0; i < (int) sizeof (longdata) - 1; ++i)
        nn_assert (buf2 [i] == longdata [i]);
    rc = nn_freemsg (buf2);
    errno_assert (rc == 0);

    test_close (sc);
    test_close (sb);

    return 0;
}
Esempio n. 14
0
int ftw_socket_inbox_reply(json_t *response, struct ftw_incoming_request *req, const int timeout)
{
    struct ftw_socket_inbox const *s;
    struct nn_iovec iov;
    struct nn_msghdr hdr;
    size_t hdr_len;
    void *hdr_ptr;
    char *buffer;
    int rc;

    if (req == NULL) {
        ftw_json_destroy(response);
        errno = EBADF;
        return -1;
    }

    s = req->inbox;
    hdr_len = req->hdr_len;
    hdr_ptr = req->hdr_ptr;
    ftw_assert(ftw_free(req) == mgNoErr);

    if (hdr_ptr == NULL) {
        ftw_json_destroy(response);
        errno = EBADF;
        return -1;
    }
    
    if (s == NULL) {
        nn_freemsg(hdr_ptr);
        ftw_json_destroy(response);
        errno = EBADF;
        return -1;
    }

    buffer = json_dumps(response, JSON_ALLOW_NUL);
    ftw_json_destroy(response);

    if (buffer == NULL) {
        nn_freemsg(hdr_ptr);
        errno = EBADF;
        return -1;
    }

    iov.iov_base = buffer;
    iov.iov_len = strlen(buffer);

    hdr.msg_iov = &iov;
    hdr.msg_iovlen = 1;
    hdr.msg_control = hdr_ptr;
    hdr.msg_controllen = hdr_len;

    rc = nn_setsockopt(s->id, NN_SOL_SOCKET, NN_SNDTIMEO, &timeout, sizeof(timeout));
    if (rc < 0) {
        free(buffer);
        nn_freemsg(hdr_ptr);
        return rc;
    }

    rc = nn_sendmsg(s->id, &hdr, 0);
    free(buffer);
    nn_freemsg(hdr_ptr);
    return rc;
}
Esempio n. 15
0
File: cmsg.c Progetto: Abioy/nanomsg
int main ()
{
    int rc;
    int rep;
    int req;
    struct nn_msghdr hdr;
    struct nn_iovec iovec;
    unsigned char body [3];
    unsigned char ctrl [256];
    struct nn_cmsghdr *cmsg;
    unsigned char *data;
    void *buf;
    
    rep = test_socket (AF_SP_RAW, NN_REP);
    test_bind (rep, SOCKET_ADDRESS);
    req = test_socket (AF_SP, NN_REQ);
    test_connect (req, SOCKET_ADDRESS);

    /* Test ancillary data in static buffer. */

    test_send (req, "ABC");

    iovec.iov_base = body;
    iovec.iov_len = sizeof (body);
    hdr.msg_iov = &iovec;
    hdr.msg_iovlen = 1;
    hdr.msg_control = ctrl;
    hdr.msg_controllen = sizeof (ctrl);
    rc = nn_recvmsg (rep, &hdr, 0);
    errno_assert (rc == 3);

    cmsg = NN_CMSG_FIRSTHDR (&hdr);
    while (1) {
        nn_assert (cmsg);
        if (cmsg->cmsg_level == PROTO_SP && cmsg->cmsg_type == SP_HDR)
            break;
        cmsg = NN_CMSG_NXTHDR (&hdr, cmsg);
    }
    nn_assert (cmsg->cmsg_len == NN_CMSG_SPACE (8));
    data = NN_CMSG_DATA (cmsg);
    nn_assert (!(data[0] & 0x80));
    nn_assert (data[4] & 0x80);

    rc = nn_sendmsg (rep, &hdr, 0);
    nn_assert (rc == 3);
    test_recv (req, "ABC");

    /* Test ancillary data in dynamically allocated buffer (NN_MSG). */

    test_send (req, "ABC");

    iovec.iov_base = body;
    iovec.iov_len = sizeof (body);
    hdr.msg_iov = &iovec;
    hdr.msg_iovlen = 1;
    hdr.msg_control = &buf;
    hdr.msg_controllen = NN_MSG;
    rc = nn_recvmsg (rep, &hdr, 0);
    errno_assert (rc == 3);

    cmsg = NN_CMSG_FIRSTHDR (&hdr);
    while (1) {
        nn_assert (cmsg);
        if (cmsg->cmsg_level == PROTO_SP && cmsg->cmsg_type == SP_HDR)
            break;
        cmsg = NN_CMSG_NXTHDR (&hdr, cmsg);
    }
    nn_assert (cmsg->cmsg_len == NN_CMSG_SPACE (8));
    data = NN_CMSG_DATA (cmsg);
    nn_assert (!(data[0] & 0x80));
    nn_assert (data[4] & 0x80);

    rc = nn_sendmsg (rep, &hdr, 0);
    nn_assert (rc == 3);
    test_recv (req, "ABC");

    test_close (req);
    test_close (rep);

    return 0;
}
Esempio n. 16
0
static void sockevent1(uint64_t id, int fd, int revents, void *ud) {
  struct tasklet *self = (struct tasklet *)ud;

  //TRACE("[%s] sockevent1() id=%lx fd=%d revents=%x\n", self->task_path, id, fd, revents);

  self->stat.sockevent1_evct++;

  int recvct = 0;

  if (revents & POLLIN) {

    int nnsk = -1;
    if (fd == self->nn.sub_efd) {
      nnsk = self->nn.sub;
    } else if (fd == self->nn.bsub_efd) {
      nnsk = self->nn.bsub;
      self->stat.sockevent1_evct_bsub++;
    }

    if (nnsk >= 0) {
      int r;
#if USE_NN_MSG
      char *nnmsg = NULL;
      r = nn_recv(nnsk, &nnmsg, NN_MSG, NN_DONTWAIT);
#else
      char nnmsg[2048];
      r = nn_recv(nnsk, &nnmsg, sizeof(nnmsg), NN_DONTWAIT);
#endif
      if (r < 0) {
	switch (errno) {
	case EAGAIN:
	case EINTR:
	  self->stat.msg_recv_again++;
#if USE_NN_MSG
	  RW_ASSERT(!nnmsg);
#endif
	  break;
	default:
	  self->stat.msg_recv_err++;
	  self->stat.msg_recv_err_lasterrno = errno;
	  printf("[%s] + nn_recv(sk=%d) err %d %s\n", self->task_path, nnsk, errno, strerror(errno));
	}
      } else {
	recvct++;
	self->stat.msg_recv++;
	int i=0;
	while (i < r && nnmsg[i++]) { }
	if ((i + (int)sizeof(rwmsg_header_t)) <= r) {
	  rwmsg_header_t *hdr = (rwmsg_header_t *)&nnmsg[i];
	  char *payload = &nnmsg[i+sizeof(rwmsg_header_t)];
	  int paylen = r - (i + sizeof(rwmsg_header_t));
	  paylen=paylen;
	  payload=payload;
	  int toss = (self->wantblockmsg && hdr->rwmsg_type == RSP && hdr->rwmsg_ser != self->wantblockmsg);
	  TRACE ("[%s] + %s nn_recv(sk=%d/%s) to '%s' ser %u typ %s ret '%s' pay len %d '%s' \n",
		  self->task_path,
		  toss ? "TOSS" : "",
		  nnsk,
		  (nnsk == self->nn.bsub ? "bsub" : nnsk == self->nn.sub ? "sub" : "???"),
		  nnmsg,
		  hdr->rwmsg_ser,
		  hdr->rwmsg_type == REQ ? "REQ" : hdr->rwmsg_type == RSP ? "RSP" : "XXX",
		  hdr->rwmsg_ret,
		  paylen,
		  payload);

	  if (!toss) {
	    switch (hdr->rwmsg_type) {
	    case RSP:
	      self->stat.rsp_recv++;
	      if (nnsk == self->nn.bsub) {
		if (hdr->rwmsg_ser == self->wantblockmsg) {
		  self->stat.rsp_recv_blocking++;
		  self->wantblockmsg = 0;
		  /* CAUTION need to return right away */
		} else {
		  self->stat.rsp_recv_blocking_unk++;
		}
	      } else {
		/* Call timer1 to put some more messages outstanding.  Do not block. */
		int saveblocker = self->blocker;
		self->blocker = 0;
		timer1(0, self);
		self->blocker = saveblocker;
	      }
	    default:
	      break;
	    case REQ:
	      {
		int blocking =  (hdr->rwmsg_ret[3] == 'B');
		self->stat.req_recv++;
		if (blocking) {
		  self->stat.req_recv_blocking++;
		}
		if (0 && blocking) {
		  /* don't answer, to try timeout */
		  goto meh;
		}
		hdr->rwmsg_type = RSP;
		struct nn_iovec iov[3] = {
		  { .iov_base = hdr->rwmsg_ret, .iov_len = strlen(hdr->rwmsg_ret)+1 },
		  { .iov_base = hdr, .iov_len = sizeof(*hdr) },
		  { .iov_base = "This is a response payload.", .iov_len = 28 }
		};
		struct nn_msghdr msghdr = {
		  .msg_iov = iov,
		  .msg_iovlen = 3
		};
		
		TRACE("[%s] + nn_sendmsg sent %s rsp ser %u to %s\n", 
		       self->task_path, 
		       blocking ? "blocking" : "nonblocking",
		       hdr->rwmsg_ser,
		       hdr->rwmsg_ret);

		int r = nn_sendmsg(self->nn.pub, &msghdr, NN_DONTWAIT);
		if (r < 0) {
		  switch (errno) {
		  case EAGAIN:
		  case EINTR:
		    self->stat.rsp_sent_again++;
		    // TBD
		    break;
		  default:
		    self->stat.rsp_sent_err++;
		    self->stat.rsp_sent_err_lasterrno = errno;
		    printf("[%s] + nn_sendmsg errno=%u %s\n", self->task_path, errno, strerror(errno));
		    break;
		  }
		} else {
		  self->stat.rsp_sent++;
		  if (blocking) {
		    self->stat.rsp_sent_blocking++;
		  }
		}
	      }
	      break;
	    }
	  } else {
Esempio n. 17
0
int testmsg()
{
    int rc;
    int sb;
    int sc;
    unsigned char *buf1, *buf2;
    int i;
    struct nn_iovec iov;
    struct nn_msghdr hdr;
    printf("test msg\n");
    if ( 1 )
    {
        sb = test_socket (AF_SP, NN_PAIR);
        test_bind (sb, SOCKET_ADDRESS);
        sc = test_socket (AF_SP, NN_PAIR);
        test_connect (sc, SOCKET_ADDRESS);
        
        buf1 = nn_allocmsg (256, 0);
        alloc_assert (buf1);
        for (i = 0; i != 256; ++i)
            buf1 [i] = (unsigned char) i;
        printf("send 256\n");
        rc = nn_send (sc, &buf1, NN_MSG, 0);
        printf("rc.%d\n",rc);
        errno_assert (rc >= 0);
        nn_assert (rc == 256);
        
        buf2 = NULL;
        rc = nn_recv (sb, &buf2, NN_MSG, 0);
        errno_assert (rc >= 0);
        nn_assert (rc == 256);
        nn_assert (buf2);
        for (i = 0; i != 256; ++i)
            nn_assert (buf2 [i] == (unsigned char) i);
        rc = nn_freemsg (buf2);
        errno_assert (rc == 0);
        
        buf1 = nn_allocmsg (256, 0);
        alloc_assert (buf1);
        for (i = 0; i != 256; ++i)
            buf1 [i] = (unsigned char) i;
        iov.iov_base = &buf1;
        iov.iov_len = NN_MSG;
        memset (&hdr, 0, sizeof (hdr));
        hdr.msg_iov = &iov;
        hdr.msg_iovlen = 1;
        rc = nn_sendmsg (sc, &hdr, 0);
        errno_assert (rc >= 0);
        nn_assert (rc == 256);
        
        buf2 = NULL;
        iov.iov_base = &buf2;
        iov.iov_len = NN_MSG;
        memset (&hdr, 0, sizeof (hdr));
        hdr.msg_iov = &iov;
        hdr.msg_iovlen = 1;
        rc = nn_recvmsg (sb, &hdr, 0);
        errno_assert (rc >= 0);
        nn_assert (rc == 256);
        nn_assert (buf2);
        for (i = 0; i != 256; ++i)
            nn_assert (buf2 [i] == (unsigned char) i);
        rc = nn_freemsg (buf2);
        errno_assert (rc == 0);
        
        test_close (sc);
        test_close (sb);
    }
    /*  Test receiving of large message  */
    sb = test_socket(AF_SP, NN_PAIR);
    //printf("test_bind.(%s)\n",SOCKET_ADDRESS_TCP);
    test_bind(sb,SOCKET_ADDRESS_TCP);
    sc = test_socket(AF_SP,NN_PAIR);
    //printf("test_connect.(%s)\n",SOCKET_ADDRESS_TCP);
    test_connect(sc,SOCKET_ADDRESS_TCP);

    for (i = 0; i < (int) sizeof (longdata); ++i)
        longdata[i] = '0' + (i % 10);
    longdata [sizeof(longdata) - 1] = 0;
    printf("send longdata.%d\n",(int32_t)sizeof(longdata));
    test_send(sb,longdata);
    printf("recv longdata.%d\n",(int32_t)sizeof(longdata));
    rc = nn_recv (sc, &buf2, NN_MSG, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == sizeof (longdata) - 1);
    nn_assert (buf2);
    for (i = 0; i < (int) sizeof (longdata) - 1; ++i)
        nn_assert (buf2 [i] == longdata [i]);
    rc = nn_freemsg (buf2);
    errno_assert (rc == 0);

    test_close (sc);
    test_close (sb);
    //printf("testmsg completed\n");
    return 0;
}
Esempio n. 18
0
int ftw_nanomsg_connector_ask(struct ftw_socket ** const sock, int send_timeout,
    int recv_timeout, const LStrHandle request, LStrHandle response)
{
    int rc;
    struct nn_iovec iov;
    struct nn_msghdr hdr;
    void *recv_buf;
    MgErr resize_err;
    struct ftw_socket const *s;

    s = *sock;

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

    iov.iov_base = LHStrBuf(request);
    iov.iov_len = LHStrLen(request);

    hdr.msg_iov = &iov;
    hdr.msg_iovlen = 1;
    hdr.msg_control = NULL;
    hdr.msg_controllen = 0;

    rc = nn_setsockopt(s->id, NN_SOL_SOCKET, NN_SNDTIMEO, &send_timeout, sizeof(send_timeout));
    if (rc < 0)
        return rc;

    rc = nn_setsockopt(s->id, NN_SOL_SOCKET, NN_RCVTIMEO, &recv_timeout, sizeof(recv_timeout));
    if (rc < 0)
        return rc;

    rc = nn_sendmsg(s->id, &hdr, 0);

    if (rc >= 0) {
        recv_buf = NULL;
        iov.iov_base = &recv_buf;
        iov.iov_len = NN_MSG;

        hdr.msg_iov = &iov;
        hdr.msg_iovlen = 1;
        hdr.msg_control = NULL;
        hdr.msg_controllen = 0;

        rc = nn_recvmsg(s->id, &hdr, 0);

        if (rc >= 0) {
            resize_err = ftw_support_buffer_to_LStrHandle(&response, recv_buf, rc);
            if (resize_err != mgNoErr) {
                errno = resize_err + LV_USER_ERROR;
                rc = -1;
            }
        }

        if (recv_buf)
            nn_freemsg(recv_buf);
    }

    return rc;
}
Esempio n. 19
0
int main()
{

    int back_router = nn_socket(AF_SP_RAW, NN_REP);
    nn_bind(back_router, BACKROUTER);

    chan jobs = chmake(char*, 128);
    chan workers = chmake(char*, 64);

    go(collect(workers, back_router));
    go(generate_work(jobs));
    go(route_work(workers, back_router, jobs)); 

    msleep(now() + 5435345);
    
    int front_router = nn_socket(AF_SP_RAW, NN_REP);
    nn_bind(front_router, FRONTROUTER);
    
    char ctrl [256];
    char body [256];
    char ctrl2 [256];
    char body2 [256];

    struct nn_msghdr hdr;
    struct nn_msghdr hdr2;

    struct nn_iovec iovec;
    iovec.iov_base = body;
    iovec.iov_len = sizeof(body);

    memset(&hdr, 0, sizeof (hdr));
    hdr.msg_iov = &iovec;
    hdr.msg_iovlen = 1;

    hdr.msg_control = ctrl;
    hdr.msg_controllen = sizeof (ctrl);

    int rc = nn_recvmsg(back_router, &hdr, 0);
    printf("got rc: %d, got ctrl: %d\n", rc, hdr.msg_controllen);

    struct nn_iovec iovec2;
    iovec2.iov_base = body2;
    iovec2.iov_len = sizeof(body2);

    memset(&hdr2, 0, sizeof (hdr));

    hdr2.msg_iov = &iovec2;
    hdr2.msg_iovlen = 1;
    hdr2.msg_control = ctrl2;
    hdr2.msg_controllen = sizeof (ctrl2);

    rc = nn_recvmsg(back_router, &hdr2, 0);
    /* 
    struct nn_cmsghdr *cmsg;

    cmsg = NN_CMSG_FIRSTHDR (&hdr);
    while (cmsg != NULL) {
        size_t len = cmsg->cmsg_len - sizeof (struct nn_cmsghdr);
        printf ("level: %d property: %d length: %dB data: ",
            (int) cmsg->cmsg_level,
            (int) cmsg->cmsg_type,
            (int) len);
        unsigned char *data = NN_CMSG_DATA(cmsg);
        while (len) {
            printf ("%02X", *data);
            ++data;
            --len;
        }
        printf ("\n");

        cmsg = NN_CMSG_NXTHDR (&hdr.msg_control, cmsg);
    }
*/
    hdr.msg_iov = &iovec2;
    hdr.msg_iovlen = 1;
    nn_sendmsg(back_router, &hdr, 0);

    /*
    while(1){
        sleep(10);
        buf = NULL;
        printf("waiting for worker\n");

        int n = nn_recv(response, &buf, NN_MSG, 0);

        printf("server got msg: %.*s\n", n, buf);
        sprintf(tevs, "you are number %d served", ++c);
        printf("responding to %d\n",response);
        
        nn_send(response, tevs, strlen(tevs), 0);
        nn_freemsg(buf);
    }
   
    nn_shutdown(response, 0); 
    */
    return 0;
}