List of usage examples for io.netty.buffer ByteBuf writeLong
public abstract ByteBuf writeLong(long value);
From source file:com.mpush.common.message.ByteBufMessage.java
License:Apache License
public void encodeLong(ByteBuf body, long field) { body.writeLong(field); }
From source file:com.nanxiaoqiang.test.netty.protocol.demo1.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"); // Header Start sendBuf.writeInt((msg.getHeader().getCrcCode()));// crc 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 w w . j a v a2 s . 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; // Head End // Body Start if (msg.getBody() != null) {// ? marshallingEncoder.encode(msg.getBody(), sendBuf); } else // ? sendBuf.writeInt(0);// 0int sendBuf.setInt(4, sendBuf.readableBytes() - 8);// 0~3CRC4~7??8? }
From source file:com.neoba.FacebookUserCreateMessage.java
ByteBuf result() throws JSONException { ByteBuf reply = isnametaken ? buffer(6) : buffer(6 + suggestionssize); reply.writeByte(Constants.VERSION);/* ww w . j a v a2 s . co m*/ reply.writeByte(Constants.USER_CREATE); if (!isnametaken) { reply.writeInt(Constants.W_SUCCESS); reply.writeInt(fuser.getFriends().length()); for (int i = 0; i < fuser.getFriends().length(); i++) { reply.writeLong(Long.parseLong(fuser.getFriends().getJSONObject(i).getString("id"))); reply.writeInt(fuser.getFriends().getJSONObject(i).getString("username").length()); reply.writeBytes(fuser.getFriends().getJSONObject(i).getString("username").getBytes()); } } else reply.writeInt(Constants.W_ERR_DUP_USERNAME); return reply; }
From source file:com.neoba.FacebookUserLogin.java
ByteBuf result() throws JSONException { ByteBuf reply; String username;//from w w w. j a v a 2s .co m if (response == Constants.W_SUCCESS) { username = user.getString("username"); reply = buffer(6 + 16 + 4 + username.length()); } else { reply = buffer(6); } reply.writeByte(Constants.VERSION); reply.writeByte(Constants.USER_LOGIN); reply.writeInt(response); if (response == Constants.W_SUCCESS) { reply.writeLong(sessid.getLeastSignificantBits()); reply.writeLong(sessid.getMostSignificantBits()); reply.writeInt(user.getString("username").length()); for (byte b : ((String) user.get("username")).getBytes()) { reply.writeByte(b); } } return reply; }
From source file:com.neoba.messages.DocumentCreateMessage.java
@Override public ByteBuf result() { ByteBuf reply = buffer(2 + 4 + 16); reply.writeByte(Constants.VERSION);//from w ww . ja v a 2s. c o m reply.writeByte(Constants.DOCUMENT_CREATE); reply.writeInt(Constants.W_SUCCESS); reply.writeLong(id.getLeastSignificantBits()); reply.writeLong(id.getMostSignificantBits()); return reply; }
From source file:com.neoba.messages.UserLoginMessage.java
@Override public ByteBuf result() { ByteBuf reply; if (response == Constants.W_SUCCESS) { reply = buffer(6 + 16);/*from w w w. j a v a 2s . co m*/ } else { reply = buffer(6); } reply.writeByte(Constants.VERSION); reply.writeByte(Constants.USER_LOGIN); reply.writeInt(response); if (response == Constants.W_SUCCESS) { reply.writeLong(sessid.getLeastSignificantBits()); reply.writeLong(sessid.getMostSignificantBits()); } return reply; }
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;// ww w.j av a2s . c o m 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.relayrides.pushy.apns.ApnsClientHandler.java
License:Open Source License
@Override public void userEventTriggered(final ChannelHandlerContext context, final Object event) throws Exception { if (event instanceof IdleStateEvent) { assert PING_TIMEOUT < ApnsClient.PING_IDLE_TIME_MILLIS; log.trace("Sending ping due to inactivity."); final ByteBuf pingDataBuffer = context.alloc().ioBuffer(8, 8); pingDataBuffer.writeLong(this.nextPingId++); this.encoder().writePing(context, false, pingDataBuffer, context.newPromise()) .addListener(new GenericFutureListener<ChannelFuture>() { @Override/*from w w w . j a va 2s . co m*/ public void operationComplete(final ChannelFuture future) throws Exception { if (future.isSuccess()) { ApnsClientHandler.this.pingTimeoutFuture = future.channel().eventLoop() .schedule(new Runnable() { @Override public void run() { log.debug("Closing channel due to ping timeout."); future.channel().close(); } }, PING_TIMEOUT, TimeUnit.SECONDS); } else { log.debug("Failed to write PING frame.", future.cause()); future.channel().close(); } } }); this.flush(context); } super.userEventTriggered(context, event); }
From source file:com.replaymod.sponge.recording.spongecommon.SpongeConnectionEventListener.java
License:MIT License
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { if (!firedInitializing && "net.minecraft.network.login.server.S02PacketLoginSuccess".equals(msg.getClass().getName())) { Object profile = Reflection.getFieldValueByType(msg.getClass(), "com.mojang.authlib.GameProfile", msg); String name = (String) Reflection.getFieldValueByType(profile.getClass(), "java.lang.String", profile); UUID uuid = (UUID) Reflection.getFieldValueByType(profile.getClass(), "java.util.UUID", profile); spongeConnection.getGame().getEventManager() .post(new SpongeConnectionInitializingEvent(spongeConnection, name, uuid)); firedInitializing = true;//from w w w .j av a 2 s .c om Optional<Recorder> recorder = spongeConnection.getRecorder(); if (recorder.isPresent()) { SpongeRecorder spongeRecorder = (SpongeRecorder) recorder.get(); ByteBuf buf = ctx.alloc().buffer(16); buf.writeLong(uuid.getMostSignificantBits()); buf.writeLong(uuid.getLeastSignificantBits()); spongeRecorder.writePacket(false, buf); buf.release(); } } super.write(ctx, msg, promise); }
From source file:com.seventh_root.ld33.common.network.packet.clientbound.UnitSpawnClientBoundPacket.java
License:Apache License
@Override public void write(ByteBuf buf) throws UnsupportedEncodingException { super.write(buf); writeString(buf, unitUUID);/*from ww w . j a va2 s . c o m*/ writeString(buf, playerUUID); buf.writeInt(x); buf.writeInt(y); writeString(buf, type); buf.writeLong(completionTime); }