⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 packet.java

📁 Open DMT GPS server source code
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            if (type == ServerStandardPayloadTemplate_table[i].getPacketType()) {                return ServerStandardPayloadTemplate_table[i];            }        }        return null;    }    // ------------------------------------------------------------------------    // Server to client general packets    public static Packet createServerPacket(int type)    {        return new Packet(null, false, HEADER_BASIC, type);    }    public static Packet createServerPacket(int type, Payload payload)    {        return new Packet(null, false, HEADER_BASIC, type, payload);    }    public static Packet createServerPacket(int type, byte payload[])    {        return new Packet(null, false, HEADER_BASIC, type, payload);    }        // ------------------------------------------------------------------------    // Server to client error packets    public static Packet createServerErrorPacket(int errCode, Packet causePkt)    {        Payload payload = new Payload();        payload.writeULong((long)errCode, 2);        if (causePkt != null) {            payload.writeULong((long)causePkt.getPacketHeader() , 1);            payload.writeULong((long)causePkt.getPacketType()   , 1);        } else {            payload.writeULong(0L                               , 1);            payload.writeULong(0L                               , 1);        }        // caller may still want to write additional arguments to the specific error payload        return createServerPacket(PKT_SERVER_ERROR, payload);    }        public static Packet createServerErrorPacket(int errCode, int header, int type)    {        Payload payload = new Payload();        payload.writeULong((long)errCode, 2);        payload.writeULong((long)header , 1);        payload.writeULong((long)type   , 1);        // caller may still want to write in arguments to the specific error payload        return createServerPacket(PKT_SERVER_ERROR, payload);    }        // ------------------------------------------------------------------------    // Server to client file upload packet    //   0:1 - Record Type    //   1:3 - File size    //   4:X - File name    /* Tell client to 'Get' file [0x31] */    public static Packet createServerGetFilePacket(String fileName, long fileSize)    {        if (fileName == null) { fileName = ""; }        Payload payload = new Payload();        payload.writeULong((long)0x31, 1); // UPLOAD_TYPE_OOB_GETFILE        payload.writeULong(fileSize, 3);        payload.writeString(fileName, 64);        return createServerPacket(PKT_SERVER_FILE_UPLOAD, payload);    }    /* Tell client to 'Put' file [0x41] */    public static Packet createServerPutFilePacket(String fileName)    {        if (fileName == null) { fileName = ""; }        Payload payload = new Payload();        payload.writeULong((long)0x41, 1); // UPLOAD_TYPE_OOB_PUTFILE        payload.writeULong(0L, 3);        payload.writeString(fileName, 64);        return createServerPacket(PKT_SERVER_FILE_UPLOAD, payload);    }    // ------------------------------------------------------------------------    // Server to client property packets    public static Packet createServerGetPropertyPacket(int propCode, byte propData[], int propDataLen)    {        Payload payload = new Payload();        payload.writeULong((long)propCode, 2);        payload.writeBytes(propData, propDataLen);        return createServerPacket(PKT_SERVER_GET_PROPERTY, payload);    }    public static Packet createServerSetPropertyPacket(int propCode, byte propData[], int propDataLen)    {        Payload payload = new Payload();        payload.writeULong((long)propCode, 2);        payload.writeBytes(propData, propDataLen);        return createServerPacket(PKT_SERVER_SET_PROPERTY, payload);    }    public static Packet createServerSetPropertyPacket(int propCode, long value[])    {        byte propData[] = PropCodes.encodePropertyData(propCode, value);        if (propData != null) {            return createServerSetPropertyPacket(propCode, propData, propData.length);        } else {            return null;        }    }    public static Packet createServerSetPropertyPacket(int propCode, double value[])    {        byte propData[] = PropCodes.encodePropertyData(propCode, value);        if (propData != null) {            return createServerSetPropertyPacket(propCode, propData, propData.length);        } else {            return null;        }    }    public static Packet createServerSetPropertyPacket(int propCode, String value)    {        byte propData[] = PropCodes.encodePropertyData(propCode, ((value!=null)?value:""));        if (propData != null) {            return createServerSetPropertyPacket(propCode, propData, propData.length);        } else {            return null;        }    }    // ------------------------------------------------------------------------    private static int CalcChecksum(byte b[])    {        if (b == null) {            return -1;        } else {            int cksum = 0, s = 0;            if ((b.length > 0) && (b[0] == Encoding.AsciiEncodingChar)) { s++; }            for (; s < b.length; s++) {                if (b[s] == Encoding.AsciiChecksumChar ) { break; }                if (b[s] == Encoding.AsciiEndOfLineChar) { break; }                cksum = (cksum ^ b[s]) & 0xFF;            }            return cksum;        }    }        // ------------------------------------------------------------------------        private int             encoding           = Encoding.ENCODING_BINARY;    private boolean         hasAsciiChecksum   = false;    private DeviceID        deviceId           = null; // only used to parse custom device packets    private PayloadTemplate payloadTemplate    = null;    private boolean         isClient           = true;    private int             header             = 0;    private int             type               = 0;    private Payload         payload            = null;    public Packet(PayloadTemplate template, int header, byte payload[])    {        this.deviceId        = null; // not needed for server packets        this.payloadTemplate = template;        this.isClient        = true;        this.header          = header;        this.type            = template.getPacketType();        this.payload         = new Payload(payload);    }    public Packet(DeviceID devId, boolean isClient, int header, int type)    {        this(devId, isClient, header, type, new Payload());    }    public Packet(DeviceID devId, boolean isClient, int header, int type, byte payload[])    {        this(devId, isClient, header, type, new Payload(payload));    }    public Packet(DeviceID devId, boolean isClient, int header, int type, Payload payload)    {        this.deviceId        = devId; // not needed for server packets        this.payloadTemplate = null;        this.isClient        = isClient;        this.header          = header;        this.type            = type;        this.payload         = payload;    }    public Packet(DeviceID devId, boolean isClient, String pkt)         throws PacketParseException     {        this(devId, isClient, StringTools.getBytes(pkt));    }    public Packet(DeviceID devId, boolean isClient, byte pkt[])         throws PacketParseException     {        // 'pkt' always contains only a single packet        this.deviceId = devId; // (for custom templates) not needed for server packets        this.isClient = isClient;        this.encoding = Encoding.ENCODING_UNKNOWN;        if (pkt.length < 3) {                        this.header = (pkt.length > 0)? ((int)pkt[0] & 0xFF) : 0x00;            this.type   = (pkt.length > 1)? ((int)pkt[1] & 0xFF) : 0x00;            throw new PacketParseException(ServerErrors.NAK_PACKET_LENGTH, this); // errData ok                    } else        if (pkt[0] == Encoding.AsciiEncodingChar) {                        /* checksum */            int pLen = 1; // start with first character after AsciiEndOfLineChar            int cksumActual = 0, cksumTest = -1;            this.hasAsciiChecksum = false;            for (;(pLen < pkt.length) && (pkt[pLen] != Encoding.AsciiEndOfLineChar); pLen++) {                if (pkt[pLen] == Encoding.AsciiChecksumChar) {                    this.hasAsciiChecksum = true;                    String hexCksum = StringTools.toStringValue(pkt, pLen + 1, 2);                    cksumTest = StringTools.parseHexInt(hexCksum, -1);                    break;                }                cksumActual = (cksumActual ^ pkt[pLen]) & 0xFF;            }            // 'pLen' now represents length of actual packet string.                        /* string packet */            String p = StringTools.toStringValue(pkt, 0, pLen);                        /* header */            this.header = (pLen >= 3)? StringTools.parseHexInt(p.substring(1,3), 0x00) : 0x00;            this.type   = (pLen >= 5)? StringTools.parseHexInt(p.substring(3,5), 0x00) : 0x00;            if (this.header != HEADER_BASIC) {                throw new PacketParseException(ServerErrors.NAK_PACKET_HEADER, this); // errData ok            }            /* minimum length */            if (pLen < 5) { // eg. "$E0D1"                throw new PacketParseException(ServerErrors.NAK_PACKET_LENGTH, this); // errData ok            }                        /* check checksum */            // wait until header/type are parsed before testing checksum            if (cksumTest < 0) {                // record does not contain a checksum            } else            if (cksumTest != cksumActual) {                // If the checksum fails, then we really can't trust any information contained in the                 // packet, thus we are unable to accurately let the client know specifically which packet                // had the problem.                throw new PacketParseException(ServerErrors.NAK_PACKET_CHECKSUM, this); // errData ok            } else {                //Print.logDebug("Checksum is OK!");            }            /* payload encoding */            int ench = (p.length() >= 6)? p.charAt(5) : -1;            if ((ench == Encoding.AsciiEndOfLineChar) || (ench < 0)) {                // encoding not known, assign default                this.encoding = this.hasAsciiChecksum? Encoding.ENCODING_BASE64_CKSUM : Encoding.ENCODING_BASE64;                this.payload  = new Payload(new byte[0]);            } else            if (ench == Encoding.ENCODING_HEX_CHAR) {                // Hex                this.encoding = this.hasAsciiChecksum? Encoding.ENCODING_HEX_CKSUM : Encoding.ENCODING_HEX;                this.payload  = new Payload(StringTools.parseHex(p.substring(6), new byte[0]));            } else            if (ench == Encoding.ENCODING_BASE64_CHAR) {                // Base64                this.encoding = this.hasAsciiChecksum? Encoding.ENCODING_BASE64_CKSUM : Encoding.ENCODING_BASE64;                this.payload  = new Payload(Base64.decode(p.substring(6)));            } else            if (ench == Encoding.ENCODING_CSV_CHAR) {                // CSV                this.encoding = this.hasAsciiChecksum? Encoding.ENCODING_CSV_CKSUM : Encoding.ENCODING_CSV;                this.payload  = _decodeCSV(this, p.substring(6));            } else {                // unrecognized encoding                throw new PacketParseException(ServerErrors.NAK_PACKET_ENCODING, this); // errData ok            }                    } else        if (pkt[0] == (byte)HEADER_BASIC) {                        /* binary header */            this.encoding = Encoding.ENCODING_BINARY;            this.header   = (int)pkt[0] & 0xFF;            this.type     = (int)pkt[1] & 0xFF;                        /* check payload length */            int len = (int)pkt[2] & 0xFF;            if (len != pkt.length - 3) {                throw new PacketParseException(ServerErrors.NAK_PACKET_LENGTH, this); // errData ok            }                        /* payload */            this.payload  = new Payload(pkt, 3, len);                    } else {                        this.encoding = Encoding.ENCODING_UNKNOWN;            this.header   = (int)pkt[0] & 0xFF;            this.type     = (int)pkt[1] & 0xFF;            throw new PacketParseException(ServerErrors.NAK_PACKET_HEADER, this); // errData ok        }            }    public Packet(byte pkt[])         throws PacketParseException     {        // parsed 'server' packet only        this(null, false, pkt);    }        // ------------------------------------------------------------------------    public void setEncoding(int encoding)    {        this.encoding = encoding;    }        public int getEncoding()    {        return this.encoding;    }        // ------------------------------------------------------------------------    public int getPacketHeader()    {        return this.header;    }        public boolean isBasicPacketHeader()    {        return (this.getPacketHeader() == HEADER_BASIC);    }        // ------------------------------------------------------------------------    public int getPacketType()    {        return this.type;    }    public boolean isIdentType()    {        int t = this.getPacketType();        return (t == PKT_CLIENT_UNIQUE_ID ) ||                (t == PKT_CLIENT_ACCOUNT_ID) ||               (t == PKT_CLIENT_DEVICE_ID );    }    public boolean isEventType()    {        return Packet.isEventType(this.getPacketType());    }    public static boolean isEventType(int t)    {        if (Packet.isFixedEventType(t) || Packet.isCustomEventType(t)) {            return true;        } else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -