Пример #1
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);
            }
Пример #2
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已被增加。
            }
        }
Пример #3
0
 public byte[] LoadContentFromFile(long id)
 {
     return(ChatHistoryMessage.LoadContentFromFile(System.IO.Path.Combine(this.ContentHome, id.ToString())));
 }