Method: Nmsg::Socket#send_msg

Defined in:
ext/nmsg/rubyext.c

#send_msg(obj) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'ext/nmsg/rubyext.c', line 139

VALUE rb_socket_send_msg(VALUE self, VALUE obj) {
    GET_SOCKET(self);
    if (!S || S->fd == -1)
        return Qnil;

    const int msg_bytes = RSTRING_LEN(obj);
    const char *data = RSTRING_PTR(obj);
    void *msg = nn_allocmsg(msg_bytes, 0);
    if (!msg)
        return Qnil;
    memcpy(msg, data, msg_bytes);

    const int nbytes = nn_send(S->fd, &msg, NN_MSG, NN_DONTWAIT);
    if (nbytes < 0) {
        nn_freemsg(msg);
        return Qnil;
    }

    return (nbytes == msg_bytes) ? Qtrue : Qfalse;
}