internal Packet(RequestHeader header, ReplyHeader replyHeader, IRecord request, IRecord response, byte[] data, ZooKeeper.WatchRegistration watchRegistration, string serverPath, string clientPath) { this.header = header; this.replyHeader = replyHeader; this.request = request; this.response = response; this.serverPath = serverPath; this.clientPath = clientPath; if (data != null) { this.data = data; } else { try { MemoryStream ms = new MemoryStream(); using (EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Big, ms, Encoding.UTF8)) { BinaryOutputArchive boa = BinaryOutputArchive.getArchive(writer); boa.WriteInt(-1, "len"); // We'll fill this in later if (header != null) { header.Serialize(boa, "header"); } if (request != null) { request.Serialize(boa, "request"); } ms.Position = 0; int len = Convert.ToInt32(ms.Length); // now we have the real length writer.Write(len - 4); // update the length info this.data = ms.ToArray(); } } #if !NET_CORE catch (IOException e) { LOG.Warn("Ignoring unexpected exception", e); } #endif #if NET_CORE catch (Exception e) { } #endif } this.watchRegistration = watchRegistration; }
public Packet QueuePacket(RequestHeader h, ReplyHeader r, IRecord request, IRecord response, string clientPath, string serverPath, ZooKeeper.WatchRegistration watchRegistration, object callback, object ctx) { return(producer.QueuePacket(h, r, request, response, clientPath, serverPath, watchRegistration)); }
public ReplyHeader SubmitRequest(RequestHeader h, IRecord request, IRecord response, ZooKeeper.WatchRegistration watchRegistration) { ReplyHeader r = new ReplyHeader(); Packet p = QueuePacket(h, r, request, response, null, null, watchRegistration, null, null); if (!p.WaitUntilFinishedSlim(SessionTimeout)) { throw new TimeoutException(new StringBuilder("The request ").Append(request).Append(" timed out while waiting for a response from the server.").ToString()); } return(r); }
public Packet QueuePacket(RequestHeader h, ReplyHeader r, IRecord request, IRecord response, string clientPath, string serverPath, ZooKeeper.WatchRegistration watchRegistration) { if (h.Type != (int)OpCode.Ping && h.Type != (int)OpCode.Auth) { h.Xid = Xid; } Packet p = new Packet(h, r, request, response, null, watchRegistration, clientPath, serverPath); if (!zooKeeper.State.IsAlive() || closing || Interlocked.CompareExchange(ref isDisposed, 0, 0) == 1) { if (LOG.IsDebugEnabled) { LOG.DebugFormat("Connection closing. Sending ConLossPacket. IsAlive: {0}, closing: {1}", zooKeeper.State.IsAlive(), closing); } ConLossPacket(p); } else { if (h.Type == (int)OpCode.CloseSession) { closing = true; } // enqueue the packet when zookeeper is connected addPacketLast(p); } return(p); }
public ReplyHeader SubmitRequest(RequestHeader h, IRecord request, IRecord response, ZooKeeper.WatchRegistration watchRegistration) { ReplyHeader r = new ReplyHeader(); Packet p = QueuePacket(h, r, request, response, null, null, watchRegistration, null, null); SpinWait spin = new SpinWait(); DateTime start = DateTime.Now; // now wait for reply for the packet while (!p.Finished) { spin.SpinOnce(); if (spin.Count > ClientConnection.maxSpin) { if (DateTime.Now.Subtract(start) > SessionTimeout) { throw new TimeoutException(new StringBuilder("The request ").Append(request).Append(" timed out while waiting for a response from the server.").ToString()); } spin.Reset(); } } return(r); }
public Packet QueuePacket(RequestHeader h, ReplyHeader r, IRecord request, IRecord response, string clientPath, string serverPath, ZooKeeper.WatchRegistration watchRegistration) { lock (outgoingQueueLock) { //lock here for XID? if (h.Type != (int)OpCode.Ping && h.Type != (int)OpCode.Auth) { h.Xid = Xid; } Packet p = new Packet(h, r, request, response, null, watchRegistration, clientPath, serverPath); p.clientPath = clientPath; p.serverPath = serverPath; if (!zooKeeper.State.IsAlive()) { ConLossPacket(p); } else { outgoingQueue.AddLast(p); Monitor.PulseAll(outgoingQueueLock); } return(p); } }
public async Task <ReplyHeader> SubmitRequestAsync(RequestHeader h, IRecord request, IRecord response, ZooKeeper.WatchRegistration watchRegistration) { ReplyHeader r = new ReplyHeader(); Packet p = QueuePacket(h, r, request, response, null, null, watchRegistration, null, null); CancellationTokenSource cts = new CancellationTokenSource(); if (await Task.WhenAny(p.Task, Task.Delay(SessionTimeout, cts.Token)).ConfigureAwait(false) != p.Task) { throw new TimeoutException($"The request {request} timed out while waiting for a response from the server."); } cts.Cancel(); // we haven't timed out - cancel the timeout task await p.Task; // forces exceptions to be thrown correctly return(r); }
public ReplyHeader SubmitRequest(RequestHeader h, IRecord request, IRecord response, ZooKeeper.WatchRegistration watchRegistration) { ReplyHeader r = new ReplyHeader(); Packet p = QueuePacket(h, r, request, response, null, null, watchRegistration, null, null); if (!p.Task.Wait(SessionTimeout)) { throw new TimeoutException($"The request {request} timed out while waiting for a response from the server."); } return(r); }
public ReplyHeader SubmitRequest(RequestHeader h, IRecord request, IRecord response, ZooKeeper.WatchRegistration watchRegistration) { ReplyHeader r = new ReplyHeader(); Packet p = QueuePacket(h, r, request, response, null, null, watchRegistration, null, null); lock (p) { while (!p.Finished) { if (!Monitor.Wait(p, SessionTimeout)) { throw new TimeoutException(string.Format("The request {0} timed out while waiting for a resposne from the server.", request)); } } } return(r); }