Example #1
0
        public void write(ByteBuffer buf)
        {
            StringBuilder sbuf = new StringBuilder();

            sbuf.Append("[\"N\",")
            .Append("\"S\",")            // Binary Stream
            .Append("\"").Append(BVersioning.longToString(version)).Append("\"").Append(",")
            .Append("\"L\"").Append(",") // Little Endian
            .Append("\"\"");

            if (bversion != 0)
            {
                sbuf.Append(",").Append(bversion);
            }

            if (sessionId != null)
            {
                sbuf.Append(",\"").Append(sessionId).Append("\"");
            }

            sbuf.Append("]");
            buf.put(Encoding.UTF8.GetBytes(sbuf.ToString()));
        }
Example #2
0
        public void read(ByteBuffer buf)
        {
            String str = Encoding.UTF8.GetString(buf.array(), buf.position(), buf.remaining());

            str = str.Trim();
            if (str.IndexOf("[") == 0)
            {
                int endPos = str.IndexOf("]");
                if (endPos == str.Length - 1)
                {
                    str = str.Substring(1, str.Length - 2);
                    String[] items = str.Split(',');
                    int      idx   = 0;
                    if (items.Length >= 5)
                    {
                        if (items[idx++] == "\"N\"")
                        {
                            protocols = items[idx++];
                            if (protocols.Length >= 3)
                            {
                                protocols = protocols.Substring(1, protocols.Length - 2);

                                String versionStr = items[idx++];
                                versionStr = versionStr.Substring(1, versionStr.Length - 2);
                                version    = BVersioning.stringToLong(versionStr);

                                String byteOrderStr = items[idx++];
                                if (byteOrderStr.Equals("\"L\""))
                                {
                                    byteOrder = ByteOrder.LITTLE_ENDIAN;
                                }
                                else if (byteOrderStr.Equals("\"B\""))
                                {
                                    byteOrder = ByteOrder.BIG_ENDIAN;
                                }
                                else
                                {
                                    byteOrder = ByteOrder.UNDEFINED;
                                }

                                String targetIdStr = items[idx++];
                                if (targetIdStr.Length > 2)
                                {
                                    targetIdStr = targetIdStr.Substring(1, targetIdStr.Length - 2);
                                    targetId    = BTargetId.parseString(targetIdStr);
                                }

                                // BYPS Version.
                                // Due to a bug in versions before BYPS_VERSION_WITH_SESSIONID,
                                // this value is not correctly negotiated. If no sessionId is
                                // found, the bversion is ignored and set to the version number
                                // prior to BYPS_VERSION_WITH_SESSIONID.

                                if (items.Length >= 6)
                                {
                                    bversion = Convert.ToInt32(items[5]);
                                }
                                else
                                {
                                    bversion = 0;
                                }

                                if (items.Length >= 7)
                                {
                                    sessionId = items[6];
                                    if (sessionId.Length > 0)
                                    {
                                        int p = (sessionId[0] == '\"') ? 1 : 0;
                                        int q = (sessionId[sessionId.Length - 1] == '\"') ? sessionId.Length - 1 : 0;
                                        sessionId = sessionId.Substring(p, q - p);
                                    }
                                }
                                else if (bversion >= BMessageHeader.BYPS_VERSION_WITH_SESSIONID)
                                {
                                    bversion = BMessageHeader.BYPS_VERSION_WITH_SESSIONID - 1;
                                }

                                return; // OK
                            }
                        }
                    }
                }
            }

            throw new BException(BExceptionC.CORRUPT, "Invalid negotiate message.");
        }