Skip to content

Commit

Permalink
Low-level backend support for sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Aug 19, 2012
1 parent 51face1 commit ec0eab3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/Builtins.cs
Expand Up @@ -7,6 +7,7 @@
using System.Runtime.CompilerServices;
using System.Threading;
using System.Text;
using System.Net.Sockets;

namespace Niecza {
// IForeignInterpreter is used for runtime loading of the Perl 5
Expand Down Expand Up @@ -2934,4 +2935,29 @@ public class Blackhole : Variable {
public static int blob_len(byte[] inp) {
return inp.Length;
}

public static Socket socket_new(int family, int type, int proto) {
return new Socket( (AddressFamily)family, (SocketType)type,
(ProtocolType)proto );
}

public static byte[] socket_read(Socket from, int ct) {
byte[] buf = new byte[ct];
int rct = from.Receive(buf);
Array.Resize(ref buf, rct);
return buf;
}

public static void socket_write(Socket sock, byte[] data) {
sock.Send(data);
}

public static void socket_close(Socket sock) {
sock.Shutdown( SocketShutdown.Both ); // XXX perhaps should be separate
sock.Close();
}

public static void socket_connect(Socket sock, string host, int port) {
sock.Connect(host, port);
}
}
1 change: 1 addition & 0 deletions lib/CodeGen.cs
Expand Up @@ -2450,6 +2450,7 @@ class NamProcessor {
namtypes["treader"] = typeof(TextReader);
namtypes["twriter"] = typeof(TextWriter);
namtypes["blob"] = typeof(byte[]);
namtypes["socket"] = typeof(System.Net.Sockets.Socket);

handlers = new Dictionary<string, Func<NamProcessor,object[],CpsOp>>();
thandlers = new Dictionary<string, Func<CpsOp[], CpsOp>>();
Expand Down

0 comments on commit ec0eab3

Please sign in to comment.