sfml3.rb

Class: SFML::IpAddress

Inherits:
Object
  • Object
show all
Defined in:
ext/network/ip_address.c,
ext/network/ip_address.c

Overview

An IPv4 network address.

Constant Summary collapse

NONE =

An invalid/unspecified address.

rb_obj_freeze(ip_address_to_rb(sfIpAddress_None))
ANY =

Any address, 0.0.0.0, e.g. to bind a listener to all network interfaces.

rb_obj_freeze(ip_address_to_rb(sfIpAddress_Any))
LOCAL_HOST =

The local host address, 127.0.0.1.

rb_obj_freeze(ip_address_to_rb(sfIpAddress_LocalHost))
BROADCAST =

The broadcast address, 255.255.255.255.

rb_obj_freeze(ip_address_to_rb(sfIpAddress_Broadcast))

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new(address) ⇒ IpAddress

address may be an Integer (32-bit representation), a String (dotted decimal or network name), or another IpAddress to copy.

Raises:

  • (TypeError) —

    if address is none of the above



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'ext/network/ip_address.c', line 90

static VALUE IpAddress_initialize(VALUE self, VALUE rb_address) {
    IpAddress* ptr = (IpAddress*)Get_IpAddress_Struct(self);

    if (RB_INTEGER_TYPE_P(rb_address)) {
        ptr->address = sfIpAddress_fromInteger((uint32_t)NUM2UINT(rb_address));
    } else if (RB_TYPE_P(rb_address, T_STRING)) {
        ptr->address = sfIpAddress_fromString(StringValueCStr(rb_address));
    } else if (rb_obj_is_kind_of(rb_address, rb_cIpAddress)) {
        ptr->address = ((IpAddress*)Get_IpAddress_Struct(rb_address))->address;
    } else {
        raise_invalid_argument_class(rb_cIpAddress);
    }

    return self;
}

Class Method Details

.from_bytes(byte0, byte1, byte2, byte3) ⇒ IpAddress

Builds an address from its four decimal byte components (e.g. 192, 168, 1, 1).

Returns:

Returns:



53
54
55
56
# File 'ext/network/ip_address.c', line 53

static VALUE IpAddress_from_bytes(VALUE klass, VALUE rb0, VALUE rb1, VALUE rb2, VALUE rb3) {
    return ip_address_to_rb(sfIpAddress_fromBytes((uint8_t)NUM2INT(rb0), (uint8_t)NUM2INT(rb1),
                                                  (uint8_t)NUM2INT(rb2), (uint8_t)NUM2INT(rb3)));
}

.from_integer(value) ⇒ IpAddress

Builds an address from its 32-bit representation, in host byte order.

Returns:

Returns:



65
66
67
# File 'ext/network/ip_address.c', line 65

static VALUE IpAddress_from_integer(VALUE klass, VALUE rb_integer) {
    return ip_address_to_rb(sfIpAddress_fromInteger((uint32_t)NUM2UINT(rb_integer)));
}

.from_string(address) ⇒ IpAddress

Builds an address from a string, which may be a dotted decimal (+"192.168.1.1"+) or a network name (+"localhost"+).

Returns:

Returns:



41
42
43
# File 'ext/network/ip_address.c', line 41

static VALUE IpAddress_from_string(VALUE klass, VALUE rb_address) {
    return ip_address_to_rb(sfIpAddress_fromString(StringValueCStr(rb_address)));
}

.local_address ⇒ IpAddress

Returns the address of the local computer on its local network.

Returns:

Returns:

  • (IpAddress) —

    the address of the local computer on the local network



114
115
116
# File 'ext/network/ip_address.c', line 114

static VALUE IpAddress_local_address(VALUE klass) {
    return ip_address_to_rb(sfIpAddress_getLocalAddress());
}

.public_address(timeout = Time.zero) ⇒ IpAddress

Blocks while querying an external web service for the computer's public address, as seen from outside the local network.

Returns:

Returns:

  • (IpAddress) —

    IpAddress::NONE if the request timed out or failed



