public void Connect() { try { if (this.Connection.Connect()) { ConnectionThread.Start(); } else { throw new Exception("Attempt failed"); } } catch { throw new Exception("Connection to MSA failed"); } }
/// <inheritdoc/> public bool Login(string defaultSchema, string user, string password, DatabaseEventCallback callback) { try { // Do some handshaking, MemoryStream bout = new MemoryStream(); BinaryWriter output = new BinaryWriter(bout, Encoding.ASCII); // Write output the magic number output.Write(0x0ced007); // Write output the driver version Version clientVersion = ClientVersion; output.Write(clientVersion.Major); output.Write(clientVersion.Minor); output.Write(initialDatabase); byte[] arr = bout.ToArray(); SendCommand(arr, 0, arr.Length); byte[] response = ReceiveCommand(0); int ack = ByteBuffer.ReadInt4(response, 0); if (ack == ProtocolConstants.Acknowledgement) { // Is there anything more to Read? if (response.Length > 4 && response[4] == 1) { // Yes so Read the server version int serverMajorVersion = ByteBuffer.ReadInt4(response, 5); int serverMinorVersion = ByteBuffer.ReadInt4(response, 9); serverVersion = new Version(serverMajorVersion, serverMinorVersion); } // Send the username and password to the server // SECURITY: username/password sent as plain text. This is okay // if we are connecting to localhost, but not good if we connecting // over the internet. We could encrypt this, but it would probably // be better if we WriteByte the entire stream through an encyption // protocol. bout = new MemoryStream(); output = new BinaryWriter(bout); output.Write(defaultSchema); output.Write(user); output.Write(password); arr = bout.ToArray(); SendCommand(arr, 0, arr.Length); response = ReceiveCommand(0); int result = ByteBuffer.ReadInt4(response, 0); if (result == ProtocolConstants.UserAuthenticationPassed) { // Set the callback, databaseCallback = callback; // User authentication passed so we successfully logged input now. connectionThread = new ConnectionThread(this); connectionThread.Start(); return true; } if (result == ProtocolConstants.UserAuthenticationFailed) throw new DataException("User Authentication failed."); if (result == ProtocolConstants.DatabaseNotFound) throw new DataException("The database specified was not found."); throw new DataException("Unexpected response."); } if (ack == ProtocolConstants.DatabaseNotFound) throw new DataException("The database was not found."); throw new DataException("No acknowledgement received from server."); } catch (IOException e) { LogException(e); throw new DataException("IOException: " + e.Message); } }