List of usage examples for io.netty.buffer ByteBuf writeBytes
public abstract ByteBuf writeBytes(ByteBuffer src);
From source file:com.openddal.server.mysql.MySQLAuthenticator.java
License:Apache License
private static void error(Channel channel, int errno, String msg) { ByteBuf out = channel.alloc().buffer(); ERR err = new ERR(); err.errorCode = errno;//from www. j a va 2s .co m err.errorMessage = msg; out.writeBytes(err.toPacket()); channel.writeAndFlush(out); }
From source file:com.openddal.server.mysql.MySQLServerHandler.java
License:Apache License
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { ByteBuf out = ctx.alloc().buffer(); Handshake handshake = new Handshake(); handshake.sequenceId = 0;//from ww w. java2 s . c om handshake.protocolVersion = MySQLServer.PROTOCOL_VERSION; handshake.serverVersion = MySQLServer.SERVER_VERSION; handshake.connectionId = session.getThreadId(); handshake.challenge1 = StringUtil.getRandomString(8); handshake.characterSet = CharsetUtil.getIndex(MySQLServer.DEFAULT_CHARSET); handshake.statusFlags = Flags.SERVER_STATUS_AUTOCOMMIT; handshake.challenge2 = StringUtil.getRandomString(12); handshake.authPluginDataLength = 21; handshake.authPluginName = Flags.MYSQL_NATIVE_PASSWORD; handshake.capabilityFlags = Flags.CLIENT_BASIC_FLAGS; handshake.removeCapabilityFlag(Flags.CLIENT_COMPRESS); handshake.removeCapabilityFlag(Flags.CLIENT_SSL); handshake.removeCapabilityFlag(Flags.CLIENT_LOCAL_FILES); session.setCharsetIndex((int) handshake.characterSet); session.setAttachment("seed", handshake.challenge1 + handshake.challenge2); out.writeBytes(handshake.toPacket()); ctx.writeAndFlush(out); }
From source file:com.openddal.server.mysql.MySQLServerHandler.java
License:Apache License
private void success(ChannelHandlerContext ctx) { ByteBuf out = ctx.alloc().buffer(); OK ok = new OK(); ok.sequenceId = nextSequenceId();//from w w w.ja va2s .c o m ok.setStatusFlag(Flags.SERVER_STATUS_AUTOCOMMIT); out.writeBytes(ok.toPacket()); ctx.writeAndFlush(out); }
From source file:com.openddal.server.mysql.MySQLServerHandler.java
License:Apache License
private void sendError(ChannelHandlerContext ctx, int errno, String msg) { ByteBuf out = ctx.alloc().buffer(); ERR err = new ERR(); err.sequenceId = nextSequenceId();/*from ww w . jav a 2s. co m*/ err.errorCode = errno; err.errorMessage = msg; out.writeBytes(err.toPacket()); ctx.writeAndFlush(out); ACCESSLOGGER.markError(errno, msg); }
From source file:com.openddal.server.mysql.MySQLServerHandler.java
License:Apache License
private void sendError(ChannelHandlerContext ctx, Throwable t) { SQLException e = ServerException.toSQLException(t); StringWriter writer = new StringWriter(500); e.printStackTrace(new PrintWriter(writer)); String message = writer.toString(); ByteBuf out = ctx.alloc().buffer(); ERR err = new ERR(); err.sequenceId = nextSequenceId();//from w w w .java 2 s . com err.errorCode = e instanceof JdbcSQLException ? ErrorCode.ER_ERROR_WHEN_EXECUTING_COMMAND : e.getErrorCode(); err.errorMessage = message; out.writeBytes(err.toPacket()); ctx.writeAndFlush(out); ACCESSLOGGER.markError((int) err.errorCode, err.errorMessage); }
From source file:com.openddal.server.mysql.MySQLServerHandler.java
License:Apache License
private void sendUpdateResult(ChannelHandlerContext ctx, QueryResult rs) { ByteBuf out = ctx.alloc().buffer(); OK ok = new OK(); ok.sequenceId = nextSequenceId();//from w w w . j av a 2s . c om ok.affectedRows = rs.getUpdateResult(); ok.setStatusFlag(Flags.SERVER_STATUS_AUTOCOMMIT); out.writeBytes(ok.toPacket()); ctx.writeAndFlush(out); }
From source file:com.openddal.server.mysql.MySQLServerHandler.java
License:Apache License
private void sendQueryResult(ChannelHandlerContext ctx, QueryResult rs) { Resultset resultset = new Resultset(); ByteBuf out = ctx.alloc().buffer(); try {/* w w w .j a va 2 s .co m*/ resultset.sequenceId = nextSequenceId(); Resultset.characterSet = session.getCharsetIndex(); ResultInterface result = rs.getQueryResult(); int columnCount = result.getVisibleColumnCount(); for (int i = 0; i < columnCount; i++) { ColumnDefinition columnPacket = ResultColumn.getColumn(result, i); resultset.addColumn(columnPacket); } while (result.next()) { ResultsetRow rowPacket = new ResultsetRow(); Value[] v = result.currentRow(); for (int i = 0; i < columnCount; i++) { Value value = v[i]; rowPacket.data.add(value.getString()); } resultset.addRow(rowPacket); } List<byte[]> packets = resultset.toPackets(); for (byte[] bs : packets) { out.writeBytes(bs); } } catch (Exception e) { ERR err = new ERR(); err.sequenceId = resultset.sequenceId++; err.errorCode = ErrorCode.ER_UNKNOWN_ERROR; err.errorMessage = "write resultset error:" + e.getMessage(); out.writeBytes(err.toPacket()); } finally { ctx.writeAndFlush(out); } }
From source file:com.phei.netty.chapter4.fault.TimeClientHandler.java
License:Apache License
@Override public void channelActive(ChannelHandlerContext ctx) { ByteBuf message; for (int i = 0; i < 100; i++) { message = Unpooled.buffer(req.length); message.writeBytes(req); ctx.writeAndFlush(message);//from w w w .java 2 s . c o m } }
From source file:com.phei.netty.protocol.netty.codec.NettyMessageEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, NettyMessage msg, ByteBuf sendBuf) throws Exception { if (msg == null || msg.getHeader() == null) throw new Exception("The encode message is null"); sendBuf.writeInt((msg.getHeader().getCrcCode())); sendBuf.writeInt((msg.getHeader().getLength())); sendBuf.writeLong((msg.getHeader().getSessionID())); sendBuf.writeByte((msg.getHeader().getType())); sendBuf.writeByte((msg.getHeader().getPriority())); sendBuf.writeInt((msg.getHeader().getAttachment().size())); String key = null;// w ww. java2s . com byte[] keyArray = null; Object value = null; for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) { key = param.getKey(); keyArray = key.getBytes("UTF-8"); sendBuf.writeInt(keyArray.length); sendBuf.writeBytes(keyArray); value = param.getValue(); marshallingEncoder.encode(value, sendBuf); } key = null; keyArray = null; value = null; //body?4int if (msg.getBody() != null) { marshallingEncoder.encode(msg.getBody(), sendBuf); } else { sendBuf.writeInt(0); } sendBuf.setInt(4, sendBuf.readableBytes() - 8); }
From source file:com.pubkit.platform.messaging.protocol.mqtt.proto.parser.ConnectEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext chc, ConnectMessage message, ByteBuf out) { ByteBuf staticHeaderBuff = chc.alloc().buffer(12); ByteBuf buff = chc.alloc().buffer(); ByteBuf variableHeaderBuff = chc.alloc().buffer(12); try {//from www .j a v a 2 s .co m staticHeaderBuff.writeBytes(Utils.encodeString("MQIsdp")); //version staticHeaderBuff.writeByte(0x03); //connection flags and Strings byte connectionFlags = 0; if (message.isCleanSession()) { connectionFlags |= 0x02; } if (message.isWillFlag()) { connectionFlags |= 0x04; } connectionFlags |= ((message.getWillQos() & 0x03) << 3); if (message.isWillRetain()) { connectionFlags |= 0x020; } if (message.isPasswordFlag()) { connectionFlags |= 0x040; } if (message.isUserFlag()) { connectionFlags |= 0x080; } staticHeaderBuff.writeByte(connectionFlags); //Keep alive timer staticHeaderBuff.writeShort(message.getKeepAlive()); //Variable part if (message.getClientID() != null) { variableHeaderBuff.writeBytes(Utils.encodeString(message.getClientID())); if (message.isWillFlag()) { variableHeaderBuff.writeBytes(Utils.encodeString(message.getWillTopic())); variableHeaderBuff.writeBytes(Utils.encodeString(message.getWillMessage())); } if (message.isUserFlag() && message.getUsername() != null) { variableHeaderBuff.writeBytes(Utils.encodeString(message.getUsername())); if (message.isPasswordFlag() && message.getPassword() != null) { variableHeaderBuff.writeBytes(Utils.encodeString(message.getPassword())); } } } int variableHeaderSize = variableHeaderBuff.readableBytes(); buff.writeByte(AbstractMessage.CONNECT << 4); buff.writeBytes(Utils.encodeRemainingLength(12 + variableHeaderSize)); buff.writeBytes(staticHeaderBuff).writeBytes(variableHeaderBuff); out.writeBytes(buff); } finally { staticHeaderBuff.release(); buff.release(); variableHeaderBuff.release(); } }