Ejemplo n.º 1
0
        public void Encode(global::Zeze.Serialize.ByteBuffer bb)
        {
            bb.WriteInt4(this.Tag);
            bb.WriteLong(this.Id);
            bb.WriteLong(this.TimeTicks);
            bb.WriteString(this.Sender);
            bb.WriteInt(this.Type);
            bb.WriteBytes(this.Content);

            bb.WriteInt(Properties.Count);
            foreach (KeyValuePair <int, byte[]> pair in Properties)
            {
                bb.WriteInt(pair.Key);
                bb.WriteBytes(pair.Value);
            }
        }
Ejemplo n.º 2
0
            public long Read(long fromId, long count, List <ChatHistoryMessage> result)
            {
                if (count <= 0)
                {
                    return(0);
                }

                if (SeekDataOffset(fromId) < 0)
                {
                    return(0);
                }

                long i = 0;

                for (; i < count; ++i)
                {
                    byte[] msgSizeBytes = new byte[4];
                    int    msgSizeLen   = data.Read(msgSizeBytes, 0, msgSizeBytes.Length);
                    if (msgSizeLen == 0) // eof
                    {
                        break;
                    }
                    if (msgSizeBytes.Length != msgSizeLen)
                    {
                        throw new Exception("read size error");
                    }

                    int    msgSize      = BitConverter.ToInt32(msgSizeBytes, 0);
                    byte[] msgDataBytes = new byte[msgSize];
                    if (msgDataBytes.Length != data.Read(msgDataBytes, 0, msgDataBytes.Length))
                    {
                        throw new Exception("read data error");
                    }

                    global::Zeze.Serialize.ByteBuffer bb = global::Zeze.Serialize.ByteBuffer.Wrap(msgDataBytes);
                    ChatHistoryMessage msg = new ChatHistoryMessage();
                    msg.Decode(bb);
                    if (msg.Id != fromId + i)
                    {
                        throw new Exception("msgId error");
                    }

                    result.Add(msg);
                }

                return(i);
            }
Ejemplo n.º 3
0
        public void Decode(global::Zeze.Serialize.ByteBuffer bb)
        {
            this.Tag       = bb.ReadInt4();
            this.Id        = bb.ReadLong();
            this.TimeTicks = bb.ReadLong();
            this.Sender    = bb.ReadString();
            this.Type      = bb.ReadInt();
            this.Content   = bb.ReadBytes();

            int propertiesSize = bb.ReadInt();

            for (int i = 0; i < propertiesSize; ++i)
            {
                int    key   = bb.ReadInt();
                byte[] value = bb.ReadBytes();
                Properties.Add(key, value);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 增加消息到聊天历史中。
        /// </summary>
        /// <param name="sender">发送者</param>
        /// <param name="type">消息类型</param>
        /// <param name="content">消息内容</param>
        /// <returns></returns>
        public long AddMessage(string sender, int type, byte[] content)
        {
            if (type < 0)
            {
                throw new ArgumentException("type is reserved: " + type);
            }

            lock (this)
            {
                ChatHistoryMessage msg = new ChatHistoryMessage
                {
                    Tag       = 0,
                    Id        = LastId,
                    TimeTicks = DateTime.Now.Ticks,
                    Sender    = sender,
                    Type      = type,
                    Content   = content
                };

                if (this.SeparateContentLength > 0 && content.Length > this.SeparateContentLength)
                {
                    msg.Tag = ChatHistoryMessage.TagSeparate;
                    msg.SaveContentToFile(System.IO.Path.Combine(this.ContentHome, msg.Id.ToString()));
                    msg.Content = Array.Empty <byte>(); // 对于图片视频,这里可以考虑放一个缩小的提示性图片。
                }

                if (this.MaxSingleDataFileLength > 0 && _lastDataFile.DataFileLength > this.MaxSingleDataFileLength)
                {
                    OpenOrCreateLastDataFile(LastId);
                }

                global::Zeze.Serialize.ByteBuffer bb = global::Zeze.Serialize.ByteBuffer.Allocate(msg.SizeHint());
                bb.BeginWriteWithSize4(out var state);
                msg.Encode(bb);
                bb.EndWriteWithSize4(state);
                _lastDataFile.WriteToTail(bb.Bytes, bb.ReadIndex, bb.Size);

                return(LastId++); // 最后才真的增加,避免上面异常导致LastId已被增加。
            }
        }
Ejemplo n.º 5
0
            public void Delete(long id)
            {
                long offset = SeekDataOffset(id);

                if (offset < 0)
                {
                    return;
                }

                byte[] head    = new byte[4 + 4 + 9]; // size + tag + id
                int    headLen = data.Read(head, 0, head.Length);

                if (headLen == 0) // eof
                {
                    return;
                }

                global::Zeze.Serialize.ByteBuffer bb = global::Zeze.Serialize.ByteBuffer.Wrap(head, 0, headLen);
                int  msgsize = bb.ReadInt4();
                int  tag     = bb.ReadInt4();
                long existid = bb.ReadLong();

                if (existid != id)
                {
                    throw new Exception("msgId error"); // report or ignore
                }
                tag |= ChatHistoryMessage.TagDeleted;
                byte[] newtagBytes = BitConverter.GetBytes(tag);
                long   tagoffset   = offset + 4;

                if (tagoffset != data.Seek(tagoffset, System.IO.SeekOrigin.Begin))
                {
                    throw new Exception("seek error");
                }

                data.Write(newtagBytes, 0, 4);
            }
Ejemplo n.º 6
0
 public bool Send(global::Zeze.Serialize.ByteBuffer bb)
 {
     return(Send(bb.Bytes, bb.ReadIndex, bb.Size));
 }
Ejemplo n.º 7
0
 public override void Encode(global::Zeze.Serialize.ByteBuffer bb)
 {
     throw new System.NotImplementedException();
 }
Ejemplo n.º 8
0
Archivo: Bean.cs Proyecto: e2wugui/zeze
 public abstract void Encode(global::Zeze.Serialize.ByteBuffer bb);