public boolean handleMessage() throws java.io.IOException { ByteBuffer buf = ByteBuffer.allocate(4); int nBytes = ChannelUtil.fillBuffer(buf, agentSocket); if (nBytes == 0) { Log.info("DomainServer: agent closed connection " + agentSocket); return false; } if (nBytes < 4) { Log.error("DomainServer: invalid message " + nBytes); return false; } int msgLen = buf.getInt(); if (msgLen < 0) { return false; } MVByteBuffer buffer = new MVByteBuffer(msgLen); nBytes = ChannelUtil.fillBuffer(buffer.getNioBuf(), agentSocket); if (nBytes == 0) { Log.info("DomainServer: agent closed connection " + agentSocket); return false; } if (nBytes < msgLen) { Log.error( "DomainServer: invalid message, expecting " + msgLen + " got " + nBytes + " from " + agentSocket); return false; } Message message = (Message) MarshallingRuntime.unmarshalObject(buffer); if (message instanceof AgentHelloMessage) { // Successfully added agent, clear our socket var // so we don't close it. if (handleAgentHello((AgentHelloMessage) message)) agentSocket = null; return false; } else if (message instanceof AllocNameMessage) handleAllocName((AllocNameMessage) message, agentSocket); return true; }
/** make sure the packet as the remote address and remote port set */ static void sendPacket(DatagramChannel dc, RDPPacket packet) { sendMeter.add(); // allocate a buffer int bufSize = 100 + (packet.numEacks() * 4); if (packet.getData() != null) { bufSize += packet.getData().length; sendDataMeter.add(); } MVByteBuffer buf = new MVByteBuffer(bufSize); packet.toByteBuffer(buf); // function flips the buffer int remotePort = packet.getPort(); InetAddress remoteAddr = packet.getInetAddress(); if ((remotePort < 0) || (remoteAddr == null)) { throw new MVRuntimeException("RDPServer.sendPacket: remotePort or addr is null"); } try { int bytes = dc.send(buf.getNioBuf(), new InetSocketAddress(remoteAddr, remotePort)); if (bytes == 0) { Log.error("RDPServer.sendPacket: could not send packet, size=" + bufSize); } if (Log.loggingNet) Log.net( "RDPServer.sendPacket: remoteAddr=" + remoteAddr + ", remotePort=" + remotePort + ", numbytes sent=" + bytes); } catch (java.io.IOException e) { Log.exception( "RDPServer.sendPacket: remoteAddr=" + remoteAddr + ", remotePort=" + remotePort + ", got exception", e); throw new MVRuntimeException("RDPServer.sendPacket", e); } }