private void SerializeResponseMulti(RequestResponse response, IZooKeeperRequest request) { this.SerializeReplyHeader(response.Stat == null ? 0L : response.Stat.Mzxid, response.ResultCode, request.Xid); List <OpResult> results = response.Content as List <OpResult>; foreach (OpResult or in results) { this.SerializeMultiHeader(this.TranslateRmOpResultTypeToZkOpType(or.ResultType), false, this.TranslateRingMasterResultCodeToZkprErrorCode((int)or.ErrCode)); RequestResponse rr = OpResult.ToResponse(or); switch (or.ResultType) { case OpCode.Create: this.binaryWriter.WriteString32BitPrefixLengthBE(rr.Content as string); break; case OpCode.Delete: break; case OpCode.SetData: this.SerializeStat(rr.Stat); break; case OpCode.Check: break; } } this.SerializeMultiHeader(-1, true, -1); }
/// <summary> /// Deserialize a <see cref="RequestCall"/>. /// </summary> /// <param name="sessionState">PerSession state</param> /// <returns>Deserialized <see cref="RequestCall"/></returns> public ProtocolRequestCall DeserializeRequest(ZkprPerSessionState sessionState) { int callId; ZooKeeperRequestType type = ZooKeeperRequestType.Notification; if (sessionState == null) { throw new ArgumentNullException("sessionState"); } if (!sessionState.ConnectRecieved) { type = ZooKeeperRequestType.CreateSession; callId = 0; } else { callId = this.binaryReader.ReadInt32BE(); type = (ZooKeeperRequestType)this.binaryReader.ReadInt32BE(); } IRingMasterRequest ringMasterRequest; IZooKeeperRequest zkprRequest = this.DeserializeZooKeeperRequest(callId, type, sessionState, out ringMasterRequest); return(new ProtocolRequestCall() { CallId = (ulong)callId, Request = ringMasterRequest, ProtocolRequest = zkprRequest, }); }
private void SerializeResponseGetChildren(RequestResponse response, IZooKeeperRequest request) { this.SerializeReplyHeader(response.Stat == null ? 0L : response.Stat.Mzxid, response.ResultCode, request.Xid); if (response.Content != null) { IEnumerable <string> children = response.Content as IEnumerable <string>; if (children.Any <string>()) { this.binaryWriter.WriteBE((int)children.Count <string>()); foreach (string c in children) { this.binaryWriter.WriteString32BitPrefixLengthBE(c); } } else { this.binaryWriter.WriteBE((int)-1); } if (request.RequestType == ZooKeeperRequestType.GetChildren2) { this.SerializeStat(response.Stat); } } else { this.binaryWriter.WriteString32BitPrefixLengthBE(string.Empty); } }
private void SerializeResponseCreateSession(RequestResponse response, IZooKeeperRequest request) { if (response.ResultCode == (int)Microsoft.Azure.Networking.Infrastructure.RingMaster.Data.RingMasterException.Code.Ok) { int protocolVersion = ((ZkprProtocolMessages.CreateSession)request).Version; this.binaryWriter.WriteBE(protocolVersion); this.binaryWriter.WriteBE(((ZkprProtocolMessages.CreateSession)request).Timeout); ulong sessionId; sessionId = (ulong)((ZkprProtocolMessages.CreateSession)request).SessionId; if (sessionId == 0) { sessionId = Convert.ToUInt64(((string[])response.Content)[0]); } this.binaryWriter.WriteBE(sessionId); string sPassword; if (((ZkprProtocolMessages.CreateSession)request).IsNullPassword) { sPassword = ((string[])response.Content)[1]; } else { sPassword = ((ZkprProtocolMessages.CreateSession)request).Password; } this.binaryWriter.WriteString32BitPrefixLengthBE(sPassword); } else { // Dont know what to do in case of error return error } }
/// <summary> /// Serialize a <see cref="RequestResponse"/>. /// </summary> /// <param name="response">Response to serialize</param> /// <param name="version">Serialization protocol version to use</param> /// <param name="zkprRequest">The Zookeeper request</param> /// <returns>Serialized representation of the given response</returns> public byte[] SerializeResponse(RequestResponse response, uint version, IZooKeeperRequest zkprRequest) { using (var serializer = new ZkprSerializer(version)) { serializer.SerializeResponse(response, zkprRequest); return(serializer.GetBytes()); } }
private void SerializeResponseExists(RequestResponse response, IZooKeeperRequest request) { Stat stat = (Stat)response.Content; this.SerializeReplyHeader(stat == null ? 0L : stat.Mzxid, response.ResultCode, request.Xid); this.SerializeStat(stat); }
private void SerializeResponsePing(RequestResponse response, IZooKeeperRequest request) { Stat stat; stat = new Stat() { Czxid = -1 }; this.SerializeReplyHeader(stat.Mzxid, response.ResultCode, request.Xid); }
private void SerializeResponseCreate(RequestResponse response, IZooKeeperRequest request) { this.SerializeReplyHeader(response.Stat == null ? 0L : response.Stat.Mzxid, response.ResultCode, request.Xid); if (((response.ResultCode == (int)RingMasterException.Code.Ok) || (response.ResultCode == (int)RingMasterException.Code.Nodeexists)) && (response.Content != null)) { this.binaryWriter.WriteString32BitPrefixLengthBE(response.Content as string); if (request.RequestType == ZooKeeperRequestType.Create2) { this.SerializeStat(response.Stat); } } else { this.binaryWriter.WriteString32BitPrefixLengthBE(string.Empty); } }
private void SerializeResponseGetACL(RequestResponse response, IZooKeeperRequest request) { this.SerializeReplyHeader(response.Stat == null ? 0L : response.Stat.Mzxid, response.ResultCode, request.Xid); IEnumerable <Acl> theAcls = response.Content as IEnumerable <Acl>; IReadOnlyList <Acl> zkprAcls = this.TranslateRingMasterAclListToZkprAclList(theAcls); if (zkprAcls == null) { zkprAcls = new List <Acl>(); } // Write the count of Acls this.binaryWriter.WriteBE((int)zkprAcls.Count); foreach (Acl a in zkprAcls) { this.binaryWriter.WriteBE((int)a.Perms); this.binaryWriter.WriteString32BitPrefixLengthBE(a.Id.Scheme); this.binaryWriter.WriteString32BitPrefixLengthBE(a.Id.Identifier); } this.SerializeStat(response.Stat); }
private RequestResponse ProcessRequest(IRingMasterRequest request, IZooKeeperRequest zkprRequest) { if (request == null) { throw new ArgumentNullException(nameof(request)); } if (zkprRequest == null) { throw new ArgumentNullException(nameof(zkprRequest)); } switch (request.RequestType) { case RingMasterRequestType.Init: { var initRequest = (Requests.RequestInit)request; RequestResponse initResponse = null; if (this.requestHandler == null) { this.requestHandler = this.onInitSession(initRequest); if (((zkprRequest as ZkprProtocolMessages.CreateSession).SessionId != 0) || (zkprRequest as ZkprProtocolMessages.CreateSession).IsNullPassword == false) { initResponse = new RequestResponse { ResultCode = (int)RingMasterException.Code.Authfailed, Content = new string[] { "0", string.Empty }, }; } } if (initResponse == null) { initResponse = (this.requestHandler as CoreRequestHandler).InitResponse; } ZooKeeperServerEventSource.Log.ProcessSessionInit(this.Id, initResponse.ResultCode); return(initResponse); } case RingMasterRequestType.GetData: { var getDataRequest = (Requests.RequestGetData)request; getDataRequest.Watcher = this.MakeWatcher(getDataRequest.Watcher); break; } case RingMasterRequestType.GetChildren: { var getChildrenRequest = (Requests.RequestGetChildren)request; getChildrenRequest.Watcher = this.MakeWatcher(getChildrenRequest.Watcher); break; } case RingMasterRequestType.Exists: { var existsRequest = (Requests.RequestExists)request; existsRequest.Watcher = this.MakeWatcher(existsRequest.Watcher); break; } } if (this.server.Redirect != null) { RedirectSuggested redirect = this.server.Redirect(); ZooKeeperServerEventSource.Log.RedirectionSuggested(this.Id, redirect?.SuggestedConnectionString); return(new RequestResponse() { ResponsePath = request.Path, ResultCode = (int)RingMasterException.Code.Sessionmoved, Stat = default(Stat), Content = redirect, }); } if (this.requestHandler != null) { using (var completed = new AutoResetEvent(false)) { RequestResponse response = null; this.requestHandler.RequestOverlapped(request, (r, e) => { response = r; completed.Set(); }); completed.WaitOne(); return(response); } } throw new InvalidOperationException("Session has not been initialized"); }
/// <summary> /// Serialize a <see cref="RequestResponse"/>. /// </summary> /// <param name="response">Response to serialize</param> /// <param name="zkprRequest">The Per call zookeeper request</param> public void SerializeResponse(RequestResponse response, IZooKeeperRequest zkprRequest) { if (response == null) { throw new ArgumentNullException("response"); } if (zkprRequest == null) { throw new ArgumentNullException("zkprRequest"); } switch (zkprRequest.RequestType) { case ZooKeeperRequestType.CreateSession: this.SerializeResponseCreateSession(response, zkprRequest); break; case ZooKeeperRequestType.GetChildren: case ZooKeeperRequestType.GetChildren2: this.SerializeResponseGetChildren(response, zkprRequest); break; case ZooKeeperRequestType.Create: case ZooKeeperRequestType.Create2: this.SerializeResponseCreate(response, zkprRequest); break; case ZooKeeperRequestType.Delete: this.SerializeResponseDelete(response, zkprRequest); break; case ZooKeeperRequestType.Exists: this.SerializeResponseExists(response, zkprRequest); break; case ZooKeeperRequestType.GetData: this.SerializeResponseGetData(response, zkprRequest); break; case ZooKeeperRequestType.SetData: this.SerializeResponseSetData(response, zkprRequest); break; case ZooKeeperRequestType.GetACL: this.SerializeResponseGetACL(response, zkprRequest); break; case ZooKeeperRequestType.SetACL: this.SerializeResponseSetACL(response, zkprRequest); break; case ZooKeeperRequestType.Multi: this.SerializeResponseMulti(response, zkprRequest); break; case ZooKeeperRequestType.Auth: this.SerializeResponseAuth(response, zkprRequest); break; case ZooKeeperRequestType.Sync: break; case ZooKeeperRequestType.Check: // Apparently this is only supported in a multi? break; case ZooKeeperRequestType.Reconfig: break; case ZooKeeperRequestType.CheckWatches: break; case ZooKeeperRequestType.RemoveWatches: break; case ZooKeeperRequestType.CreateContainer: break; case ZooKeeperRequestType.DeleteContainer: break; case ZooKeeperRequestType.SetWatches: break; case ZooKeeperRequestType.Sasl: break; case ZooKeeperRequestType.Ping: this.SerializeResponsePing(response, zkprRequest); break; case ZooKeeperRequestType.Error: case ZooKeeperRequestType.CloseSession: case ZooKeeperRequestType.Notification: default: break; } return; }
private void SerializeResponseSetData(RequestResponse response, IZooKeeperRequest request) { this.SerializeReplyHeader(response.Stat == null ? 0L : response.Stat.Mzxid, response.ResultCode, request.Xid); this.SerializeStat(response.Stat); }
private void SerializeResponseGetData(RequestResponse response, IZooKeeperRequest request) { this.SerializeReplyHeader(response.Stat == null ? 0L : response.Stat.Mzxid, response.ResultCode, request.Xid); this.binaryWriter.WriteByteArray32BitPrefixLengthBE((byte[])response.Content); this.SerializeStat(response.Stat); }