nitlanguage
/
nit.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Merge: Binary over network
[nit.git]
/
lib
/
socket
/
socket_c.nit
diff --git
a/lib/socket/socket_c.nit
b/lib/socket/socket_c.nit
index
2dd14b5
..
fbf2923
100644
(file)
--- a/
lib/socket/socket_c.nit
+++ b/
lib/socket/socket_c.nit
@@
-37,6
+37,7
@@
in "C" `{
# Wrapper for the data structure PollFD used for polling on a socket
class PollFD
# Wrapper for the data structure PollFD used for polling on a socket
class PollFD
+ super FinalizableOnce
# The PollFD object
private var poll_struct: NativeSocketPollFD
# The PollFD object
private var poll_struct: NativeSocketPollFD
@@
-76,27
+77,30
@@
class PollFD
return response & mask;
`}
return response & mask;
`}
+ redef fun finalize_once
+ do
+ poll_struct.free
+ end
end
# Data structure used by the poll function
end
# Data structure used by the poll function
-private extern class NativeSocketPollFD `{ struct pollfd `}
+private extern class NativeSocketPollFD `{ struct pollfd * `}
- # File descriptor id
- private fun fd: Int `{ return recv.fd; `}
+ # File descriptor
+ fun fd: Int `{ return recv->fd; `}
# List of events to be watched
# List of events to be watched
- private fun events: Int `{ return recv.events; `}
+ fun events: Int `{ return recv->events; `}
# List of events received by the last poll function
# List of events received by the last poll function
- private fun revents: Int `{ return recv.revents; `}
+ fun revents: Int `{ return recv->revents; `}
new (pid: Int, events: NativeSocketPollValues) `{
new (pid: Int, events: NativeSocketPollValues) `{
- struct pollfd poll;
- poll.fd = pid;
- poll.events = events;
+ struct pollfd *poll = malloc(sizeof(struct pollfd));
+ poll->fd = pid;
+ poll->events = events;
return poll;
`}
return poll;
`}
-
end
extern class NativeSocket `{ int* `}
end
extern class NativeSocket `{ int* `}
@@
-128,25
+132,31
@@
extern class NativeSocket `{ int* `}
return write(*recv, (char*)String_to_cstring(buffer), String_length(buffer));
`}
return write(*recv, (char*)String_to_cstring(buffer), String_length(buffer));
`}
- fun read: String import NativeString.to_s_with_length `{
+ # Write `value` as a single byte
+ fun write_byte(value: Int): Int `{
+ unsigned char byt = (unsigned char)value;
+ return write(*recv, &byt, 1);
+ `}
+
+ fun read: String import NativeString.to_s_with_length, NativeString.to_s_with_copy `{
static char c[1024];
static char c[1024];
- int n = read(*recv, c, 1024);
+ int n = read(*recv, c, 1023);
if(n < 0) {
return NativeString_to_s_with_length("",0);
}
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_copy(c);
`}
# Sets an option for the socket
`}
# Sets an option for the socket
- fun setsockopt(level: NativeSocketOptLevels, option_name: NativeSocketOptNames, option_value: Int) `{
+ #
+ # Returns `true` on success.
+ fun setsockopt(level: NativeSocketOptLevels, option_name: NativeSocketOptNames, option_value: Int): Bool `{
int err = setsockopt(*recv, level, option_name, &option_value, sizeof(int));
if(err != 0){
int err = setsockopt(*recv, 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(*recv, (struct sockaddr*)addrIn, sizeof(*addrIn)); `}
@@
-177,7
+187,7
@@
extern class NativeSocket `{ int* `}
# 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 `{
# 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);
+ int poll_return = poll(filedesc, 1, timeout);
return poll_return;
`}
return poll_return;
`}
@@
-232,7
+242,7
@@
extern class NativeSocketAddrIn `{ struct sockaddr_in* `}
return sai;
`}
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;
struct sockaddr_in *sai = NULL;
sai = malloc(sizeof(struct sockaddr_in));
sai->sin_family = family;
@@
-320,6
+330,8
@@
extern class NativeSocketSet `{ fd_set* `}
end
class NativeSocketObserver
end
class NativeSocketObserver
+ # FIXME this implementation is broken. `reads`, `write` and `except`
+ # are boxed objects, passing them to a C function is illegal.
fun select(max: NativeSocket, reads: nullable NativeSocketSet, write: nullable NativeSocketSet,
except: nullable NativeSocketSet, timeout: NativeTimeval): Int `{
fd_set *rds, *wts, *exs = NULL;
fun select(max: NativeSocket, reads: nullable NativeSocketSet, write: nullable NativeSocketSet,
except: nullable NativeSocketSet, timeout: NativeTimeval): Int `{
fd_set *rds, *wts, *exs = NULL;
@@
-366,9
+378,6
@@
extern class NativeSocketAddressFamilies `{ int `}
# Novell Internet Protocol
new af_ipx `{ return AF_IPX; `}
# Novell Internet Protocol
new af_ipx `{ return AF_IPX; `}
- # Integrated Services Digital Network
- new af_isdn `{ return AF_ISDN; `}
-
# IPv6
new af_inet6 `{ return AF_INET6; `}
# IPv6
new af_inet6 `{ return AF_INET6; `}
@@
-385,7
+394,6
@@
extern class NativeSocketProtocolFamilies `{ int `}
new pf_decnet `{ return PF_DECnet; `}
new pf_route `{ return PF_ROUTE; `}
new pf_ipx `{ return PF_IPX; `}
new pf_decnet `{ return PF_DECnet; `}
new pf_route `{ return PF_ROUTE; `}
new pf_ipx `{ return PF_IPX; `}
- new pf_isdn `{ return PF_ISDN; `}
new pf_key `{ return PF_KEY; `}
new pf_inet6 `{ return PF_INET6; `}
new pf_max `{ return PF_MAX; `}
new pf_key `{ return PF_KEY; `}
new pf_inet6 `{ return PF_INET6; `}
new pf_max `{ return PF_MAX; `}