126
127
128
129
130
131
132
133
134
135
136
137
# File 'ext/network/ip_address.c', line 126

static VALUE IpAddress_public_address(int argc, VALUE* argv, VALUE klass) {
    VALUE rb_timeout;
    sfTime timeout = sfTime_Zero;

    rb_scan_args(argc, argv, "01", &rb_timeout);

    if (!NIL_P(rb_timeout)) {
        timeout = time_from_rb(rb_timeout);
    }

    return ip_address_to_rb(sfIpAddress_getPublicAddress(timeout));
}

Instance Method Details

#==(other) ⇒ Boolean

Returns true if other is an IpAddress with the same value.

Returns:

  • (Boolean)

Returns:

  • (Boolean)


170
171
172
173
174
175
176
177
178
179
180
181
# File 'ext/network/ip_address.c', line 170

static VALUE IpAddress_eql(VALUE self, VALUE rb_other) {
    sfIpAddress a = ((IpAddress*)Get_IpAddress_Struct(self))->address;
    sfIpAddress b;

    if (!rb_obj_is_kind_of(rb_other, rb_cIpAddress)) {
        return Qfalse;
    }

    b = ((IpAddress*)Get_IpAddress_Struct(rb_other))->address;

    return BOOL2RB(sfIpAddress_toInteger(a) == sfIpAddress_toInteger(b));
}

#==(other) ⇒ Boolean

Returns true if other is an IpAddress with the same value.

Returns:

  • (Boolean)

Returns:

  • (Boolean)


170
171
172
173
174
175
176
177
178
179
180
181
# File 'ext/network/ip_address.c', line 170

static VALUE IpAddress_eql(VALUE self, VALUE rb_other) {
    sfIpAddress a = ((IpAddress*)Get_IpAddress_Struct(self))->address;
    sfIpAddress b;

    if (!rb_obj_is_kind_of(rb_other, rb_cIpAddress)) {
        return Qfalse;
    }

    b = ((IpAddress*)Get_IpAddress_Struct(rb_other))->address;

    return BOOL2RB(sfIpAddress_toInteger(a) == sfIpAddress_toInteger(b));
}

#hash ⇒ Integer

Returns a hash value consistent with #==, so addresses work as Hash keys.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    a hash suitable for use as a Hash key, consistent with #==



189
190
191
# File 'ext/network/ip_address.c', line 189

static VALUE IpAddress_hash(VALUE self) {
    return UINT2NUM(sfIpAddress_toInteger(((IpAddress*)Get_IpAddress_Struct(self))->address));
}

#to_integer ⇒ Integer

Returns the address as a 32-bit integer.

Returns:

  • (Integer)

Returns:

  • (Integer) —

    the 32-bit representation, in host byte order



159
160
161
# File 'ext/network/ip_address.c', line 159

static VALUE IpAddress_to_integer(VALUE self) {
    return UINT2NUM(sfIpAddress_toInteger(((IpAddress*)Get_IpAddress_Struct(self))->address));
}

#to_s ⇒ String

Returns the address in dotted decimal notation.

Returns:

  • (String)

Returns:

  • (String) —

    dotted decimal representation, e.g. "192.168.1.1"



145
146
147
148
149
150
151
# File 'ext/network/ip_address.c', line 145

static VALUE IpAddress_to_s(VALUE self) {
    char buffer[16];

    sfIpAddress_toString(((IpAddress*)Get_IpAddress_Struct(self))->address, buffer);

    return rb_str_new_cstr(buffer);
}

#to_s ⇒ String

Returns the address in dotted decimal notation.

Returns:

  • (String)

Returns:

  • (String) —

    dotted decimal representation, e.g. "192.168.1.1"



145
146
147
148
149
150
151
# File 'ext/network/ip_address.c', line 145

static VALUE IpAddress_to_s(VALUE self) {
    char buffer[16];

    sfIpAddress_toString(((IpAddress*)Get_IpAddress_Struct(self))->address, buffer);

    return rb_str_new_cstr(buffer);
}