/// <summary> /// Callback method to process received packet from the secure transport connection /// </summary> /// <param name="packet">Received data packet</param> /// <returns>Async task that resolves to indicate the completion of packet processing</returns> public Task OnPacketReceived(byte[] packet) { var connection = this.Connection; var timer = Stopwatch.StartNew(); ProtocolRequestCall call = this.protocol.DeserializeRequest(packet, packet.Length, connection.ProtocolVersion, this.sessionState); RequestResponse response = null; try { if (call.Request != null) { string sState = this.sessionState.ToString(); ZooKeeperServerEventSource.Log.ProcessRequest(this.Id, call.CallId, (int)call.Request.RequestType, call.Request.Path, packet.Length, connection.ProtocolVersion, sState); response = this.ProcessRequest(call.Request, call.ProtocolRequest as IZooKeeperRequest); ZooKeeperServerEventSource.Log.ProcessRequestSucceeded(this.Id, call.CallId, (int)call.Request.RequestType, call.Request.Path, packet.Length, connection.ProtocolVersion, sState); response.CallId = call.CallId; } else { if (call.ProtocolRequest is ZkprProtocolMessages.Ping) { ZkprProtocolMessages.Ping pingRequest = call.ProtocolRequest as ZkprProtocolMessages.Ping; response = new RequestResponse(); response.CallId = (ulong)pingRequest.Xid; response.ResultCode = (int)RingMasterException.Code.Ok; } } } catch (Exception ex) { ZooKeeperServerEventSource.Log.ProcessRequestFailed(this.Id, call == null ? ulong.MaxValue : call.CallId, ex.ToString()); response = new RequestResponse(); response.CallId = call.CallId; response.ResultCode = (int)RingMasterException.Code.Systemerror; } if (response != null) { byte[] responsePacket = this.protocol.SerializeResponse(response, connection.ProtocolVersion, call.ProtocolRequest as IZooKeeperRequest); if (responsePacket != null) { connection.Send(responsePacket); } else { ZooKeeperServerEventSource.Log.NullResponsePacket( response.CallId, call.ProtocolRequest == null ? "null" : (call.ProtocolRequest as IZooKeeperRequest).RequestType.ToString(), call.Request == null ? "null" : call.Request.RequestType.ToString()); } } timer.Stop(); ZooKeeperServerEventSource.Log.ProcessRequestCompleted(this.Id, call == null ? ulong.MaxValue : call.CallId, timer.ElapsedMilliseconds); this.instrumentation?.OnRequestCompleted(call.Request == null ? RingMasterRequestType.None : call.Request.RequestType, timer.Elapsed); return(Task.FromResult(0)); }
/// <summary> /// Deserialize a <see cref="RequestCall"/>. /// </summary> /// <param name="serializedRequest">Serialized representation of the request</param> /// <param name="serializedRequestLength">Length of serialized request</param> /// <param name="version">Serialization protocol version to use</param> /// <param name="sessionState">The Session State</param> /// <returns>The deserialized <see cref="RequestCall"/></returns> public ProtocolRequestCall DeserializeRequest(byte[] serializedRequest, int serializedRequestLength, uint version, ISessionState sessionState) { using (var deserializer = new ZkprDeserializer(serializedRequest, serializedRequestLength, version)) { ZkprPerSessionState zkprSessionState = sessionState as ZkprPerSessionState; ProtocolRequestCall theCall = deserializer.DeserializeRequest(zkprSessionState); return(theCall); } }