X-Git-Url: http://nitlanguage.org diff --git a/lib/socket/socket_c.nit b/lib/socket/socket_c.nit index ba76860..94eecd7 100644 --- a/lib/socket/socket_c.nit +++ b/lib/socket/socket_c.nit @@ -87,13 +87,13 @@ end private extern class NativeSocketPollFD `{ struct pollfd * `} # File descriptor - fun fd: Int `{ return recv->fd; `} + fun fd: Int `{ return self->fd; `} # List of events to be watched - fun events: Int `{ return recv->events; `} + fun events: Int `{ return self->events; `} # List of events received by the last poll function - fun revents: Int `{ return recv->revents; `} + fun revents: Int `{ return self->revents; `} new (pid: Int, events: NativeSocketPollValues) `{ struct pollfd *poll = malloc(sizeof(struct pollfd)); @@ -115,47 +115,51 @@ extern class NativeSocket `{ int* `} return d; `} - fun destroy `{ free(recv); `} + fun destroy `{ free(self); `} - fun close: Int `{ return close(*recv); `} + fun close: Int `{ return close(*self); `} - fun descriptor: Int `{ return *recv; `} - - fun gethostbyname(n: String): NativeSocketHostent import String.to_cstring `{ return gethostbyname(String_to_cstring(n)); `} + fun descriptor: Int `{ return *self; `} fun connect(addrIn: NativeSocketAddrIn): Int `{ - return connect(*recv, (struct sockaddr*)addrIn, sizeof(*addrIn)); + return connect(*self, (struct sockaddr*)addrIn, sizeof(*addrIn)); `} fun write(buffer: String): Int import String.to_cstring, String.length `{ - return write(*recv, (char*)String_to_cstring(buffer), String_length(buffer)); + return write(*self, (char*)String_to_cstring(buffer), String_length(buffer)); + `} + + # Write `value` as a single byte + fun write_byte(value: Int): Int `{ + unsigned char byt = (unsigned char)value; + return write(*self, &byt, 1); `} - fun read: String import NativeString.to_s_with_length `{ - static char c[1024]; - int n = read(*recv, c, 1024); + fun read: String import NativeString.to_s_with_length, NativeString `{ + char *c = new_NativeString(1024); + int n = read(*self, c, 1023); if(n < 0) { return NativeString_to_s_with_length("",0); } - char* ret = malloc(n + 1); - memcpy(ret, c, n); - ret[n] = '\0'; - return NativeString_to_s_with_length(ret, n); + c[n] = 0; + return NativeString_to_s_with_length(c, n); `} # Sets an option for the socket - fun setsockopt(level: NativeSocketOptLevels, option_name: NativeSocketOptNames, option_value: Int) `{ - int err = setsockopt(*recv, level, option_name, &option_value, sizeof(int)); + # + # 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){ - perror("Error on setsockopts: "); - exit(1); + return 0; } + return 1; `} - fun bind(addrIn: NativeSocketAddrIn): Int `{ return bind(*recv, (struct sockaddr*)addrIn, sizeof(*addrIn)); `} + fun bind(addrIn: NativeSocketAddrIn): Int `{ return bind(*self, (struct sockaddr*)addrIn, sizeof(*addrIn)); `} - fun listen(size: Int): Int `{ return listen(*recv, size); `} + 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] @@ -187,7 +191,7 @@ extern class NativeSocket `{ int* `} private fun native_accept(addr_in: NativeSocketAddrIn): NativeSocket `{ socklen_t s = sizeof(struct sockaddr); - int socket = accept(*recv, (struct sockaddr*)addr_in, &s); + int socket = accept(*self, (struct sockaddr*)addr_in, &s); if (socket == -1) return NULL; int *ptr = malloc(sizeof(int)); @@ -205,7 +209,7 @@ extern class NativeSocket `{ int* `} # Set wether this socket is non blocking fun non_blocking=(value: Bool) `{ - int flags = fcntl(*recv, F_GETFL, 0); + int flags = fcntl(*self, F_GETFL, 0); if (flags == -1) flags = 0; if (value) { @@ -215,7 +219,7 @@ extern class NativeSocket `{ int* `} } else { return; } - fcntl(*recv, F_SETFL, flags); + fcntl(*self, F_SETFL, flags); `} end @@ -236,7 +240,7 @@ extern class NativeSocketAddrIn `{ struct sockaddr_in* `} return sai; `} - new with(port: Int, family: NativeSocketAddressFamilies) `{ + new with_port(port: Int, family: NativeSocketAddressFamilies) `{ struct sockaddr_in *sai = NULL; sai = malloc(sizeof(struct sockaddr_in)); sai->sin_family = family; @@ -254,19 +258,19 @@ extern class NativeSocketAddrIn `{ struct sockaddr_in* `} return sai; `} - fun address: String import NativeString.to_s `{ return NativeString_to_s((char*)inet_ntoa(recv->sin_addr)); `} + fun address: String import NativeString.to_s `{ return NativeString_to_s((char*)inet_ntoa(self->sin_addr)); `} - fun family: NativeSocketAddressFamilies `{ return recv->sin_family; `} + fun family: NativeSocketAddressFamilies `{ return self->sin_family; `} - fun port: Int `{ return ntohs(recv->sin_port); `} + fun port: Int `{ return ntohs(self->sin_port); `} - fun destroy `{ free(recv); `} + fun destroy `{ free(self); `} end extern class NativeSocketHostent `{ struct hostent* `} - private fun native_h_aliases(i: Int): String import NativeString.to_s `{ return NativeString_to_s(recv->h_aliases[i]); `} + private fun native_h_aliases(i: Int): String import NativeString.to_s `{ return NativeString_to_s(self->h_aliases[i]); `} - private fun native_h_aliases_reachable(i: Int): Bool `{ return (recv->h_aliases[i] != NULL); `} + private fun native_h_aliases_reachable(i: Int): Bool `{ return (self->h_aliases[i] != NULL); `} fun h_aliases: Array[String] do @@ -280,13 +284,13 @@ extern class NativeSocketHostent `{ struct hostent* `} return d end - fun h_addr: String import NativeString.to_s `{ return NativeString_to_s((char*)inet_ntoa(*(struct in_addr*)recv->h_addr)); `} + fun h_addr: String import NativeString.to_s `{ return NativeString_to_s((char*)inet_ntoa(*(struct in_addr*)self->h_addr)); `} - fun h_addrtype: Int `{ return recv->h_addrtype; `} + fun h_addrtype: Int `{ return self->h_addrtype; `} - fun h_length: Int `{ return recv->h_length; `} + fun h_length: Int `{ return self->h_length; `} - fun h_name: String import NativeString.to_s `{ return NativeString_to_s(recv->h_name); `} + fun h_name: String import NativeString.to_s `{ return NativeString_to_s(self->h_name); `} end extern class NativeTimeval `{ struct timeval* `} @@ -298,11 +302,11 @@ extern class NativeTimeval `{ struct timeval* `} return tv; `} - fun seconds: Int `{ return recv->tv_sec; `} + fun seconds: Int `{ return self->tv_sec; `} - fun microseconds: Int `{ return recv->tv_usec; `} + fun microseconds: Int `{ return self->tv_usec; `} - fun destroy `{ free(recv); `} + fun destroy `{ free(self); `} end extern class NativeSocketSet `{ fd_set* `} @@ -312,15 +316,15 @@ extern class NativeSocketSet `{ fd_set* `} return f; `} - fun set(s: NativeSocket) `{ FD_SET(*s, recv); `} + fun set(s: NativeSocket) `{ FD_SET(*s, self); `} - fun is_set(s: NativeSocket): Bool `{ return FD_ISSET(*s, recv); `} + fun is_set(s: NativeSocket): Bool `{ return FD_ISSET(*s, self); `} - fun zero `{ FD_ZERO(recv); `} + fun zero `{ FD_ZERO(self); `} - fun clear(s: NativeSocket) `{ FD_CLR(*s, recv); `} + fun clear(s: NativeSocket) `{ FD_CLR(*s, self); `} - fun destroy `{ free(recv); `} + fun destroy `{ free(self); `} end class NativeSocketObserver @@ -473,6 +477,55 @@ extern class NativeSocketPollValues `{ int `} # Combines two NativeSocketPollValues private fun +(other: NativeSocketPollValues): NativeSocketPollValues `{ - return recv | other; + return self | other; + `} +end + +redef class Sys + # Get network host entry + fun gethostbyname(name: NativeString): NativeSocketHostent `{ + return gethostbyname(name); `} + + # Last error raised by `gethostbyname` + fun h_errno: HErrno `{ return h_errno; `} +end + +# Error code of `Sys::h_errno` +extern class HErrno `{ int `} + # The specified host is unknown + fun host_not_found: Bool `{ return self == HOST_NOT_FOUND; `} + + # The requested name is valid but does not have an IP address + # + # Same as `no_data`. + fun no_address: Bool `{ return self == NO_ADDRESS; `} + + # The requested name is valid byt does not have an IP address + # + # Same as `no_address`. + fun no_data: Bool `{ return self == NO_DATA; `} + + # A nonrecoverable name server error occurred + fun no_recovery: Bool `{ return self == NO_RECOVERY; `} + + # A temporary error occurred on an authoritative name server, try again later + fun try_again: Bool `{ return self == TRY_AGAIN; `} + + redef fun to_s + do + if host_not_found then + return "The specified host is unknown" + else if no_address then + return "The requested name is valid but does not have an IP address" + else if no_recovery then + return "A nonrecoverable name server error occurred" + else if try_again then + return "A temporary error occurred on an authoritative name server, try again later" + else + # This may happen if another call was made to `gethostbyname` + # before we fetch the error code. + return "Unknown error on `gethostbyname`" + end + end end