Native C socket

Introduced properties

fun accept: nullable SocketAcceptResult

socket :: NativeSocket :: accept

Accept a new connection on self
fun bind(addrIn: NativeSocketAddrIn): Int

socket :: NativeSocket :: bind

Bind the socket to a local address
fun close: Int

socket :: NativeSocket :: close

Close the socket in both read/write
fun connect(addrIn: NativeSocketAddrIn): Int

socket :: NativeSocket :: connect

Connect to another open socket
fun descriptor: Int

socket :: NativeSocket :: descriptor

Get the FD related to self
fun destroy

socket :: NativeSocket :: destroy

Free the socket
fun listen(size: Int): Int

socket :: NativeSocket :: listen

Prepare for listening to incoming connections
fun non_blocking=(value: Bool)

socket :: NativeSocket :: non_blocking=

Set whether this socket is non blocking
fun poll_hup_err: Int

socket :: NativeSocket :: poll_hup_err

Poll this socket with POLLHUP|POLLERR
fun read(buffer: CString, length: Int): Int

socket :: NativeSocket :: read

Read length bytes into buffer, returns the number of bytes read
fun recv(buf: CString, len: Int, flags: Int): Int

socket :: NativeSocket :: recv

Receive a message into buf of maximum len bytes
fun recvfrom(buf: CString, len: Int, flags: Int, src_addr: NativeSocketAddrIn): Int

socket :: NativeSocket :: recvfrom

Receive a message into buf of maximum len bytes and store sender info into src_addr
fun sendto(buf: CString, len: Int, flags: Int, dest_addr: NativeSocketAddrIn): Int

socket :: NativeSocket :: sendto

Send len bytes from buf to dest_addr
fun setsockopt(level: NativeSocketOptLevels, option_name: NativeSocketOptNames, option_value: Int): Bool

socket :: NativeSocket :: setsockopt

Sets an option for the socket
init socket(domain: NativeSocketAddressFamilies, socketType: NativeSocketTypes, protocol: NativeSocketProtocolFamilies): NativeSocket

socket :: NativeSocket :: socket

Create a new C socket
fun socket_poll(filedesc: PollFD, timeout: Int): Array[NativeSocketPollValues]

socket :: NativeSocket :: socket_poll

Checks if the buffer is ready for any event specified when creating the pollfd structure
fun write(buffer: CString, length: Int): Int

socket :: NativeSocket :: write

Write length bytes from buffer
fun write_byte(value: Int): Int

socket :: NativeSocket :: write_byte

Write value as a single byte

Redefined properties

redef type SELF: NativeSocket

socket $ NativeSocket :: SELF

Type of this instance, automatically specialized in every class

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
fun accept: nullable SocketAcceptResult

socket :: NativeSocket :: accept

Accept a new connection on self
fun address_is_null: Bool

core :: Pointer :: address_is_null

Is the address behind this Object at NULL?
fun bind(addrIn: NativeSocketAddrIn): Int

socket :: NativeSocket :: bind

Bind the socket to a local address
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun close: Int

socket :: NativeSocket :: close

Close the socket in both read/write
fun connect(addrIn: NativeSocketAddrIn): Int

socket :: NativeSocket :: connect

Connect to another open socket
fun descriptor: Int

socket :: NativeSocket :: descriptor

Get the FD related to self
fun destroy

socket :: NativeSocket :: destroy

Free the socket
fun free

core :: Pointer :: free

Free the memory pointed by this pointer
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun listen(size: Int): Int

socket :: NativeSocket :: listen

Prepare for listening to incoming connections
fun non_blocking=(value: Bool)

socket :: NativeSocket :: non_blocking=

Set whether this socket is non blocking
init nul: Pointer

core :: Pointer :: nul

C NULL pointer
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun poll_hup_err: Int

socket :: NativeSocket :: poll_hup_err

Poll this socket with POLLHUP|POLLERR
fun read(buffer: CString, length: Int): Int

