Exemple #1
0
        public bool connect(string hostName, int port)
        {
            lock (threadLock)
            {
                try
                {
                    if (sessionID.Length > 0)
                    {
                        return(true);
                    }

                    this.hostName = hostName;
                    this.port     = port;
                    socket        = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    socket.Connect(hostName, port);
                    socket.NoDelay = true;
                    @out           = new LittleEndianDataOutputStream(new BufferedStream(new NetworkStream(socket)));

                    ExtendedDataInput @in  = new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket)));
                    string            body = "connect\n";
                    @out.writeBytes("API 0 ");
                    @out.writeBytes(body.Length.ToString());
                    @out.writeChar('\n');
                    @out.writeBytes(body);
                    @out.flush();


                    string line   = @in.readLine();
                    int    endPos = line.IndexOf(' ');
                    if (endPos <= 0)
                    {
                        close();
                        return(false);
                    }
                    sessionID = line.Substring(0, endPos);

                    int startPos = endPos + 1;
                    endPos = line.IndexOf(' ', startPos);
                    if (endPos != line.Length - 2)
                    {
                        close();
                        return(false);
                    }

                    if (line[endPos + 1] == '0')
                    {
                        remoteLittleEndian = false;
                        @out = new BigEndianDataOutputStream(new BufferedStream(new NetworkStream(socket)));
                    }
                    else
                    {
                        remoteLittleEndian = true;
                    }

                    return(true);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
Exemple #2
0
        public virtual IEntity run(string script, ProgressListener listener)
        {
            lock (threadLock)
            {
                bool reconnect = false;
                if (socket == null || !socket.Connected)
                {
                    if (sessionID.Length == 0)
                    {
                        throw new IOException("Database connection is not established yet.");
                    }
                    else
                    {
                        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        socket.Connect(hostName, port);

                        @out = new LittleEndianDataOutputStream(new BufferedStream(new NetworkStream(socket)));
                    }
                }

                string            body   = "script\n" + script;
                ExtendedDataInput @in    = null;
                string            header = null;
                try
                {
                    @out.writeBytes((listener != null ? "API2 " : "API ") + sessionID + " ");
                    @out.writeBytes(AbstractExtendedDataOutputStream.getUTFlength(body, 0, 0).ToString());
                    @out.writeChar('\n');
                    @out.writeBytes(body);
                    @out.flush();

                    @in = remoteLittleEndian ? (ExtendedDataInput) new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))) : (ExtendedDataInput) new BigEndianDataInputStream(new BufferedStream(new NetworkStream(socket)));

                    header = @in.readLine();
                }
                catch (IOException ex)
                {
                    if (reconnect)
                    {
                        socket = null;
                        throw ex;
                    }

                    try
                    {
                        tryReconnect();
                        @out.writeBytes((listener != null ? "API2 " : "API ") + sessionID + " ");
                        @out.writeBytes(AbstractExtendedDataOutputStream.getUTFlength(body, 0, 0).ToString());
                        @out.writeChar('\n');
                        @out.writeBytes(body);
                        @out.flush();

                        @in       = remoteLittleEndian ? (ExtendedDataInput) new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))) : (ExtendedDataInput) new BigEndianDataInputStream(new BufferedStream(new NetworkStream(socket)));
                        header    = @in.readLine();
                        reconnect = true;
                    }
                    catch (Exception e)
                    {
                        socket = null;
                        throw e;
                    }
                }
                string msg;

                while (header.Equals("MSG"))
                {
                    //read intermediate message to indicate the progress
                    msg = @in.readString();
                    if (listener != null)
                    {
                        listener.progress(msg);
                    }
                    header = @in.readLine();
                }

                string[] headers = header.Split(' ');
                if (headers.Length != 3)
                {
                    socket = null;
                    throw new IOException("Received invalid header: " + header);
                }

                if (reconnect)
                {
                    sessionID = headers[0];
                }
                int numObject = int.Parse(headers[1]);

                msg = @in.readLine();
                if (!msg.Equals("OK"))
                {
                    throw new IOException(msg);
                }

                if (numObject == 0)
                {
                    return(new data.Void());
                }
                try
                {
                    short flag = @in.readShort();
                    int   form = flag >> 8;
                    int   type = flag & 0xff;

                    if (form < 0 || form > MAX_FORM_VALUE)
                    {
                        throw new IOException("Invalid form value: " + form);
                    }
                    if (type < 0 || type > MAX_TYPE_VALUE)
                    {
                        throw new IOException("Invalid type value: " + type);
                    }

                    DATA_FORM df = (DATA_FORM)Enum.GetValues(typeof(DATA_FORM)).GetValue(form);
                    DATA_TYPE dt = (DATA_TYPE)Enum.GetValues(typeof(DATA_TYPE)).GetValue(type);

                    return(factory.createEntity(df, dt, @in));
                }
                catch (IOException ex)
                {
                    socket = null;
                    throw ex;
                }
            }
        }