Search in sources :

Example 1 with IntObjectMap

use of com.firenio.collection.IntObjectMap in project baseio by generallycloud.

the class NioEventLoop method remove_closed_channels.

private void remove_closed_channels() {
    IntArrayList c_list = this.close_channels;
    IntObjectMap<Channel> channels = this.channels;
    if (!c_list.isEmpty()) {
        for (int i = 0; i < c_list.size(); i++) {
            Channel ch = channels.remove(c_list.get(i));
            if (ch.isOpen()) {
                channels.put(ch.getFd(), ch);
            }
        }
        c_list.clear();
        int keys_length = channels.keys.length;
        if (keys_length >= CHANNELS_COMPACT_SIZE) {
            if (channels.size() < keys_length >>> 3) {
                int map_size = Math.max(CHANNELS_COMPACT_SIZE >>> 2, channels.size());
                IntObjectMap<Channel> new_channels = new IntObjectMap<>(map_size);
                new_channels.putAll(channels);
                this.channels = new_channels;
            }
        }
    }
}
Also used : IntObjectMap(com.firenio.collection.IntObjectMap) SocketChannel(java.nio.channels.SocketChannel) IntArrayList(com.carrotsearch.hppc.IntArrayList)

Example 2 with IntObjectMap

use of com.firenio.collection.IntObjectMap in project baseio by generallycloud.

the class HttpProxyServer method strtup.

public synchronized void strtup(NioEventLoopGroup group, int port) throws Exception {
    if (context != null && context.isActive()) {
        return;
    }
    IoEventHandle eventHandle = new IoEventHandle() {

        @Override
        public void accept(Channel ch_src, Frame frame) throws Exception {
            final HttpFrame f = (HttpFrame) frame;
            if (f.getMethod() == HttpMethod.CONNECT) {
                ch_src.writeAndFlush(CONNECT_RES_BUF.duplicate());
                HttpProxyAttr s = HttpProxyAttr.get(ch_src);
                String[] arr = f.getHost().split(":");
                s.host = arr[0];
                s.port = Integer.parseInt(arr[1]);
                s.handshakeFinished = true;
            } else {
                String host = f.getHost();
                String[] arr = host.split(":");
                int port = 80;
                if (arr.length == 2) {
                    port = Integer.parseInt(arr[1]);
                }
                if (f.getRequestHeaders().remove(HttpHeader.Proxy_Connection.getId()) == null) {
                    return;
                }
                NioEventLoop el = ch_src.getEventLoop();
                ChannelConnector context = new ChannelConnector(el, arr[0], port);
                context.addProtocolCodec(new ClientHttpCodec());
                context.setIoEventHandle(new IoEventHandle() {

                    @Override
                    public void accept(Channel ch, Frame frame) throws Exception {
                        ClientHttpFrame res = (ClientHttpFrame) frame;
                        IntObjectMap<String> hs = res.getResponse_headers();
                        for (hs.scan(); hs.hasNext(); ) {
                            String v = hs.getValue();
                            if (v == null) {
                                continue;
                            }
                            if (hs.getKey() == HttpHeader.Content_Length.getId() || hs.getKey() == HttpHeader.Connection.getId() || hs.getKey() == HttpHeader.Transfer_Encoding.getId() || hs.getKey() == HttpHeader.Content_Encoding.getId()) {
                                continue;
                            }
                            f.setResponseHeader(hs.getKey(), v.getBytes());
                        }
                        if (res.getContent() != null) {
                            f.setContent(res.getContent());
                        } else if (res.isChunked()) {
                            f.setString("not support chunked now.", ch);
                        }
                        ch_src.writeAndFlush(f);
                        ch.close();
                    }
                });
                String url = parseRequestURL(f.getRequestURL());
                context.setPrintConfig(false);
                context.addChannelEventListener(new LoggerChannelOpenListener());
                context.connect((ch, ex) -> {
                    if (ex == null) {
                        ClientHttpFrame req = new ClientHttpFrame(url, f.getMethod());
                        req.setRequestHeaders(f.getRequestHeaders());
                        req.getRequestHeaders().remove(HttpHeader.Proxy_Connection.getId());
                        if (f.getMethod() == HttpMethod.POST) {
                            req.setContent(f.getContent());
                        }
                        try {
                            ch.writeAndFlush(req);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
    };
    context = new ChannelAcceptor(group, 8088);
    context.addProtocolCodec(new HttpProxyCodec());
    context.setIoEventHandle(eventHandle);
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.bind();
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) Frame(com.firenio.component.Frame) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame) HttpFrame(com.firenio.codec.http11.HttpFrame) Channel(com.firenio.component.Channel) ChannelAcceptor(com.firenio.component.ChannelAcceptor) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame) HttpFrame(com.firenio.codec.http11.HttpFrame) IOException(java.io.IOException) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener) IntObjectMap(com.firenio.collection.IntObjectMap) ChannelConnector(com.firenio.component.ChannelConnector) ClientHttpCodec(com.firenio.codec.http11.ClientHttpCodec) NioEventLoop(com.firenio.component.NioEventLoop) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame)

Aggregations

IntObjectMap (com.firenio.collection.IntObjectMap)2 IntArrayList (com.carrotsearch.hppc.IntArrayList)1 ClientHttpCodec (com.firenio.codec.http11.ClientHttpCodec)1 ClientHttpFrame (com.firenio.codec.http11.ClientHttpFrame)1 HttpFrame (com.firenio.codec.http11.HttpFrame)1 Channel (com.firenio.component.Channel)1 ChannelAcceptor (com.firenio.component.ChannelAcceptor)1 ChannelConnector (com.firenio.component.ChannelConnector)1 Frame (com.firenio.component.Frame)1 IoEventHandle (com.firenio.component.IoEventHandle)1 LoggerChannelOpenListener (com.firenio.component.LoggerChannelOpenListener)1 NioEventLoop (com.firenio.component.NioEventLoop)1 IOException (java.io.IOException)1 SocketChannel (java.nio.channels.SocketChannel)1