Пример #1
0
        private SednaSession(string dbname, string host, int port, string userName, string password)
        {
            currentDatabase = dbname;
            try
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Connect(new IPEndPoint(Dns.GetHostEntry(host).AddressList[0], port));
            }
            catch (IOException ex)
            {
                throw new SednaException("No connection could be made to that host: " + ex.Message);
            }

            outputStream = new NetworkStream(socket, FileAccess.Write);
            inputStream  = new NetworkStream(socket, FileAccess.Read);
            // send startup message.
            NetworkOperations.WriteMessage(InstructionCode.StartUp, outputStream, inputStream, InstructionCode.SendSessionParameters);
            // send parameters.
            NetworkOperations.Message msg = CreateParametersMessage(dbname, userName, password);
            NetworkOperations.WriteMessage(msg, outputStream);

            // get the response...
            NetworkOperations.ReadMessage(msg, inputStream);
            // need to send the password
            if (msg.Instruction == InstructionCode.SendAuthenticationParameters)
            {
                NetworkOperations.WriteMessage(InstructionCode.AuthenticationResponse, outputStream, inputStream, new string[] { password }, InstructionCode.AuthenticationOK);
            }
            else if (msg.Instruction == InstructionCode.AuthenticationFailed || msg.Instruction == InstructionCode.GeneralError)
            {
                throw new SednaException("Authentication failed");
            }
        }
Пример #2
0
 internal QueryResult(NetworkOperations.StringItem item, NetworkStream inputStream, NetworkStream outputStream)
 {
     this.inputStream = inputStream;
     this.outputStream = outputStream;
     text = item.Item;
     hasNextItem = item.HasNextItem;
 }
Пример #3
0
 /// <summary>
 /// Terminates a session, rolling back any transactions that were not committed.
 /// </summary>
 public void Close()
 {
     // for reason the server rejects this message.
     NetworkOperations.WriteMessage(InstructionCode.CloseConnection, outputStream, inputStream, InstructionCode.ConnectionClosed, InstructionCode.TransactionRollback);
     inputStream.Close();
     outputStream.Close();
     isClosed = true;
 }
Пример #4
0
 /// <summary>
 /// Writes out a string (or series of strings) in sedna format.
 /// </summary>
 public static void WriteString(BinaryWriter writer, params string[] textItems)
 {
     foreach (string text in textItems)
     {
         writer.Write((byte)0);
         NetworkOperations.WriteInt(text.Length, int_array, 0);
         writer.Write(int_array);
         writer.Write(Encoding.ASCII.GetBytes(text));
     }
 }
Пример #5
0
 public static void WriteMessage(Message msg, NetworkStream outputStream)
 {
     if (msg.Body.Length > SocketMessageBufferSize)
     {
         throw new IndexOutOfRangeException("Size of packet exceeds maximum buffer size");
     }
     NetworkOperations.WriteInt((int)msg.Instruction, outputStream);
     NetworkOperations.WriteInt((int)msg.Body.Length, outputStream);
     outputStream.Write(msg.Body, 0, msg.Body.Length);
     outputStream.Flush();
 }
Пример #6
0
        public static void ReadMessage(Message msg, NetworkStream bufInputStream)
        {
            msg.Instruction = (InstructionCode)NetworkOperations.ReadInt(bufInputStream);
            int len = NetworkOperations.ReadInt(bufInputStream);

            msg.Body = new byte[len];

            if (len != 0)
            {
                ReadWithTimeout(bufInputStream, msg.Body, 0, len);
            }
        }
Пример #7
0
        private NetworkOperations.Message CreateParametersMessage(string dbname, string userName, string password)
        {
            NetworkOperations.Message msg = new NetworkOperations.Message();
            MemoryStream stream           = new MemoryStream();
            BinaryWriter writer           = new BinaryWriter(stream);

            msg.Instruction = InstructionCode.Login;
            writer.Write((byte)1);             // protocol version... 1.0
            writer.Write((byte)0);
            NetworkOperations.WriteString(writer, userName, dbname);
            writer.Close();
            msg.Body = stream.ToArray();
            return(msg);
        }
Пример #8
0
        public static bool BulkLoad(Stream input, NetworkStream bufInputStream, NetworkStream outputStream)
        {
            Message msg        = new Message();
            int     bytes_read = -1;

            try
            {
                while (bytes_read != 0)
                {
                    byte[] buffer = new byte[BulkLoadPortionSize];
                    bytes_read = input.Read(buffer, 0, BulkLoadPortionSize);
                    if (bytes_read != 0)
                    {
                        // crop the array to the correct size
                        msg.Body = new byte[bytes_read + IntSize + StringTypeSize];
                        Array.Copy(buffer, 0, msg.Body, IntSize + StringTypeSize, bytes_read);

                        msg.Instruction = InstructionCode.BulkLoadPortion;
                        msg.Body[0]     = 0;

                        NetworkOperations.WriteInt(bytes_read, msg.Body, 0 + StringTypeSize);
                        NetworkOperations.WriteMessage(msg, outputStream);
                    }
                }

                NetworkOperations.WriteMessage(InstructionCode.BulkLoadEnd, outputStream, bufInputStream, InstructionCode.BulkLoadSucceeded, InstructionCode.UpdateSucceeded);

                return(true);
            }
            catch (IOException e)
            {
                msg.Instruction = InstructionCode.BulkLoadError;
                try
                {
                    NetworkOperations.WriteMessage(msg, outputStream);
                    NetworkOperations.ReadMessage(msg, bufInputStream);
                }
                catch (IOException ex)
                {
                    throw new SednaException(InstructionCode.BulkLoadFailed, "Unable to bulk load: IO error: " + ex.Message);
                }

                throw new SednaException(InstructionCode.BulkLoadFailed, NetworkOperations.GetErrorInfo(msg.Instruction, msg.Body) + " : " + e.Message);
            }
        }
