socket :: TCPStream :: defaultinit
socket :: TCPStream :: ready_to_read
Easier use of pollin to check for something to read on all channels of any prioritysocket $ TCPStream :: raw_read_byte
Read a byte directly from the underlying stream, withoutsocket $ TCPStream :: raw_read_bytes
Read at mostmax
bytes from the underlying stream into buf
,
socket $ TCPStream :: write_bytes_from_cstring
Writelen
bytes from ns
core :: Reader :: append_line_to
Read a string until the end of the line and append it tos
.
binary :: BinaryStream :: big_endian
Use the big-endian convention? otherwise use little-endian.binary :: BinaryStream :: big_endian=
Use the big-endian convention? otherwise use little-endian.core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
core :: Reader :: defaultinit
core :: Writer :: defaultinit
socket :: TCPStream :: defaultinit
core :: Stream :: defaultinit
socket :: TCPSocket :: defaultinit
core :: Duplex :: defaultinit
socket :: Socket :: defaultinit
binary :: BinaryStream :: defaultinit
core :: PollableReader :: defaultinit
core :: Object :: defaultinit
core :: Reader :: deserialize_msgpack
Deserialize full Nitnullable Object
from MessagePack formated data
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
core :: Stream :: last_error=
Error produced by the file streamcore :: Stream :: lookahead=
Lookahead buffer for codecscore :: Stream :: lookahead_capacity
Capacity of the lookaheadcore :: Stream :: lookahead_capacity=
Capacity of the lookaheadcore :: Stream :: lookahead_length
Current occupation of the lookaheadcore :: Stream :: lookahead_length=
Current occupation of the lookaheadcore :: Object :: output_class_name
Display class name on stdout (debug only).core :: PollableReader :: poll_in
Is there something to read? (without blocking)core :: Reader :: raw_read_byte
Read a byte directly from the underlying stream, withoutcore :: Reader :: raw_read_bytes
Read at mostmax
bytes from the underlying stream into buf
,
core :: Reader :: read_block
Read the length as a 64 bits integer, then the content of the blockcore :: Reader :: read_bytes_to_cstring
Reads up tomax
bytes from source and stores them in bytes
core :: Reader :: read_double
Read a floating point on 64 bits and return it as aFloat
core :: Reader :: read_float
Read a floating point on 32 bits and return it as aFloat
core :: Reader :: read_int64
Read a signed integer on 64 bits and return is anInt
core :: Reader :: read_msgpack
Read the next MessagePack object and return it as a simple Nit objectcore :: Reader :: read_nonwhitespace
Skip whitespace characters (if any) then return the following non-whitespace character.socket :: TCPStream :: ready_to_read
Easier use of pollin to check for something to read on all channels of any prioritycore :: Writer :: serialize_msgpack
Serializevalue
in MessagePack format
core :: Writer :: write_block
Write the length as a 64 bits integer, then the content oftext
core :: Writer :: write_bool
Write a booleanvalue
on a byte, using 0 for false
and 1 for true
core :: Stream :: write_buffer
Buffer for writing data to a streamcore :: Stream :: write_buffer=
Buffer for writing data to a streamcore :: Writer :: write_bytes_from_cstring
Writelen
bytes from ns
core :: Writer :: write_double
Write a floating pointvalue
on 64 bits
core :: Writer :: write_int64
Writevalue
as a signed integer on 64 bits
core :: Writer :: write_msgpack_array
Write an array header forlen
items in the shortest possible MessagePack array format
core :: Writer :: write_msgpack_array16
Write an array header forlen
items, max of 0xFFFF items
core :: Writer :: write_msgpack_array32
Write an array header forlen
items, max of 0xFFFF_FFFF items
core :: Writer :: write_msgpack_bin
Writedata
in the shortest possible MessagePack bin format
core :: Writer :: write_msgpack_bin16
Writedata
in bin16 format, max of 0xFFFF bytes
core :: Writer :: write_msgpack_bin32
Writedata
in bin32 format, max of 0xFFFF_FFFF bytes
core :: Writer :: write_msgpack_bin8
Writedata
in bin8 format, max of 0xFF bytes
core :: Writer :: write_msgpack_bool
Writebool
in MessagePack format
core :: Writer :: write_msgpack_double
Writevalue
as a MessagePack double
core :: Writer :: write_msgpack_ext
Write an application-specific extension fortyp
and bytes
in the shortest possible MessagePack ext format
core :: Writer :: write_msgpack_ext16
Write the header for an application-specific extension oflen
data bytes
core :: Writer :: write_msgpack_ext32
Write the header for an application-specific extension oflen
data bytes
core :: Writer :: write_msgpack_ext8
Write the header for an application-specific extension oflen
data bytes
core :: Writer :: write_msgpack_fixarray
Write an array header forlen
items, max of 0x0F items
core :: Writer :: write_msgpack_fixext1
Write the header for an application-specific extension of one data bytecore :: Writer :: write_msgpack_fixext16
Write the header for an application-specific extension of 16 data bytescore :: Writer :: write_msgpack_fixext2
Write the header for an application-specific extension of two data bytescore :: Writer :: write_msgpack_fixext4
Write the header for an application-specific extension of 4 data bytescore :: Writer :: write_msgpack_fixext8
Write the header for an application-specific extension of 8 data bytescore :: Writer :: write_msgpack_fixint
Writevalue
as a single byte with metadata
core :: Writer :: write_msgpack_fixmap
Write a map header forlen
key/value pairs, max of 0x0F pairs
core :: Writer :: write_msgpack_fixstr
Writetext
in fixstr format, max of 0x1F bytes
core :: Writer :: write_msgpack_float
Writevalue
as a MessagePack float (losing precision)
core :: Writer :: write_msgpack_int
Write the integervalue
either as the shortest possible MessagePack int
core :: Writer :: write_msgpack_int16
Writevalue
over two signed bytes, following 1 metadata byte
core :: Writer :: write_msgpack_int32
Writevalue
over 4 signed bytes, following 1 metadata byte
core :: Writer :: write_msgpack_int64
Writevalue
over 8 signed bytes, following 1 metadata byte
core :: Writer :: write_msgpack_int8
Writevalue
over one signed byte, following 1 metadata byte
core :: Writer :: write_msgpack_map
Write a map header forlen
keys/value pairs in the shortest possible MessagePack map format
core :: Writer :: write_msgpack_map16
Write a map header forlen
key/value pairs, max of 0xFFFF pairs
core :: Writer :: write_msgpack_map32
Write a map header forlen
key/value pairs, max of 0xFFFF_FFFF pairs
core :: Writer :: write_msgpack_null
Writenull
, or nil, in MessagePack format
core :: Writer :: write_msgpack_str
Writetext
in the shortest possible MessagePack format
core :: Writer :: write_msgpack_str16
Writetext
in str16 format, max of 0xFFFF bytes
core :: Writer :: write_msgpack_str32
Writetext
in str32 format, max of 0xFFFF_FFFF bytes
core :: Writer :: write_msgpack_str8
Writetext
in str8 format, max of 0xFF bytes
core :: Writer :: write_msgpack_uint16
Writevalue
over two unsigned bytes, following 1 metadata byte
core :: Writer :: write_msgpack_uint32
Writevalue
over 4 unsigned bytes, following 1 metadata byte
core :: Writer :: write_msgpack_uint64
Writevalue
over 8 unsigned bytes, following 1 metadata byte
core :: Writer :: write_msgpack_uint8
Writevalue
over one unsigned byte, following 1 metadata byte
Reader
capable of declaring if readable without blocking
# Simple communication stream with a remote socket
class TCPStream
super TCPSocket
super PollableReader
super Duplex
# Real canonical name of the host to which `self` is connected
var host: String
private var addrin: NativeSocketAddrIn is noinit
redef var closed = false
# TODO make init private
# Creates a socket connection to host `host` on port `port`
init connect(host: String, port: Int)
do
native = new NativeSocket.socket(new NativeSocketAddressFamilies.af_inet,
new NativeSocketTypes.sock_stream, new NativeSocketProtocolFamilies.pf_unspec)
if native.address_is_null then
closed = true
return
end
if not native.setsockopt(new NativeSocketOptLevels.socket, new NativeSocketOptNames.reuseaddr, 1) then
closed = true
return
end
var hostent = sys.gethostbyname(host.to_cstring)
if hostent.address_is_null then
# Error in name lookup
last_error = new IOError.from_h_errno
closed = true
return
end
addrin = new NativeSocketAddrIn
addrin.fill_from_hostent hostent
addrin.port = port
address = addrin.address.to_s
init(addrin.port, hostent.h_name.to_s)
closed = not internal_connect
if closed then
# Connection failed
last_error = new IOError.from_errno
end
end
# Creates a client socket, this is meant to be used by accept only
private init server_side(h: SocketAcceptResult)
do
native = h.socket
addrin = h.addr_in
address = addrin.address.to_s
init(addrin.port, address)
end
redef fun poll_in do return ready_to_read(0)
# Returns an array containing an enum of the events ready to be read
#
# event_types : Combination of several event types to watch
#
# timeout : Time in milliseconds before stopping listening for events on this socket
private fun pollin(event_types: Array[NativeSocketPollValues], timeout: Int): Array[NativeSocketPollValues] do
if closed then return new Array[NativeSocketPollValues]
return native.socket_poll(new PollFD.from_poll_values(native.descriptor, event_types), timeout)
end
# Easier use of pollin to check for something to read on all channels of any priority
#
# timeout : Time in milliseconds before stopping to wait for events
fun ready_to_read(timeout: Int): Bool
do
var events = [new NativeSocketPollValues.pollin]
return pollin(events, timeout).length != 0
end
# Is this socket still connected?
fun connected: Bool
do
if closed then return false
if native.poll_hup_err == 0 then
return true
else
closed = true
return false
end
end
redef fun is_writable do return not closed
# Establishes a connection to socket addrin
#
# REQUIRES : not self.end_reached
private fun internal_connect: Bool
do
assert not closed
return native.connect(addrin) >= 0
end
redef fun raw_read_byte do
var rd = native.read(write_buffer, 1)
if rd < 1 then return -1
return write_buffer[0].to_i
end
redef fun raw_read_bytes(ns, max) do
var rd = native.read(ns, max)
print "Read {rd} bytes"
if rd < 0 then return -1
return rd
end
# If socket.end_reached, nothing will happen
redef fun write(msg)
do
if closed then return
native.write(msg.to_cstring, msg.length)
end
redef fun write_byte(value)
do
if closed then return
native.write_byte value
end
redef fun write_bytes_from_cstring(ns, len) do
if closed then return
native.write(ns, len)
end
# Write `msg`, with a trailing `\n`
fun write_ln(msg: Text)
do
if closed then return
write msg.to_s
write "\n"
end
redef fun close
do
if closed then return
if native.close >= 0 then
closed = true
end
end
# Send the data present in the socket buffer
fun flush
do
if not native.setsockopt(new NativeSocketOptLevels.tcp, new NativeSocketOptNames.tcp_nodelay, 1) or
not native.setsockopt(new NativeSocketOptLevels.tcp, new NativeSocketOptNames.tcp_nodelay, 0) then
closed = true
end
end
end
lib/socket/socket.nit:55,1--217,3