private void PrimeConnection() { LOG.InfoFormat("Socket connection established to {0}, initiating session", client.Client.RemoteEndPoint); lastConnectIndex = currentConnectIndex; ConnectRequest conReq = new ConnectRequest(0, lastZxid, Convert.ToInt32(conn.SessionTimeout.TotalMilliseconds), conn.SessionId, conn.SessionPassword); lock (outgoingQueue) { if (!ClientConnection.disableAutoWatchReset && (!zooKeeper.DataWatches.IsEmpty() || !zooKeeper.ExistWatches.IsEmpty() || !zooKeeper.ChildWatches.IsEmpty())) { var sw = new SetWatches(lastZxid, zooKeeper.DataWatches, zooKeeper.ExistWatches, zooKeeper.ChildWatches); var h = new RequestHeader(); h.Type = (int)OpCode.SetWatches; h.Xid = -8; Packet packet = new Packet(h, new ReplyHeader(), sw, null, null, null, null, null); outgoingQueue.AddFirst(packet); } foreach (ClientConnection.AuthData id in conn.authInfo) { outgoingQueue.AddFirst( new Packet(new RequestHeader(-4, (int)OpCode.Auth), null, new AuthPacket(0, id.Scheme, id.GetData()), null, null, null, null, null)); } outgoingQueue.AddFirst(new Packet(null, null, conReq, null, null, null, null, null)); } if (LOG.IsDebugEnabled) { LOG.DebugFormat("Session establishment request sent on {0}", client.Client.RemoteEndPoint); } }
internal void primeConnection() { LOG.info("Socket connection established to " + clientCnxnSocket.getRemoteSocketAddress() + ", initiating session"); isFirstConnect = false; long sessId = (seenRwServerBefore.Value) ? sessionId : 0; ConnectRequest conReq = new ConnectRequest(0, lastZxid.Value, sessionTimeout, sessId, sessionPasswd); lock (outgoingQueue) { // We add backwards since we are pushing into the front // Only send if there's a pending watch // TODO: here we have the only remaining use of zooKeeper in // this class. It's to be eliminated! if (!disableAutoWatchReset) { List <string> dataWatches = zooKeeper.getDataWatches(); List <string> existWatches = zooKeeper.getExistWatches(); List <string> childWatches = zooKeeper.getChildWatches(); if (dataWatches.Count > 0 || existWatches.Count > 0 || childWatches.Count > 0) { SetWatches sw = new SetWatches(lastZxid.Value, prependChroot(dataWatches), prependChroot(existWatches), prependChroot(childWatches)); RequestHeader h = new RequestHeader(); h.set_Type((int)ZooDefs.OpCode.setWatches); h.setXid(-8); Packet packet = new Packet(h, new ReplyHeader(), sw, null, null); outgoingQueue.AddFirst(packet); } } foreach (AuthData id in authInfo) { outgoingQueue.AddFirst(new Packet(new RequestHeader(-4, (int)ZooDefs.OpCode.auth), null, new AuthPacket(0, id.scheme, id.data), null, null)); } outgoingQueue.AddFirst(new Packet(null, null, conReq, null, null, readOnly)); } clientCnxnSocket.enableReadWriteOnly(); if (LOG.isDebugEnabled()) { LOG.debug("Session establishment request sent on " + clientCnxnSocket.getRemoteSocketAddress()); } }
private void PrimeConnection(TcpClient client) { LOG.Info(string.Format("Socket connection established to {0}, initiating session", client.Client.RemoteEndPoint)); ConnectRequest conReq = new ConnectRequest(0, lastZxid, Convert.ToInt32(conn.SessionTimeout.TotalMilliseconds), conn.SessionId, conn.SessionPassword); byte[] buffer; using (MemoryStream ms = new MemoryStream()) using (EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Big, ms, Encoding.UTF8)) { BinaryOutputArchive boa = BinaryOutputArchive.getArchive(writer); boa.WriteInt(-1, "len"); conReq.Serialize(boa, "connect"); ms.Position = 0; writer.Write(ms.ToArray().Length - 4); buffer = ms.ToArray(); } lock (outgoingQueueLock) { if (!ClientConnection.disableAutoWatchReset && (!zooKeeper.DataWatches.IsEmpty() || !zooKeeper.ExistWatches.IsEmpty() || !zooKeeper.ChildWatches.IsEmpty())) { var sw = new SetWatches(lastZxid, zooKeeper.DataWatches, zooKeeper.ExistWatches, zooKeeper.ChildWatches); var h = new RequestHeader(); h.Type = (int)OpCode.SetWatches; h.Xid = -8; Packet packet = new Packet(h, new ReplyHeader(), sw, null, null, null, null, null); outgoingQueue.AddFirst(packet); } foreach (ClientConnection.AuthData id in conn.authInfo) { outgoingQueue.AddFirst(new Packet(new RequestHeader(-4, (int)OpCode.Auth), null, new AuthPacket(0, id.scheme, id.data), null, null, null, null, null)); } outgoingQueue.AddFirst((new Packet(null, null, null, null, buffer, null, null, null))); } lock (this) { EnableWrite(); } if (LOG.IsDebugEnabled) { LOG.Debug("Session establishment request sent on " + client.Client.RemoteEndPoint); } }
private void PrimeConnection() { LOG.InfoFormat("Socket connection established to {0}, initiating session", client.Client.RemoteEndPoint); ConnectRequest conReq = new ConnectRequest(0, lastZxid, Convert.ToInt32(conn.SessionTimeout.TotalMilliseconds), conn.SessionId, conn.SessionPassword); Packet conPacket = new Packet(null, null, conReq, null, null, null, null, null); if (conn.saslClient != null) { lock (outgoingQueue) { // SASL negociation is synchronous, and must complete before we send the (non-SASL) auth data and // watches. We explicitly drive the send/receive as the queue processing loop is not active yet. // First, push the ConnectRequest down the pipe. DoSend(conPacket); conPacket = null; byte[] token = conn.saslClient.Start((IPEndPoint)client.Client.LocalEndPoint, (IPEndPoint)client.Client.RemoteEndPoint); try { bool lastPacket = false; do { RequestHeader h = new RequestHeader(); ReplyHeader r = new ReplyHeader(); h.Type = (int)OpCode.SASL; GetSASLRequest request = new GetSASLRequest(token != null ? token : new byte[0]); SetSASLResponse response = new SetSASLResponse(); Packet p = new Packet(h, r, request, response, null, null, null, null); // Push the packet. DoSend(p); // Synchronously wait for the response. if (!p.Task.Wait(conn.ConnectionTimeout)) { throw new TimeoutException($"The request {request} timed out while waiting for a response from the server."); } if (r.Err != 0) { throw KeeperException.Create((KeeperException.Code)Enum.ToObject(typeof(KeeperException.Code), r.Err)); } if (lastPacket) { break; } // SASL round. token = conn.saslClient.EvaluateChallenge(response.Token); if (conn.saslClient.IsCompleted) { if (conn.saslClient.HasLastPacket) { lastPacket = true; } else { break; } } } // We must have received a 'ConnectResponse' by // now, as we have waited for 1+ packets. while (zooKeeper.State.IsConnected()); } finally { conn.saslClient.Finish(); } } if (zooKeeper.State.IsConnected()) { conn.consumer.QueueEvent(new WatchedEvent(KeeperState.SaslAuthenticated, EventType.None, null)); } } bool hasOutgoingRequests; lock (outgoingQueue) { if (!ClientConnection.DisableAutoWatchReset && (!zooKeeper.DataWatches.IsEmpty() || !zooKeeper.ExistWatches.IsEmpty() || !zooKeeper.ChildWatches.IsEmpty())) { var sw = new SetWatches(lastZxid, prependChroot(zooKeeper.DataWatches), prependChroot(zooKeeper.ExistWatches), prependChroot(zooKeeper.ChildWatches)); var h = new RequestHeader(); h.Type = (int)OpCode.SetWatches; h.Xid = -8; Packet packet = new Packet(h, new ReplyHeader(), sw, null, null, null, null, null); //outgoingQueue.AddFirst(packet); addPacketFirst(packet); } foreach (ClientConnection.AuthData id in conn.authInfo) { addPacketFirst( new Packet(new RequestHeader(-4, (int)OpCode.Auth), null, new AuthPacket(0, id.Scheme, id.GetData()), null, null, null, null, null)); } // The ConnectRequest packet has already been sent in the SASL case. if (conPacket != null) { addPacketFirst(conPacket); conPacket = null; } hasOutgoingRequests = !outgoingQueue.IsEmpty(); } if (hasOutgoingRequests) { packetAre.Set(); } if (LOG.IsDebugEnabled) { LOG.DebugFormat("Session establishment request sent on {0}", client.Client.RemoteEndPoint); } }
internal void primeConnection() { LOG.info("Socket connection established to " + clientCnxnSocket.getRemoteSocketAddress() + ", initiating session"); isFirstConnect = false; long sessId = (seenRwServerBefore.Value) ? sessionId : 0; ConnectRequest conReq = new ConnectRequest(0, lastZxid.Value, sessionTimeout, sessId, sessionPasswd); lock (outgoingQueue) { // We add backwards since we are pushing into the front // Only send if there's a pending watch // TODO: here we have the only remaining use of zooKeeper in // this class. It's to be eliminated! List <string> dataWatches = zooKeeper.getDataWatches(); List <string> existWatches = zooKeeper.getExistWatches(); List <string> childWatches = zooKeeper.getChildWatches(); long setWatchesLastZxid = lastZxid.Value; bool done = false; if (dataWatches.Count > 0 || existWatches.Count > 0 || childWatches.Count > 0) { using (var dataWatchesIter = prependChroot(dataWatches).GetEnumerator()) using (var existWatchesIter = prependChroot(existWatches).GetEnumerator()) using (var childWatchesIter = prependChroot(childWatches).GetEnumerator()) while (!done) { var dataWatchesBatch = new List <string>(); var existWatchesBatch = new List <string>(); var childWatchesBatch = new List <string>(); int batchLength = 0; // Note, we may exceed our max length by a bit when we add the last // watch in the batch. This isn't ideal, but it makes the code simpler. while (batchLength < SET_WATCHES_MAX_LENGTH) { string watch; if (dataWatchesIter.MoveNext()) { watch = dataWatchesIter.Current; dataWatchesBatch.Add(watch); } else if (existWatchesIter.MoveNext()) { watch = existWatchesIter.Current; existWatchesBatch.Add(watch); } else if (childWatchesIter.MoveNext()) { watch = childWatchesIter.Current; childWatchesBatch.Add(watch); } else { done = true; break; } batchLength += watch.Length; } SetWatches sw = new SetWatches(setWatchesLastZxid, dataWatchesBatch, existWatchesBatch, childWatchesBatch); RequestHeader h = new RequestHeader(); h.set_Type((int)ZooDefs.OpCode.setWatches); h.setXid(-8); Packet packet = new Packet(h, new ReplyHeader(), sw, null, null); outgoingQueue.AddFirst(packet); } } foreach (AuthData id in authInfo) { outgoingQueue.AddFirst(new Packet(new RequestHeader(-4, (int)ZooDefs.OpCode.auth), null, new AuthPacket(0, id.scheme, id.data), null, null)); } outgoingQueue.AddFirst(new Packet(null, null, conReq, null, null, readOnly)); } clientCnxnSocket.enableReadWriteOnly(); if (LOG.isDebugEnabled()) { LOG.debug("Session establishment request sent on " + clientCnxnSocket.getRemoteSocketAddress()); } }