Пример #9
0
        public static StringItem ReadStringItem(NetworkStream inputStream)
        {
            Message       msg    = new Message();
            StringItem    sitem  = new StringItem();
            StringBuilder strBuf = new StringBuilder();

            NetworkOperations.ReadMessage(msg, inputStream);
            if ((msg.Instruction == InstructionCode.ItemEnd) || (msg.Instruction == InstructionCode.ResultEnd))
            {
                sitem.HasNextItem = false;
                sitem.Item        = null;
                return(sitem);
            }

            while ((msg.Instruction != InstructionCode.ItemEnd) && (msg.Instruction != InstructionCode.ResultEnd))
            {
                if (msg.Instruction == InstructionCode.GeneralError)
                {
                    throw NetworkOperations.GetErrorInfo(msg.Instruction, msg.Body);
                }

                if (msg.Instruction == InstructionCode.ItemPart)
                {
                    strBuf.Append(Encoding.ASCII.GetString(msg.Body, (IntSize + StringTypeSize), msg.Body.Length - (IntSize + StringTypeSize)));
                }

                NetworkOperations.ReadMessage(msg, inputStream);
            }

            if (msg.Instruction == InstructionCode.ResultEnd)
            {
                sitem.HasNextItem = false;
            }
            else if (msg.Instruction == InstructionCode.ItemEnd)
            {
                sitem.HasNextItem = true;
            }

            sitem.Item = strBuf.ToString();

            return(sitem);
        }
Пример #10
0
        /// <summary>
        /// Retrieves the next part of the result.
        /// </summary>
        /// <returns></returns>
        public string Next()
        {
            if (!hasNextItem)
            {
                return(null);
            }

            NetworkOperations.Message msg = new NetworkOperations.Message();
            msg.Instruction = InstructionCode.GetNextItem;
            NetworkOperations.WriteMessage(msg, outputStream);

            NetworkOperations.StringItem item = NetworkOperations.ReadStringItem(inputStream);
            if (item.Item == null)
            {
                hasNextItem = false;
                return(null);
            }
            else
            {
                text += item.Item;
                return(item.Item);
            }
        }
Пример #11
0
        /// <summary>
        /// Executes a query that is read from a stream.
        /// </summary>
        public QueryResult Execute(TextReader queryReader)
        {
            try
            {
                if (!inTransaction)
                {
                    BeginTransaction();
                }

                string query = queryReader.ReadToEnd();
                NetworkOperations.Message msg = new NetworkOperations.Message();
                msg.Instruction = InstructionCode.Execute;

                MemoryStream stream = new MemoryStream();
                BinaryWriter writer = new BinaryWriter(stream);
                writer.Write((byte)0);                 // xml is 0, scheme xml is 1
                NetworkOperations.WriteString(writer, query);
                writer.Close();

                msg.Body = stream.ToArray();

                NetworkOperations.WriteMessage(msg, outputStream);

                NetworkOperations.ReadMessage(msg, inputStream);

                if (msg.Instruction == InstructionCode.QuerySucceeded)
                {
                    return(new QueryResult(NetworkOperations.ReadStringItem(inputStream), inputStream, outputStream));
                }

                else if (msg.Instruction == InstructionCode.QueryFailed || msg.Instruction == InstructionCode.UpdateFailed || msg.Instruction == InstructionCode.GeneralError)
                {
                    throw NetworkOperations.GetErrorInfo(msg.Instruction, msg.Body);
                }

                else if (msg.Instruction == InstructionCode.UpdateSucceeded)
                {
                    haveUpdated = true;
                    return(null);
                }

                else if (msg.Instruction == InstructionCode.BulkLoadFileRequest)
                {
                    try
                    {
                        haveUpdated = true;
                        string filename   = NetworkOperations.ReadString(msg, 0);
                        Stream fileStream = File.OpenRead(filename);
                        if (!NetworkOperations.BulkLoad(fileStream, inputStream, outputStream))
                        {
                            throw new SednaException(InstructionCode.BulkLoadFailed, "Bulk load failed");
                        }
                        return(null);
                    }
                    finally
                    {
                        stream.Close();
                    }
                }
                else if (msg.Instruction == InstructionCode.BulkLoadStreamRequest)
                {
                    if (!NetworkOperations.BulkLoad(Console.OpenStandardInput(), inputStream, outputStream))
                    {
                        throw new SednaException("Bulk load failed");
                    }
                    return(null);
                }
                else
                {
                    throw new SednaException("Unexpected error");
                }
            }
            catch (Exception ex)
            {
                inTransaction = false;
                haveUpdated   = false;
                throw ex;
            }
        }
Пример #12
0
 /// <summary>
 /// Commits the current transaction
 /// </summary>
 public void CommitTransaction()
 {
     inTransaction = false;
     haveUpdated   = false;
     NetworkOperations.WriteMessage(InstructionCode.CommitTransaction, outputStream, inputStream, InstructionCode.CommitSucceeded);
 }
Пример #13
0
 public void RollbackTransaction()
 {
     inTransaction = false;
     haveUpdated   = false;
     NetworkOperations.WriteMessage(InstructionCode.RollbackTransaction, outputStream, inputStream, InstructionCode.TransactionRollback);
 }
Пример #14
0
 /// <summary>
 /// Begins a new transaction manually
 /// </summary>
 public void BeginTransaction()
 {
     NetworkOperations.WriteMessage(InstructionCode.BeginTransaction, outputStream, inputStream, InstructionCode.BeginTransactionOk);
     inTransaction = true;
     haveUpdated   = false;
 }