public void RegisterProcessor(PacketProcessorConfig config) { _isRegistered = true; foreach (var handler in _handlers) //Add system request handlers { config.AddProcessor(handler.Key, p => { var context = new RequestContext(p); handler.Value(context).ContinueWith(task => { if (!context.IsComplete) { if (task.IsFaulted) { var clientException = task.Exception.InnerExceptions.OfType <ClientException>().FirstOrDefault(); var msg = clientException != null ? clientException.Message : "An error occured on the server."; context.Error(s => p.Serializer().Serialize(msg, s)); } else { context.Complete(); } } }); return(true); }); } config.AddProcessor((byte)MessageIDTypes.ID_REQUEST_RESPONSE_MSG, p => { var temp = new byte[2]; p.Stream.Read(temp, 0, 2); var id = BitConverter.ToUInt16(temp, 0); Request request; if (_pendingRequests.TryRemove(id, out request)) { p.Metadata["request"] = request; request.lastRefresh = DateTime.UtcNow; request.tcs.TrySetResult(p); } else { _logger.Trace("Unknown request id."); } return(true); }); config.AddProcessor((byte)MessageIDTypes.ID_REQUEST_RESPONSE_COMPLETE, p => { var temp = new byte[2]; p.Stream.Read(temp, 0, 2); var id = BitConverter.ToUInt16(temp, 0); var hasValues = p.Stream.ReadByte() == 1; if (!hasValues) { Request request; if (this._pendingRequests.TryRemove(id, out request)) { p.Metadata["request"] = request; request.tcs.TrySetResult(null); } else { _logger.Trace("Unknown request id."); } } return(true); }); config.AddProcessor((byte)MessageIDTypes.ID_REQUEST_RESPONSE_ERROR, p => { var temp = new byte[2]; p.Stream.Read(temp, 0, 2); var id = BitConverter.ToUInt16(temp, 0); Request request; if (_pendingRequests.TryRemove(id, out request)) { p.Metadata["request"] = request; var msg = p.Serializer().Deserialize <string>(p.Stream); request.tcs.TrySetException(new ClientException(msg)); } else { _logger.Trace("Unknown request id."); } return(true); }); }
public void RegisterProcessor(PacketProcessorConfig config) { config.AddCatchAllProcessor(Handler); }