[swift-server-dev] Sockets API

Michael Chiu hatsuneyuji at icloud.com
Thu Oct 27 18:27:44 CDT 2016


On 27 Oct 2016, at 15:27:56 CDT 2016, Helge Heß <me at helgehess.eu> wrote:
> I can see (and essentially agree) with your point, but then I’m also back at wondering why there is a need for a common socket API at _such_ a low level. 
I think one of the reason why common socket api is necessary since socket is not standardized across platforms (WINSOCK, BSD Socket, Linux Socket…) at all. 
Another reason is probably because of the sockaddr struct family. They have different sizes, different alignment(on different os), often need to cast the pointer around, and incredibly hard to use in pure swift manner. 
Nevertheless I think swift is a great language for both high and low level programming, if we have some swift-like yet low level api we essentially open up the opportunities for other developers.
 
> A framework choosing a custom event loop certainly can work just fine today with the Posix functions available?
I’m not quite sure what you mean here.
> I don’t understand what you are saying here :-) Are you just describing an abstract Socket base class which has a ‘RawSocket’ subclass in which read/write is directly invoking Posix.read/write and a ‘SSLSocket’ subclass which has another implementation of that dealing with the encryption?
> Or do you really want to subclass a socket in say a PostgreSQLSocket and then override a function like `handleData(…)`. That would sound wrong to me. A PGConnection should interact using a socket object, but not inherit from one.

Sorry for my bad explanation, override is definitely not a good word choice. What I mean is 

    protocol Readable {
        func read() -> Data?// how do we read
   }

   protocol Writable {
        func write(data: Data) // how do we write (send, sendfile, …) 
   } 

   protocol Socket: Readable, Writable {}

so we can easily make something like:

class TLSSocket: Socket {
    func read() -> Data? {
    … ssl_read….
    }
    func write(data: Data) {
    … ssl_write….
    }    
}

such that we can easily implement low level optimization and extent to different socket interfaces.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-server-dev/attachments/20161027/9c1ec5ae/attachment.html>


More information about the swift-server-dev mailing list