socket :: NativeSocket :: read

Read length bytes into buffer, returns the number of bytes read
fun recv(buf: CString, len: Int, flags: Int): Int

socket :: NativeSocket :: recv

Receive a message into buf of maximum len bytes
fun recvfrom(buf: CString, len: Int, flags: Int, src_addr: NativeSocketAddrIn): Int

socket :: NativeSocket :: recvfrom

Receive a message into buf of maximum len bytes and store sender info into src_addr
fun sendto(buf: CString, len: Int, flags: Int, dest_addr: NativeSocketAddrIn): Int

socket :: NativeSocket :: sendto

Send len bytes from buf to dest_addr
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun setsockopt(level: NativeSocketOptLevels, option_name: NativeSocketOptNames, option_value: Int): Bool

socket :: NativeSocket :: setsockopt

Sets an option for the socket
init socket(domain: NativeSocketAddressFamilies, socketType: NativeSocketTypes, protocol: NativeSocketProtocolFamilies): NativeSocket

socket :: NativeSocket :: socket

Create a new C socket
fun socket_poll(filedesc: PollFD, timeout: Int): Array[NativeSocketPollValues]

socket :: NativeSocket :: socket_poll

Checks if the buffer is ready for any event specified when creating the pollfd structure
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
fun write(buffer: CString, length: Int): Int

socket :: NativeSocket :: write

Write length bytes from buffer
fun write_byte(value: Int): Int

socket :: NativeSocket :: write_byte

Write value as a single byte
package_diagram socket::NativeSocket NativeSocket core::Pointer Pointer socket::NativeSocket->core::Pointer core::Object Object core::Pointer->core::Object ...core::Object ... ...core::Object->core::Object

Ancestors

interface Object

core :: Object

The root of the class hierarchy.

Parents

extern class Pointer

core :: Pointer

Pointer classes are used to manipulate extern C structures.

Class definitions

