Merge: Binary over network
[nit.git] / lib / socket / socket_c.nit
index 44aff9e..fbf2923 100644 (file)
@@ -132,16 +132,20 @@ extern class NativeSocket `{ int* `}
                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];
-               int n = read(*recv, c, 1024);
+               int n = read(*recv, 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_copy(c);
        `}
 
        # Sets an option for the socket
@@ -238,7 +242,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;