Пример #1
0
        public BBMessageContent(BaseBulletinBoard board, BulletinMessage msg) : base(0x71)
        {
            var poster  = SafeString(msg.PostedName);
            var subject = SafeString(msg.Subject);
            var time    = SafeString(msg.GetTimeAsString());

            EnsureCapacity(22 + poster.Length + subject.Length + time.Length);

            Stream.Write((byte)0x02);   // Packet ID
            Stream.Write(board.Serial); // Bulletin board serial
            Stream.Write(msg.Serial);   // Message serial

            WriteString(poster);
            WriteString(subject);
            WriteString(time);

            Stream.Write((short)msg.PostedBody);
            Stream.Write((short)msg.PostedHue);

            var len = msg.PostedEquip.Length;

            if (len > 255)
            {
                len = 255;
            }

            Stream.Write((byte)len);

            for (var i = 0; i < len; ++i)
            {
                var eq = msg.PostedEquip[i];

                Stream.Write((short)eq._itemID);
                Stream.Write((short)eq._hue);
            }

            len = msg.Lines.Length;

            if (len > 255)
            {
                len = 255;
            }

            Stream.Write((byte)len);

            for (var i = 0; i < len; ++i)
            {
                WriteString(msg.Lines[i], true);
            }
        }
Пример #2
0
        public BBMessageHeader(BaseBulletinBoard board, BulletinMessage msg) : base(0x71)
        {
            var poster  = SafeString(msg.PostedName);
            var subject = SafeString(msg.Subject);
            var time    = SafeString(msg.GetTimeAsString());

            EnsureCapacity(22 + poster.Length + subject.Length + time.Length);

            Stream.Write((byte)0x01);                        // Packet ID
            Stream.Write(board.Serial);                      // Bulletin board serial
            Stream.Write(msg.Serial);                        // Message serial

            Stream.Write(msg.Thread?.Serial ?? Serial.Zero); // Thread serial--parent

            WriteString(poster);
            WriteString(subject);
            WriteString(time);
        }
Пример #3
0
        public static void SendBBMessage(this NetState ns, BaseBulletinBoard board, BulletinMessage msg, bool content = false)
        {
            if (ns == null)
            {
                return;
            }

            var longestTextLine = 0;
            var maxLength       = 22;
            var poster          = msg.PostedName ?? "";

            poster.UpdateLengthCounters(ref maxLength, ref longestTextLine);

            var subject = msg.Subject ?? "";

            subject.UpdateLengthCounters(ref maxLength, ref longestTextLine);

            var time = msg.GetTimeAsString() ?? "";

            time.UpdateLengthCounters(ref maxLength, ref longestTextLine);

            var equipLength = Math.Min(255, msg.PostedEquip.Length);
            var linesLength = Math.Min(255, msg.Lines.Length);

            if (content)
            {
                maxLength += 2 + equipLength * 4; // We have an extra 4 from the thread serial
                for (int i = 0; i < linesLength; i++)
                {
                    msg.Lines[i].UpdateLengthCounters(ref maxLength, ref longestTextLine, true);
                }
            }

            Span <byte> textBuffer = stackalloc byte[TextEncoding.UTF8.GetMaxByteCount(longestTextLine)];

            var writer = maxLength > 81920 ? new SpanWriter(maxLength) : new SpanWriter(stackalloc byte[maxLength]);

            writer.Write((byte)0x71);                    // Packet ID
            writer.Seek(2, SeekOrigin.Current);
            writer.Write((byte)(content ? 0x02 : 0x01)); // Command
            writer.Write(board.Serial);
            writer.Write(msg.Serial);
            if (!content)
            {
                writer.Write(msg.Thread?.Serial ?? Serial.Zero);
            }

            writer.WriteString(poster, textBuffer);
            writer.WriteString(subject, textBuffer);
            writer.WriteString(time, textBuffer);

            if (content)
            {
                writer.Write((short)msg.PostedBody);
                writer.Write((short)msg.PostedHue);

                writer.Write((byte)equipLength);
                for (var i = 0; i < equipLength; i++)
                {
                    var eq = msg.PostedEquip[i];
                    writer.Write((short)eq.itemID);
                    writer.Write((short)eq.hue);
                }

                writer.Write((byte)linesLength);
                for (var i = 0; i < linesLength; i++)
                {
                    writer.WriteString(msg.Lines[i], textBuffer, true);
                }
            }

            writer.WritePacketLength();
            ns.Send(writer.Span);

            writer.Dispose();
        }