socket $ NativeSocket
# Native C socket
extern class NativeSocket `{ int* `}

	# Create a new C socket
	new socket(domain: NativeSocketAddressFamilies, socketType: NativeSocketTypes, protocol: NativeSocketProtocolFamilies) `{
		int ds = socket(domain, socketType, protocol);
		if(ds == -1){
			return NULL;
		}
		int *d = malloc(sizeof(int));
		memcpy(d, &ds, sizeof(ds));
		return d;
	`}

	# Free the socket
	fun destroy `{ free(self); `}

	# Close the socket in both read/write
	fun close: Int `{ return close(*self); `}

	# Get the FD related to `self`
	fun descriptor: Int `{ return *self; `}

	# Connect to another open socket
	#
	# SEE: C documentation for more details on the `connect` operation
	fun connect(addrIn: NativeSocketAddrIn): Int `{
		return connect(*self, (struct sockaddr*)addrIn, sizeof(*addrIn));
	`}

	# Write `length` bytes from `buffer`
	fun write(buffer: CString, length: Int): Int `{
		return write(*self, buffer, length);
	`}

	# Write `value` as a single byte
	fun write_byte(value: Int): Int `{
		unsigned char byt = (unsigned char)value;
		return write(*self, &byt, 1);
	`}

	# Read `length` bytes into `buffer`, returns the number of bytes read
	fun read(buffer: CString, length: Int): Int `{
		return read(*self, buffer, length);
	`}

	# Sets an option for the socket
	#
	# Returns `true` on success.
	fun setsockopt(level: NativeSocketOptLevels, option_name: NativeSocketOptNames, option_value: Int): Bool `{
		int err = setsockopt(*self, level, option_name, &option_value, sizeof(int));
		if(err != 0){
			return 0;
		}
		return 1;
	`}

	# Bind the socket to a local address
	#
	# SEE: C documentation for more details on the bind operation
	fun bind(addrIn: NativeSocketAddrIn): Int `{ return bind(*self, (struct sockaddr*)addrIn, sizeof(*addrIn)); `}

	# Prepare for listening to incoming connections
	fun listen(size: Int): Int `{ return listen(*self, size); `}

	# Checks if the buffer is ready for any event specified when creating the pollfd structure
	fun socket_poll(filedesc: PollFD, timeout: Int): Array[NativeSocketPollValues]
	do
		var result = native_poll(filedesc.poll_struct, timeout)
		assert result != -1
		return filedesc.check_response(result)
	end

	# Poll this socket with `POLLHUP|POLLERR`
	#
	# A return value of 0 means there is no errors.
	fun poll_hup_err: Int `{
		struct pollfd fd = {*self, POLLHUP|POLLERR, 0};
		int res = poll(&fd, 1, 0);
		return res;
	`}

	# Call to the poll function of the C socket
	#
	# Signature:
	# int poll(struct pollfd fds[], nfds_t nfds, int timeout);
	#
	# Official documentation of the poll function:
	#
	# The poll() function provides applications with a mechanism for multiplexing input/output over a set of file descriptors.
	# For each member of the array pointed to by fds, poll() shall examine the given file descriptor for the event(s) specified in events.
	# The number of pollfd structures in the fds array is specified by nfds.
	# The poll() function shall identify those file descriptors on which an application can read or write data, or on which certain events have occurred.
	# The fds argument specifies the file descriptors to be examined and the events of interest for each file descriptor.
	# It is a pointer to an array with one member for each open file descriptor of interest.
	# The array's members are pollfd structures within which fd specifies an open file descriptor and events and revents are bitmasks constructed by
	# OR'ing a combination of the pollfd flags.
	private fun native_poll(filedesc: NativeSocketPollFD, timeout: Int): Int `{
		int poll_return = poll(filedesc, 1, timeout);
		return poll_return;
	`}

	private fun native_accept(addr_in: NativeSocketAddrIn): NativeSocket `{
		socklen_t s = sizeof(struct sockaddr);
		int socket = accept(*self, (struct sockaddr*)addr_in, &s);
		if (socket == -1) return NULL;

		int *ptr = malloc(sizeof(int));
		*ptr = socket;
		return ptr;
	`}

	# Accept a new connection on `self`
	#
	# Require the socket to be first bound and listening for connections
	fun accept: nullable SocketAcceptResult
	do
		var addrIn = new NativeSocketAddrIn
		var s = native_accept(addrIn)
		if s.address_is_null then return null
		return new SocketAcceptResult(s, addrIn)
	end

	# Set whether this socket is non blocking
	fun non_blocking=(value: Bool) `{
		int flags = fcntl(*self, F_GETFL, 0);
		if (flags == -1) flags = 0;

		if (value) {
			flags = flags | O_NONBLOCK;
		} else if (flags & O_NONBLOCK) {
			flags = flags - O_NONBLOCK;
		} else {
			return;
		}
		fcntl(*self, F_SETFL, flags);
	`}

	# Send `len` bytes from `buf` to `dest_addr`
	fun sendto(buf: CString, len: Int, flags: Int, dest_addr: NativeSocketAddrIn): Int `{
		return sendto(*self, buf, len, flags, (struct sockaddr*)dest_addr, sizeof(struct sockaddr_in));
	`}

	# Receive a message into `buf` of maximum `len` bytes
	fun recv(buf: CString, len: Int, flags: Int): Int `{
		return recv(*self, buf, len, flags);
	`}

	# Receive a message into `buf` of maximum `len` bytes and store sender info into `src_addr`
	fun recvfrom(buf: CString, len: Int, flags: Int, src_addr: NativeSocketAddrIn): Int `{
		socklen_t srclen = sizeof(struct sockaddr_in);
		return recvfrom(*self, buf, len, flags, (struct sockaddr*)src_addr, &srclen);
	`}
end
lib/socket/socket_c.nit:108,1--261,3