Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Provide wrapper of asynchronous socket to make the usage of them easier #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
xfeep opened this issue Aug 10, 2014 · 1 comment
Closed
Labels
Milestone

Comments

@xfeep
Copy link
Member

xfeep commented Aug 10, 2014

So far there hasn't any wrapper of asynchronous socket. But asynchronous socket is a little too low level to use them easily. So we need a wrapper of asynchronous socket to make the usage of them easier. This wrapper should also consider the consistent problems with future WebSocket/SSE(Server Sent Event) API provided by Nginx-Clojure.

@xfeep xfeep added the feature label Aug 10, 2014
@xfeep xfeep added this to the 0.2.5 milestone Aug 10, 2014
@xfeep
Copy link
Member Author

xfeep commented Aug 31, 2014

This API is a little like Java 7 NIO.2 Asynchronous Channel.

For Clojure

(defprotocol AsynchronousChannel
  (aclose! [ch]
     "Close the channel")
  (aconnect! [ch url attachment on-err on-done]
     "Connect to the remote url.
      `url can be \"192.168.2.34:80\" , \"www.bing.com:80\", or unix domain socket \"unix:/var/mytest/server.sock\"
      `on-err and `on-done are  functions which have the form (fn[status,attachment]
       when passed to `on-err, `status is an error code which range from 
    NginxClojureAsynSocket.NGX_HTTP_CLOJURE_SOCKET_ERR to NGX_HTTP_CLOJURE_SOCKET_ERR_OUTOFMEMORY
       when passed to `on-done `status is always 0. ")
  (arecv! [ch buf attachment on-err on-done]
     "receive data from the channel.
      `buf can be byte[] or ByteBuffer
      `on-err and `on-done are  functions which have the form (fn[status,attachment]
       when passed to `on-err, `status is an error code which is eof (0) or range from 
    NginxClojureAsynSocket.NGX_HTTP_CLOJURE_SOCKET_ERR to NGX_HTTP_CLOJURE_SOCKET_ERR_OUTOFMEMORY
       when passed to `on-done `status is always > 0 and means number of bytes received.
      ")
  (asend! [ch data attachment on-err on-done]
     "send data to the channel.
      `data can be String, byte[] or ByteBuffer
      `on-err and `on-done are  functions which have the form (fn[status,attachment]
       when passed to `on-err, `status is an error code which is eof (0) or range from 
    NginxClojureAsynSocket.NGX_HTTP_CLOJURE_SOCKET_ERR to NGX_HTTP_CLOJURE_SOCKET_ERR_OUTOFMEMORY
       when passed to `on-done `status is always > 0 and means number of bytes sent.")
  (aclosed? [ch])
  (ashutdown! [ch how]
   "shutdown some kind of events trigger of the socket
    `how can be :soft-read :soft-write :soft-both :read :write :both. If we use :soft-xxx it won't 
    shutdown the physical socket and just turn off the events trigger for better performance.
    Otherwise it will shutdown the physical socket, more details can be found from http://linux.die.net/man/2/shutdown")
  (aset-context! [ch ctx])
  (aget-context [ch])
  (aset-timeout! [ch connect-timeout read-timeout write-timeout]
    "The timeout unit is millisecond.
     if timeout < 0 it will be ignored, if timeout = 0 it means no timeout settings"
    )
  (error-str [ch code]
    "return the error string message from the error code"))

For Java

public class NginxClojureAsynChannel {

    public static interface CompletionListener<T> {
        /**
         * 
         * @param status
         *        read/written number of bytes 
         * @param attachment
         *        the attachment object which was as the last read/write method input argument before CompletionListener argument.
         *        e.g. <code>write(buf, off, size, attachement,listener)</code>
         */
        public void onDone(long status, T attachment);

        /**
         * 
         * @param code
         *        if code = 0, eof for read/write , always > 0
         *        if code < 0, code is error code which range from 
         *        NginxClojureAsynSocket.NGX_HTTP_CLOJURE_SOCKET_ERR to NGX_HTTP_CLOJURE_SOCKET_ERR_OUTOFMEMORY
         *        We can get the error string by invoking buildError(code)
         * @param attachment
         *        the attachment object which was as the last read/write method input argument before CompletionListener argument.
         *        e.g. <code>write(buf, off, size, attachement, listener)</code>
         */
        public void onError(long code, T attachment);
    }

/**
     * if timeout is negative, it will be ignored. if timeout is 0, this means no timeout.
     */
    public void setTimeout(long connectTimeout, long readTimeout, long writeTimeout);
    /**
     * connect to remote server, unix domain socket also supported
     * @param url 
     *        e.g. "192.168.2.34:80" , "www.bing.com:80", or unix domain socket "unix:/var/mytest/server.sock"
     * @param listener 
     *        completion listener
     * @param attachement 
     *        when action is done or meets error this attachment will be passed to the method of the completion listener
     */
    public <T> void connect(String url, T attachement, CompletionListener<T> listener);

    public <T> void write(byte[] buf, long off, long size, T attachement, CompletionListener<T> listener);

    public <T> void write(ByteBuffer buf, T attachement, CompletionListener<T> listener) ;

    public <T> void read(byte[] buf, long off, long size, T attachement, CompletionListener<T> listener);

    public <T> void read(ByteBuffer buf, T attachement, CompletionListener<T> listener);

    public boolean isClosed();

    public void setListener(ChannelListener<NginxClojureAsynChannel> listener) ;

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant