protected void InternalUpdate() { do { while (offset < data.Length) { if (connection.Available <= 0) { // we will return! return; } SocketError sockError; int rc = connection.Client.Receive(data, offset, data.Length - offset, SocketFlags.None, out sockError); if (sockError == SocketError.WouldBlock) { return; } if (rc == 0) { throw new CannotConnectException("unexpected EOF"); } if (sockError != SocketError.Success) { throw new CannotConnectException(sockError.ToString()); } offset += rc; } switch (state) { case NIPState.TransportProtocol: if (!ByteUtils.Compare(data, 0, acceptor.ProtocolDescriptor, 0, 4)) { throw new CannotConnectException("Unknown protocol version: " + ByteUtils.DumpBytes(data, 0, 4) + " [" + ByteUtils.AsPrintable(data, 0, 4) + "]"); } state = NIPState.DictionarySize; data = new byte[1]; offset = 0; break; case NIPState.DictionarySize: { MemoryStream ms = new MemoryStream(data); try { uint count = ByteUtils.DecodeLength(ms); state = NIPState.DictionaryContent; data = new byte[count]; offset = 0; } catch (InvalidDataException) { // we keep reading until we have an encoded length byte[] newData = new byte[data.Length + 1]; Buffer.BlockCopy(data, 0, newData, 0, data.Length); data = newData; // and get that next byte! } } break; case NIPState.DictionaryContent: { MemoryStream ms = new MemoryStream(data); Dictionary<string, string> dict = ByteUtils.DecodeDictionary(ms); acceptor.RemoveAndNotify(this, new TcpTransport(connection), dict); } return; } } while (connection.Available > 0); }
internal NegotiationInProgress(TcpAcceptor acc, TcpClient c) { log = LogManager.GetLogger(GetType()); acceptor = acc; connection = c; state = NIPState.TransportProtocol; data = new byte[4]; // protocol descriptor is 4 bytes offset = 0; try { connection.NoDelay = true; } catch (Exception) {/*ignore*/} }