private void RefreshDatabaseInfo() { m_stream.Write((byte)ServerCommand.GetAllDatabases); m_stream.Flush(); var command = (ServerResponse)m_stream.ReadUInt8(); switch (command) { case ServerResponse.UnhandledException: string exception = m_stream.ReadString(); throw new Exception("Server UnhandledExcetion: \n" + exception); case ServerResponse.ListOfDatabases: int cnt = m_stream.ReadInt32(); var dict = new Dictionary <string, DatabaseInfo>(); while (cnt > 0) { cnt--; var info = new DatabaseInfo(m_stream); dict.Add(info.DatabaseName.ToUpper(), info); } m_databaseInfos = dict; break; default: throw new Exception("Unknown server response: " + command.ToString()); } }
private void Complete() { if (!m_completed) { m_completed = true; m_onComplete(); string exception; var command = (ServerResponse)m_stream.ReadUInt8(); switch (command) { case ServerResponse.UnhandledException: exception = m_stream.ReadString(); throw new Exception("Server UnhandledExcetion: \n" + exception); case ServerResponse.ErrorWhileReading: exception = m_stream.ReadString(); throw new Exception("Server Error While Reading: \n" + exception); case ServerResponse.CanceledRead: break; case ServerResponse.ReadComplete: break; default: throw new Exception("Unknown server response: " + command.ToString()); } } }
/// <summary> /// Creates a <see cref="SnapStreamingClient"/> /// </summary> /// <param name="stream">The config to use for the client</param> /// <param name="credentials">Authenticates using the supplied user credentials.</param> /// <param name="useSsl">specifies if a ssl connection is desired.</param> protected void Initialize(Stream stream, SecureStreamClientBase credentials, bool useSsl) { if (stream == null) { throw new ArgumentNullException("stream"); } if (credentials == null) { throw new ArgumentNullException("credentials"); } m_credentials = credentials; m_rawStream = stream; m_rawStream.Write(0x2BA517361121L); m_rawStream.Write(useSsl); //UseSSL var command = (ServerResponse)m_rawStream.ReadNextByte(); switch (command) { case ServerResponse.UnknownProtocol: throw new Exception("Client and server cannot agree on a protocol, this is commonly because they are running incompatible versions."); case ServerResponse.KnownProtocol: break; default: throw new Exception("Unknown server response: " + command.ToString()); } useSsl = m_rawStream.ReadBoolean(); if (!m_credentials.TryAuthenticate(m_rawStream, useSsl, out m_secureStream)) { throw new Exception("Authentication Failed"); } m_stream = new RemoteBinaryStream(m_secureStream); command = (ServerResponse)m_stream.ReadUInt8(); switch (command) { case ServerResponse.UnhandledException: string exception = m_stream.ReadString(); throw new Exception("Server UnhandledExcetion: \n" + exception); case ServerResponse.UnknownProtocol: throw new Exception("Client and server cannot agree on a protocol, this is commonly because they are running incompatible versions."); case ServerResponse.ConnectedToRoot: break; default: throw new Exception("Unknown server response: " + command.ToString()); } RefreshDatabaseInfo(); }
/// <summary> /// This function will verify the connection, create all necessary streams, set timeouts, and catch any exceptions and terminate the connection /// </summary> /// <returns>True if successful, false if needing to exit the socket.</returns> public bool RunDatabaseLevel() { while (true) { ServerCommand command = (ServerCommand)m_stream.ReadUInt8(); switch (command) { case ServerCommand.SetEncodingMethod: try { m_encodingMethod = Library.CreateStreamEncoding <TKey, TValue>(new EncodingDefinition(m_stream)); } catch { m_stream.Write((byte)ServerResponse.UnknownEncodingMethod); m_stream.Flush(); return(false); } m_stream.Write((byte)ServerResponse.EncodingMethodAccepted); m_stream.Flush(); break; case ServerCommand.Read: if (!ProcessRead()) { return(false); } break; case ServerCommand.DisconnectDatabase: m_sortedTreeEngine.Dispose(); m_sortedTreeEngine = null; m_stream.Write((byte)ServerResponse.DatabaseDisconnected); m_stream.Flush(); return(true); case ServerCommand.Write: ProcessWrite(); break; case ServerCommand.CancelRead: break; default: m_stream.Write((byte)ServerResponse.UnknownDatabaseCommand); m_stream.Write((byte)command); m_stream.Flush(); return(false); } } }
/// <summary> /// This function will process any of the packets that come in. It will throw an error if anything happens. /// This will cause the calling function to close the connection. /// </summary> /// <remarks></remarks> private void ProcessRootLevelCommands() { m_host = SnapClient.Connect(m_server); while (true) { ServerCommand command = (ServerCommand)m_stream.ReadUInt8(); switch (command) { case ServerCommand.GetAllDatabases: var info = m_host.GetDatabaseInfo(); m_stream.Write((byte)ServerResponse.ListOfDatabases); m_stream.Write(info.Count); foreach (var i in info) { i.Save(m_stream); } m_stream.Flush(); break; case ServerCommand.ConnectToDatabase: string databaseName = m_stream.ReadString(); Guid keyTypeId = m_stream.ReadGuid(); Guid valueTypeId = m_stream.ReadGuid(); if (!m_host.Contains(databaseName)) { m_stream.Write((byte)ServerResponse.DatabaseDoesNotExist); m_stream.Write("Database Does Not Exist"); m_stream.Flush(); return; } var database = m_host.GetDatabase(databaseName); var dbinfo = database.Info; if (dbinfo.KeyTypeID != keyTypeId) { m_stream.Write((byte)ServerResponse.DatabaseKeyUnknown); m_stream.Write("Database Key Type Is Invalid"); m_stream.Flush(); return; } if (dbinfo.ValueTypeID != valueTypeId) { m_stream.Write((byte)ServerResponse.DatabaseValueUnknown); m_stream.Write("Database Value Type Is Invalid"); m_stream.Flush(); return; } var type = typeof(SnapStreamingServer); var method = type.GetMethod("ConnectToDatabase", BindingFlags.NonPublic | BindingFlags.Instance); var reflectionMethod = method.MakeGenericMethod(database.Info.KeyType, database.Info.ValueType); var success = (bool)reflectionMethod.Invoke(this, new object[] { database }); if (!success) { return; } break; case ServerCommand.Disconnect: m_stream.Write((byte)ServerResponse.GoodBye); m_stream.Write("Good bye!"); m_stream.Flush(); return; default: m_stream.Write((byte)ServerResponse.UnknownCommand); m_stream.Write((byte)command); m_stream.Flush(); return; } } }