Class: Winipc::Pipe::Conn

Inherits:
Object
  • Object
show all
Defined in:
lib/winipc.rb,
ext/winipc/winipc.c

Instance Method Summary collapse

Instance Method Details

#<<(bytes) ⇒ Object



217
218
219
220
# File 'lib/winipc.rb', line 217

def <<(bytes)
  write(bytes)
  self
end

#_read(vmax) ⇒ Object

Conn#_read(maxlen) -> String or nil (EOF). Byte semantics: returns up to maxlen bytes as they arrive.



634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
# File 'ext/winipc/winipc.c', line 634

static VALUE
conn_read(VALUE self, VALUE vmax)
{
    conn_t *c = conn_live(self);
    long max = NUM2LONG(vmax);
    VALUE out;
    long r;

    if (max <= 0) rb_raise(rb_eArgError, "winipc: read length must be > 0");
    if (c->message_read)
        rb_raise(eModeError, "winipc: use #read_message on a message-mode pipe");
    out = rb_str_new(NULL, max);
    r = conn_read_once(c, RSTRING_PTR(out), (DWORD)max);
    if (r == READ_EOF) return Qnil;
    rb_str_set_len(out, r);
    rb_enc_associate(out, rb_ascii8bit_encoding());
    return out;
}

#_read_message(vmax) ⇒ Object

Conn#_read_message(maxlen) -> String or nil. Reassembles a whole message, growing past ERROR_MORE_DATA.



655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
# File 'ext/winipc/winipc.c', line 655

static VALUE
conn_read_message(VALUE self, VALUE vmax)
{
    conn_t *c = conn_live(self);
    long max = NUM2LONG(vmax);
    OVERLAPPED ov;
    DWORD n = 0, gle, off = 0, cap;
    VALUE out;
    BOOL ok;

    if (max <= 0) rb_raise(rb_eArgError, "winipc: read length must be > 0");
    cap = (DWORD)max;
    out = rb_str_new(NULL, cap);

    for (;;) {
        DWORD wgle = 0;
        memset(&ov, 0, sizeof(ov));
        ov.hEvent = c->read_event;
        ResetEvent(c->read_event);
        ok = ReadFile(c->h, RSTRING_PTR(out) + off, cap - off, &n, &ov);
        if (!ok) {
            gle = GetLastError();
            if (gle == ERROR_IO_PENDING) {
                DWORD w = ov_wait(c->h, &ov, INFINITE, &wgle);
                if (w != WAIT_OBJECT_0) { ov_drain(c->h, &ov); raise_gle("WaitForSingleObject", wgle); }
                ok = GetOverlappedResult(c->h, &ov, &n, FALSE);
                if (!ok) gle = GetLastError();
            }
            if (!ok) {
                if (gle == ERROR_MORE_DATA) {
                    off += n;
                    if (cap > 0x7FFFFFFFu / 2u) rb_raise(rb_eArgError, "winipc: message too large");
                    cap *= 2;
                    rb_str_resize(out, cap);
                    continue;
                }
                if (gle == ERROR_BROKEN_PIPE || gle == ERROR_PIPE_NOT_CONNECTED) {
                    if (off == 0) return Qnil; /* clean EOF before any bytes */
                    raise_gle("ReadFile", gle);
                }
                raise_gle("ReadFile", gle);
            }
        }
        off += n;
        break;
    }
    rb_str_set_len(out, off);
    rb_enc_associate(out, rb_ascii8bit_encoding());
    return out;
}

#_write(data) ⇒ Object

Conn#_write(bytes) -> Integer bytes written.



707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
# File 'ext/winipc/winipc.c', line 707

static VALUE
conn_write(VALUE self, VALUE data)
{
    conn_t *c = conn_live(self);
    OVERLAPPED ov;
    DWORD len, n = 0, gle, wgle = 0;
    const char *p;
    BOOL ok;

    len = dword_len(data);
    p = RSTRING_PTR(data);
    memset(&ov, 0, sizeof(ov));
    ov.hEvent = c->write_event;
    ResetEvent(c->write_event);

    ok = WriteFile(c->h, p, len, &n, &ov);
    if (!ok) {
        gle = GetLastError();
        if (gle == ERROR_IO_PENDING) {
            DWORD w = ov_wait(c->h, &ov, INFINITE, &wgle);
            if (w != WAIT_OBJECT_0) { ov_drain(c->h, &ov); raise_gle("WaitForSingleObject", wgle); }
            ok = GetOverlappedResult(c->h, &ov, &n, FALSE);
            if (!ok) gle = GetLastError();
        }
        if (!ok) raise_gle("WriteFile", gle);
    }
    return ULONG2NUM(n);
}

#closeObject



744
745
746
747
748
749
750
751
752
753
754
755
756
# File 'ext/winipc/winipc.c', line 744

static VALUE
conn_close(VALUE self)
{
    conn_t *c = conn_get(self);
    if (!c->closed && c->h != INVALID_HANDLE_VALUE) {
        CloseHandle(c->h); /* graceful: peer drains buffered data, then EOF */
        c->h = INVALID_HANDLE_VALUE;
    }
    if (c->read_event) { CloseHandle(c->read_event); c->read_event = NULL; }
    if (c->write_event) { CloseHandle(c->write_event); c->write_event = NULL; }
    c->closed = 1;
    return Qnil;
}

#closed?Boolean

Returns:

  • (Boolean)


758
# File 'ext/winipc/winipc.c', line 758

static VALUE conn_closed_p(VALUE self) { return conn_get(self)->closed ? Qtrue : Qfalse; }

#flushObject



736
737
738
739
740
741
742
# File 'ext/winipc/winipc.c', line 736

static VALUE
conn_flush(VALUE self)
{
    conn_t *c = conn_live(self);
    if (!FlushFileBuffers(c->h)) raise_gle("FlushFileBuffers", GetLastError());
    return self;
}

#message?Boolean

Returns:

  • (Boolean)


760
# File 'ext/winipc/winipc.c', line 760

static VALUE conn_message_p(VALUE self) { return conn_get(self)->message_read ? Qtrue : Qfalse; }

#read(maxlen) ⇒ Object

Read up to maxlen bytes (byte mode). Returns a binary String, or nil at clean EOF (peer closed).



199
200
201
# File 'lib/winipc.rb', line 199

def read(maxlen)
  Winipc.run_blocking { _read(maxlen) }
end

#read_message(maxlen = 65_536) ⇒ Object

Read one whole message (message mode), reassembling across the buffer. Returns a binary String or nil at EOF.

Raises:



205
206
207
208
209
# File 'lib/winipc.rb', line 205

def read_message(maxlen = 65_536)
  raise ModeError, "winipc: pipe is not in message mode" unless message?

  Winipc.run_blocking { _read_message(maxlen) }
end

#server?Boolean

Returns:

  • (Boolean)


759
# File 'ext/winipc/winipc.c', line 759

static VALUE conn_server_p(VALUE self) { return conn_get(self)->is_server ? Qtrue : Qfalse; }

#write(bytes) ⇒ Object Also known as: write_message

Write bytes; returns the number of bytes written.



212
213
214
# File 'lib/winipc.rb', line 212

def write(bytes)
  Winipc.run_blocking { _write(bytes) }
end