List of usage examples for io.netty.buffer ByteBuf writerIndex
public abstract int writerIndex();
From source file:org.traccar.protocol.Gt06FrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 5) { return null; }//w w w.j av a 2 s. com int length = 2 + 2; // head and tail if (buf.getByte(buf.readerIndex()) == 0x78) { length += 1 + buf.getUnsignedByte(buf.readerIndex() + 2); } else { length += 2 + buf.getUnsignedShort(buf.readerIndex() + 2); } if (buf.readableBytes() >= length && buf.getUnsignedShort(buf.readerIndex() + length - 2) == 0x0d0a) { return buf.readRetainedSlice(length); } int endIndex = buf.readerIndex() - 1; do { endIndex = buf.indexOf(endIndex + 1, buf.writerIndex(), (byte) 0x0d); if (endIndex > 0 && buf.writerIndex() > endIndex + 1 && buf.getByte(endIndex + 1) == 0x0a) { return buf.readRetainedSlice(endIndex + 2 - buf.readerIndex()); } } while (endIndex > 0); return null; }
From source file:org.traccar.protocol.Gt06ProtocolDecoder.java
License:Apache License
private void sendResponse(Channel channel, boolean extended, int type, int index, ByteBuf content) { if (channel != null) { ByteBuf response = Unpooled.buffer(); int length = 5 + (content != null ? content.readableBytes() : 0); if (extended) { response.writeShort(0x7979); response.writeShort(length); } else {//from www . ja v a2s. c o m response.writeShort(0x7878); response.writeByte(length); } response.writeByte(type); if (content != null) { response.writeBytes(content); content.release(); } response.writeShort(index); response.writeShort( Checksum.crc16(Checksum.CRC16_X25, response.nioBuffer(2, response.writerIndex() - 2))); response.writeByte('\r'); response.writeByte('\n'); // ending channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress())); } }
From source file:org.traccar.protocol.Gt06ProtocolDecoder.java
License:Apache License
private void sendPhotoRequest(Channel channel, int pictureId) { ByteBuf photo = photos.get(pictureId); ByteBuf content = Unpooled.buffer(); content.writeInt(pictureId);/*from w ww . j a va2s . c o m*/ content.writeInt(photo.writerIndex()); content.writeShort(Math.min(photo.writableBytes(), 1024)); sendResponse(channel, false, MSG_X1_PHOTO_DATA, 0, content); }
From source file:org.traccar.protocol.Gt06ProtocolDecoder.java
License:Apache License
private Object decodeBasic(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { int length = buf.readUnsignedByte(); int dataLength = length - 5; int type = buf.readUnsignedByte(); DeviceSession deviceSession = null;/*from w w w . j a va 2s . co m*/ if (type != MSG_LOGIN) { deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession == null) { return null; } if (deviceSession.getTimeZone() == null) { deviceSession.setTimeZone(getTimeZone(deviceSession.getDeviceId())); } } if (type == MSG_LOGIN) { String imei = ByteBufUtil.hexDump(buf.readSlice(8)).substring(1); buf.readUnsignedShort(); // type deviceSession = getDeviceSession(channel, remoteAddress, imei); if (deviceSession != null && deviceSession.getTimeZone() == null) { deviceSession.setTimeZone(getTimeZone(deviceSession.getDeviceId())); } if (dataLength > 10) { int extensionBits = buf.readUnsignedShort(); int hours = (extensionBits >> 4) / 100; int minutes = (extensionBits >> 4) % 100; int offset = (hours * 60 + minutes) * 60; if ((extensionBits & 0x8) != 0) { offset = -offset; } if (deviceSession != null) { TimeZone timeZone = deviceSession.getTimeZone(); if (timeZone.getRawOffset() == 0) { timeZone.setRawOffset(offset * 1000); deviceSession.setTimeZone(timeZone); } } } if (deviceSession != null) { sendResponse(channel, false, type, buf.getShort(buf.writerIndex() - 6), null); } } else if (type == MSG_HEARTBEAT) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); getLastLocation(position, null); int status = buf.readUnsignedByte(); position.set(Position.KEY_ARMED, BitUtil.check(status, 0)); position.set(Position.KEY_IGNITION, BitUtil.check(status, 1)); position.set(Position.KEY_CHARGE, BitUtil.check(status, 2)); if (buf.readableBytes() >= 2 + 6) { position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.01); } if (buf.readableBytes() >= 1 + 6) { position.set(Position.KEY_RSSI, buf.readUnsignedByte()); } sendResponse(channel, false, type, buf.getShort(buf.writerIndex() - 6), null); return position; } else if (type == MSG_ADDRESS_REQUEST) { String response = "NA&&NA&&0##"; ByteBuf content = Unpooled.buffer(); content.writeByte(response.length()); content.writeInt(0); content.writeBytes(response.getBytes(StandardCharsets.US_ASCII)); sendResponse(channel, true, MSG_ADDRESS_RESPONSE, 0, content); } else if (type == MSG_TIME_REQUEST) { Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); ByteBuf content = Unpooled.buffer(); content.writeByte(calendar.get(Calendar.YEAR) - 2000); content.writeByte(calendar.get(Calendar.MONTH) + 1); content.writeByte(calendar.get(Calendar.DAY_OF_MONTH)); content.writeByte(calendar.get(Calendar.HOUR_OF_DAY)); content.writeByte(calendar.get(Calendar.MINUTE)); content.writeByte(calendar.get(Calendar.SECOND)); sendResponse(channel, false, MSG_TIME_REQUEST, 0, content); } else if (type == MSG_X1_GPS || type == MSG_X1_PHOTO_INFO) { return decodeX1(channel, buf, deviceSession, type); } else if (type == MSG_WIFI || type == MSG_WIFI_2) { return decodeWifi(channel, buf, deviceSession, type); } else if (type == MSG_INFO) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); getLastLocation(position, null); position.set(Position.KEY_POWER, buf.readShort() * 0.01); return position; } else { return decodeBasicOther(channel, buf, deviceSession, type, dataLength); } return null; }