/* * Create and size the buffers appropriately. */ private void createBuffers() { /* * We'll assume the buffer sizes are the same * between client and server. */ SSLSession session = clientEngine.getSession(); int appBufferMax = session.getApplicationBufferSize(); int netBufferMax = session.getPacketBufferSize(); /* * We'll make the input buffers a bit bigger than the max needed * size, so that unwrap()s following a successful data transfer * won't generate BUFFER_OVERFLOWS. * * We'll use a mix of direct and indirect ByteBuffers for * tutorial purposes only. In reality, only use direct * ByteBuffers when they give a clear performance enhancement. */ clientIn = ByteBuffer.allocate(appBufferMax + 50); serverIn = ByteBuffer.allocate(appBufferMax + 50); cTOs = ByteBuffer.allocateDirect(netBufferMax); sTOc = ByteBuffer.allocateDirect(netBufferMax); clientOut = ByteBuffer.wrap("Hi Server, I'm Client".getBytes()); serverOut = ByteBuffer.wrap("Hello Client, I'm Server".getBytes()); }
void isReadable(SelectionKey k) { EventableChannel ec = (EventableChannel) k.attachment(); long b = ec.getBinding(); if (ec.isWatchOnly()) { if (ec.isNotifyReadable()) eventCallback(b, EM_CONNECTION_NOTIFY_READABLE, null); } else { myReadBuffer.clear(); try { ec.readInboundData(myReadBuffer); myReadBuffer.flip(); if (myReadBuffer.limit() > 0) { if (ProxyConnections != null) { EventableChannel target = ProxyConnections.get(b); if (target != null) { ByteBuffer myWriteBuffer = ByteBuffer.allocate(myReadBuffer.limit()); myWriteBuffer.put(myReadBuffer); myWriteBuffer.flip(); target.scheduleOutboundData(myWriteBuffer); } else { eventCallback(b, EM_CONNECTION_READ, myReadBuffer); } } else { eventCallback(b, EM_CONNECTION_READ, myReadBuffer); } } } catch (IOException e) { UnboundConnections.add(b); } } }
public EmReactor() { Timers = new TreeMap<Long, ArrayList<Long>>(); Connections = new HashMap<Long, EventableChannel>(); Acceptors = new HashMap<Long, ServerSocketChannel>(); NewConnections = new ArrayList<Long>(); UnboundConnections = new ArrayList<Long>(); DetachedConnections = new ArrayList<EventableSocketChannel>(); BindingIndex = 0; loopBreaker = new AtomicBoolean(); loopBreaker.set(false); myReadBuffer = ByteBuffer.allocate( 32 * 1024); // don't use a direct buffer. Ruby doesn't seem to like them. timerQuantum = 98; }