public void TestSerializeString() { var buffer = ArrayPool <byte> .Shared.Rent(16); var count = _networkSerializer.Serialize(typeof(string), ref buffer, 4, "Hello World", ArrayPool <byte> .Shared); Assert.Equal(new ArraySegment <byte>(new byte[4]), new ArraySegment <byte>(buffer, 0, 4)); Assert.Equal(14, count); var result = _networkSerializer.Deserialize(typeof(string), buffer, 4); Assert.Equal("Hello World", (string)result); }
async void Start() { while (true) { await Task.Delay(15000); var bytes = Task.Run(() => { lock (space) return(serializer.Serialize(space)); }); Application.Current.Properties[nameof(SpaceDto)] = bytes; await Application.Current.SavePropertiesAsync(); } }
private async void HandleEventAction(NetworkEventInfo eventInfo, object parameter) { if (_isDisposed) { return; } //RETURN: //HEAD - byte - response type //HEAD - uinteger - event id //(BODY - integer - body length) //(BODY - body length - the serialized object) var dataLength = 1 /* response type */ + 4 /* event id */; if (parameter != null) { dataLength += 4 /* length */; } var takenBuffer = Cache.Pool.Rent(dataLength + EstimatedEventParameterSize + CustomOffset); var data = takenBuffer; int parameterLength; if (parameter != null) { parameterLength = _serializer.Serialize(eventInfo.EventArgsType, ref data, CustomOffset + 9, parameter, Cache.Pool); if (data != takenBuffer) { Cache.Pool.Return(takenBuffer); } data[CustomOffset] = (byte)NetworkCallResponse.TriggerEventWithParameter; BinaryUtils.WriteInt32(data, CustomOffset + 5, parameterLength); } else { parameterLength = 0; data[CustomOffset] = (byte)NetworkCallResponse.TriggerEvent; } BinaryUtils.WriteUInt32(data, CustomOffset + 1, eventInfo.EventId); Task eventLockTask; lock (_disposedLock) { if (_isDisposed) { Cache.Pool.Return(data); return; } eventLockTask = _eventLock.EnterAsync(); } //keep event order await eventLockTask; try { await OnSendData(new BufferSegment(data, CustomOffset, dataLength + parameterLength, Cache.Pool)); } finally { _eventLock.Release(); } }
private async void EventSubscriberOnEventRaised(object sender, EventProxyEventArgs eventProxyEventArgs) { if (_events.TryGetValue(eventProxyEventArgs.EventId, out var subscription)) { byte[] data = null; var length = 0; var returnBuffer = true; async Task GetData() { var dataLength = CustomOffset + 11 + (eventProxyEventArgs.TransmissionInfo != null ? EstimatedTransmissionInfoSize : 0) + (eventProxyEventArgs.EventArgs != null ? EstimatedParameterSize : 0); var takenBuffer = _bufferManager.TakeBuffer(dataLength); data = takenBuffer; var transmissionInfoLength = 0; var parameterLength = 0; if (eventProxyEventArgs.TransmissionInfo != null) { transmissionInfoLength = _serializer.Serialize(subscription.TransmissionInfoType, ref data, CustomOffset + 11, eventProxyEventArgs.TransmissionInfo); } if (eventProxyEventArgs.EventArgs != null) { parameterLength = _serializer.Serialize(subscription.EventArgsType, ref data, CustomOffset + 11 + transmissionInfoLength, eventProxyEventArgs.EventArgs); } EventResponseType responseType; if (transmissionInfoLength > 0 && parameterLength > 0) { responseType = EventResponseType.TriggerEventWithTransmissionInfoAndParameter; } else if (transmissionInfoLength == 0 && parameterLength > 0) { responseType = EventResponseType.TriggerEventWithParameter; } else if (transmissionInfoLength > 0 && parameterLength == 0) { responseType = EventResponseType.TriggerEventWithTransmissionInfo; } else { responseType = EventResponseType.TriggerEvent; } data[CustomOffset] = (byte)responseType; Buffer.BlockCopy(BitConverter.GetBytes(eventProxyEventArgs.EventId), 0, data, CustomOffset + 1, 8); if (responseType == EventResponseType.TriggerEvent) { length = CustomOffset + 9; } else { Buffer.BlockCopy(BitConverter.GetBytes((ushort)transmissionInfoLength), 0, data, CustomOffset + 9, 2); length = CustomOffset + 11 + transmissionInfoLength + parameterLength; } if (ModifyDataDelegate != null) { data = await ModifyDataDelegate(data, 0, length).ConfigureAwait(false); } if (data != takenBuffer) { _bufferManager.ReturnBuffer(takenBuffer); returnBuffer = false; } } List <IEventSubscriber> subscribers; lock (subscription.SubscriberLock) { subscribers = subscription.Subscriber.ToList(); //copy subscribers } if (subscribers.Count == 0) { return; } List <Task> runningTasks; //check permissions if (subscription.RequiredPermissions == null) { await GetData().ConfigureAwait(false); runningTasks = subscribers.Select(x => x.TriggerEvent(data, 0, length)).ToList(); } else { var subscriberPermissionsTaskDictionary = subscribers.ToDictionary( x => x.CheckPermissions(subscription.RequiredPermissions, eventProxyEventArgs.TransmissionInfo), y => y); var tasks = subscriberPermissionsTaskDictionary.Select(x => x.Key).ToList(); runningTasks = new List <Task>(); while (tasks.Count > 0) { var finishedTask = await Task.WhenAny(tasks).ConfigureAwait(false); var subscriber = subscriberPermissionsTaskDictionary[finishedTask]; subscriberPermissionsTaskDictionary.Remove(finishedTask); tasks.Remove(finishedTask); if (finishedTask.Result) { if (data == null) { await GetData().ConfigureAwait(false); } runningTasks.Add(subscriber.TriggerEvent(data, 0, length)); } } } if (runningTasks.Count > 0) { await Task.WhenAll(runningTasks).ConfigureAwait(false); if (returnBuffer) { _bufferManager.ReturnBuffer(data); } } } }