public virtual unsafe void Send(OpAction <T> item) { ContentAction action = new ContentAction(); action.Type = item.Type; if (item.Data != null) { action.KeyValue = _propertyInfo.GetValue(item.Data).ToString(); if (item.Type == ActionType.Add || item.Type == ActionType.Update) { action.Data = item.Data.ToJsonString(); } } var bs = Encoding.UTF8.GetBytes(action.ToJsonString()); var sendData = new byte[bs.Length + 4]; fixed(byte *ptrBs = bs) { Marshal.Copy(new IntPtr(ptrBs), sendData, 4, bs.Length); int len = bs.Length; byte *ptr = (byte *)&len; Marshal.Copy(new IntPtr(ptr), sendData, 0, 4); } _stream.Write(sendData); _stream.ReadBoolean(); }
/// <summary> /// 检查是否已经全部保存 /// </summary> public virtual void CheckAllSaved() { ContentAction action = new ContentAction(); action.Type = ActionType.CheckSaved; var bs = Encoding.UTF8.GetBytes(action.ToJsonString()); _stream.Write(bs.Length); _stream.Write(bs); try { _stream.ReadBoolean(); } catch { } }
void runForSend() { var buffer = new List <byte>(20480); while (!_disposed || _queue.Count > 0) { _event.WaitOne(); _event.Reset(); while (_queue.TryDequeue(out OpAction <T> item)) { while (!_disposed) { try { ContentAction action = new ContentAction(); action.Type = item.Type; if (item.Data != null) { action.KeyValue = _propertyInfo.GetValue(item.Data).ToString(); action.Data = item.Data.ToJsonString(); } var bs = Encoding.UTF8.GetBytes(action.ToJsonString()); var lenbs = BitConverter.GetBytes(bs.Length); buffer.AddRange(lenbs); buffer.AddRange(bs); if (buffer.Count > 10240) { bs = buffer.ToArray(); buffer.Clear(); _stream.Write(bs); } if (item.CallBack != null) { item.CallBack(); } if (item.Type == ActionType.CheckSaved) { if (buffer.Count > 0) { bs = buffer.ToArray(); buffer.Clear(); _stream.Write(bs); } try { _stream.ReadBoolean(); } catch { } _stream.Dispose(); return; } break; } catch { _stream.Dispose(); try { init(); } catch { _stream = null; } } } } } _stream.Dispose(); }