h3rald / min

A small but practical concatenative programming language and shell
https://min-lang.org
MIT License
309 stars 23 forks source link

Sockets library #12

Closed sug0 closed 6 years ago

sug0 commented 6 years ago

Is it possible for you to implement a mini sockets library? Thanks.

h3rald commented 6 years ago

Funny that you ask... actually buried deep down in the commit history of this repository there is an implementation of something like that.

Unfortunately I ended up removing it mainly because at the time I didn’t have a clean enough way to manage “foreign objects” in min. Foreign because there’s nothing in min that could be used to represent a socket and passed around on the stack... we’d need some sort of opaque object containing a reference to a native Nim socket object and some symbols to manage them.

I’ll give it another try, why not! Thank you for the suggestion. Were you after a bare-bones, low-level socket implementation (open/close/listen/send/receive) or maybe also something more high level like http requests and responses? I could try implementing a generic net module and see how it goes...

sug0 commented 6 years ago

More generic low level sockets, and if possible, ssl support. A socket could just be an object that you could manipulate around, same thing with a file. The current interface for files is probably opening a file descriptor on each fwrite or fread call, right?

I'm not quite familiar with nim, so I can't really help you with the implementation details.

sug0 commented 6 years ago

I was trying to write a mini irc bot in min. My first approach was to use netcat, which didn't work well, after I realized I didn't have a standard way of opening a process, and interacting with it (writing to stdin, reading from stdout, etc)... On the other hand, simple things like http requests are fairly standard with utilities like curl, and the built in 'from-json'. The best option is to create some sort of IO object. It's generic enough to represent sockets, processes and files.

h3rald commented 6 years ago

There... I added a basic but fully functional net module that provides support for sockets. This should be enough to perform simple requests and create simple socket servers (see examples in the docs).

No SSL though, although it could be possible to add it... it's just that the security warnings on the Nim docs on lacks of testing for SSL support for sockets kinda put me off.