@Override public boolean send(String data) { // Check for and disconnect slow consumers. if (maxScheduledWriteRequests > 0 && ioSession.getScheduledWriteMessages() >= maxScheduledWriteRequests) { Session qfjSession = (Session) ioSession.getAttribute(SessionConnector.QF_SESSION); try { qfjSession.disconnect("Slow consumer", true); } catch (IOException e) { } return false; } // The data is written asynchronously in a MINA thread WriteFuture future = ioSession.write(data); if (synchronousWrites) { try { if (!future.awaitUninterruptibly(synchronousWriteTimeout)) { log.error("Synchronous write timed out after " + synchronousWriteTimeout + "ms"); return false; } } catch (RuntimeException e) { log.error("Synchronous write failed: " + e.getMessage()); return false; } } return true; }
@Override public void filterClose(final NextFilter nextFilter, final IoSession session) throws SSLException { SslHandler handler = (SslHandler) session.getAttribute(SSL_HANDLER); if (handler == null) { // The connection might already have closed, or // SSL might have not started yet. nextFilter.filterClose(session); return; } WriteFuture future = null; try { synchronized (handler) { if (isSslStarted(session)) { future = initiateClosure(nextFilter, session); future.addListener( new IoFutureListener<IoFuture>() { public void operationComplete(IoFuture future) { nextFilter.filterClose(session); } }); } } handler.flushScheduledEvents(); } finally { if (future == null) { nextFilter.filterClose(session); } } }
public void testAcceptorFilterChain() throws Exception { int port = AvailablePortFinder.getNextAvailable(1024 + 1000); IoFilter mockFilter = new MockFilter(); IoHandler mockHandler = new MockHandler(); acceptor.getFilterChain().addLast("mock", mockFilter); acceptor.setHandler(mockHandler); acceptor.bind(new InetSocketAddress(port)); try { connector.setHandler(new IoHandlerAdapter()); ConnectFuture future = connector.connect(new InetSocketAddress("127.0.0.1", port)); future.awaitUninterruptibly(); WriteFuture writeFuture = future.getSession().write(IoBuffer.allocate(16).putInt(0).flip()); writeFuture.awaitUninterruptibly(); Assert.assertTrue(writeFuture.isWritten()); future.getSession().close(true); for (int i = 0; i < 30; i++) { if (result.length() == 2) { break; } Thread.sleep(100); } Assert.assertEquals("FH", result); } finally { acceptor.unbind(); } }
public boolean sendHeartbeat() { try { WriteFuture future = this.session.write(HEARTBEATRESPONSE); return future.getException() == null; } catch (Exception e) { logger.error("send heartbeat error", e); return false; } }
public void sendSample() throws InterruptedException { IoSession session = rs232connector.getReceiverSession(); // SendOnePacket(session, pkt, nc, 1000); CHYCAOutMessage pkt = new CHYCAOutMessage(); WriteFuture wf; byte bt[] = pkt.getContent(); bt[0] = 0; pkt.setContent(bt); wf = session.write(pkt); // 发送消息 wf.awaitUninterruptibly(); Thread.sleep(1000); }
@SuppressWarnings("rawtypes") private void sendErrorResponse(final IoSession session, final RequestWrapper request) { ResponseWrapper responseWrapper = new ResponseWrapper(request.getId(), request.getCodecType(), request.getProtocolType()); responseWrapper.setException( new Exception("server threadpool full,maybe because server is slow or too many requests")); WriteFuture wf = session.write(responseWrapper); wf.addListener( new IoFutureListener() { public void operationComplete(IoFuture future) { if (!((WriteFuture) future).isWritten()) { LOGGER.error("server write response error,request id is: " + request.getId()); } } }); }
@Override public void run() { // TODO Auto-generated method stub super.run(); if (iosession != null) { try { byte[] p; if (ng != null) { if ("十六进制".equals(sendtype)) { p = Utils.getByteArray(ng); } else { p = ng.getBytes("GBK"); } } else { if ("十六进制".equals(sendtype)) { p = Utils.getByteArray(msg); } else { p = msg.getBytes("GBK"); } } IoBuffer ib = IoBuffer.wrap(p, 0, p.length); Log.i("msg", "send" + p.length + "--" + ib.array().length); WriteFuture future = iosession.write(ib); ; future.awaitUninterruptibly(); if (ng != null) { Message mg = handler.obtainMessage(); mg.what = 4; mg.obj = "发送成功"; handler.sendMessage(mg); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); Message mg = handler.obtainMessage(); mg.what = 4; mg.obj = "发送框输入数据格式错误 十六进制模式请使用空格隔开!"; handler.sendMessage(mg); } } }
@SuppressWarnings("rawtypes") public void run() { // pipeline if (message instanceof List) { List messages = (List) message; for (Object messageObject : messages) { threadPool.execute(new HandlerRunnable(session, messageObject, threadPool)); } } else { RequestWrapper request = (RequestWrapper) message; long beginTime = System.currentTimeMillis(); ResponseWrapper responseWrapper = ProtocolFactory.getServerHandler(request.getProtocolType()).handleRequest(request); final int id = request.getId(); // already timeout,so not return if ((System.currentTimeMillis() - beginTime) >= request.getTimeout()) { LOGGER.warn( "timeout,so give up send response to client,requestId is:" + id + ",client is:" + session.getRemoteAddress() + ",consumetime is:" + (System.currentTimeMillis() - beginTime) + ",timeout is:" + request.getTimeout()); return; } WriteFuture wf = session.write(responseWrapper); wf.addListener( new IoFutureListener() { public void operationComplete(IoFuture future) { if (!((WriteFuture) future).isWritten()) { LOGGER.error("server write response error,request id is: " + id); } } }); } }