private Rexp parseEvalResponse(RpacketResponse packet) { Int32 rxo = 0; Byte[] data = packet.Content; if (rserveVersion > 100) { /* since 0101 eval responds correctly by using DT_SEXP type/len header which is 4 bytes long */ rxo = 4; /* we should check parameter type (should be DT_SEXP) and fail if it's not */ if (data[0] != (byte)DataTypes.SEXP && data[0] != ((byte)DataTypes.SEXP | (byte)DataTypes.LARGE)) { throw new RconnectionException("Error while processing eval output: SEXP (type " + DataTypes.SEXP.ToString() + ") expected but found result type " + data[0].ToString() + "."); } if (data[0] == ((byte)DataTypes.SEXP | (byte)DataTypes.LARGE)) { rxo = 8; // large data need skip of 8 bytes } /* warning: we are not checking or using the length - we assume that only the one SEXP is returned. This is true for the current CMD_eval implementation, but may not be in the future. */ } Rexp rx = new Rexp(); if (data.Length > rxo) { Rexp.parseREXP(out rx, ref data, rxo); } return(rx); }
/// <summary> /// Method write string value to the remote file /// </summary> /// <param name="input">value to send</param> public void WriteFile(Stream input) { while (input.Position != input.Length) { Byte[] data; if ((input.Length - input.Position) > packetSize) { data = new Byte[packetSize]; } else { data = new Byte[input.Length - input.Position]; } input.Read(data, 0, data.Length); Rmessage msg = new Rmessage(data); Rpacket packet = new Rpacket(RCommands.writeFile, msg); sendedBytesCount += s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None); RpacketResponse response = new RpacketResponse(this.GetResponse()); if (response.IsError) { throw new RconnectionException(response.ErrorCode); } } }
/// <summary> /// Close file on the Rserve /// </summary> public void CloseFile() { Rpacket packet = new Rpacket(RCommands.closeFile); sendedBytesCount += s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None); RpacketResponse response = new RpacketResponse(this.GetResponse()); if (response.IsError) { throw new RconnectionException(response.ErrorCode); } }
/// <summary> /// Assign a content of a REXP to a symbol in R. The symbol is created if /// it doesn't exist already. /// </summary> /// <param name="symbol"> /// Symbol name.It is the responsibility of the user to make sure that the symbol /// name is valid in R. In fact R will always create the symbol, but it may not /// be accessible (examples: "bar\nfoo" or "bar$foo"). /// </param> /// <param name="input">Contents</param> public void Assign(String symbol, Rexp input) { Rmessage Rsymbol = new Rmessage(symbol); Rpacket packet = new Rpacket(RCommands.assignSEXP, Rsymbol, input); sendedBytesCount += s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None); RpacketResponse response = new RpacketResponse(GetResponse()); if (response.IsError) { throw new RconnectionException(response.ErrorCode); } }
/// <summary> /// Evaluates the given command without retriving the result /// </summary> /// <param name="Cmd">command</param> public void VoidEval(String Cmd) { Rmessage msg = new Rmessage(Cmd); Rpacket packet = new Rpacket(RCommands.voidEval, msg); s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None); RpacketResponse response = new RpacketResponse(this.GetResponse()); if (response.IsError) { throw new RconnectionException(response.ErrorCode); } }
/// <summary> /// Method write string value to the remote file /// </summary> /// <param name="input">value to send</param> public void WriteFile(String input) { Rmessage msg = new Rmessage(ASCIIEncoding.ASCII.GetBytes(input)); Rpacket packet = new Rpacket(RCommands.writeFile, msg); sendedBytesCount += s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None); RpacketResponse response = new RpacketResponse(this.GetResponse()); if (response.IsError) { throw new RconnectionException(response.ErrorCode); } }
/// <summary> /// Remove a file on the Rserve /// </summary> /// <param name="FileName"> /// File name should not contain any path delimiters, since /// Rserve may restrict the access to local working directory. /// </param> public void RemoveFile(String FileName) { Rmessage msg = new Rmessage(FileName); Rpacket packet = new Rpacket(RCommands.removeFile, msg); s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None); RpacketResponse response = new RpacketResponse(this.GetResponse()); if (response.IsError) { throw new RconnectionException(response.ErrorCode); } }
/// <summary>Evaluates the given command and retrieves the result</summary> /// <param name="Cmd">command</param> /// <returns>R-xpression</returns> public Rexp Eval(String Cmd) { Rmessage message = new Rmessage(Cmd); //Make message(command with header) Rpacket packet = new Rpacket(RCommands.eval, message); sendedBytesCount += s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None); RpacketResponse response = new RpacketResponse(GetResponse()); if (response.IsOk && (response.ContentLength > 0)) { return(parseEvalResponse(response)); } else { throw new RconnectionException("Eval failed"); } }
/// <summary> /// Reads specified number of bytes (or less) from the remote file. /// </summary> /// <param name="size">maximal number of bytes to read</param> /// <param name="data">buffer to store the read bytes</param> public void ReadFile(Int32 size, out Byte[] data) { Rmessage msg = new Rmessage(size); Rpacket packet = new Rpacket(RCommands.readFile, msg); sendedBytesCount += s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None); RpacketResponse response = new RpacketResponse(this.GetResponse()); if (response.IsOk) { data = response.Content; } else { throw new RconnectionException(response.ErrorCode); } }
/// <summary> /// Reads specified number of bytes (or less) from the remote file. /// </summary> /// <param name="size">maximal number of bytes to read</param> /// <param name="Value">String value to store the read and ASCII decoded bytes</param> public void ReadFile(Int32 size, out String Value) { Rmessage msg = new Rmessage(size); Rpacket packet = new Rpacket(RCommands.readFile, msg); sendedBytesCount += s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None); RpacketResponse response = new RpacketResponse(this.GetResponse()); if (response.IsOk) { Value = ASCIIEncoding.ASCII.GetString(response.Content, 0, response.Content.Length * 2); } else { throw new RconnectionException(response.ErrorCode); } }
///** login using supplied user/pwd. Note that login must be the first ///command if used ///@param user username ///@param pwd password ///@return returns <code>true</code> on success */ public void login(String user, String pwd) { if (authReq) { //if(!s.Connected);not connected if (ARtype == authTypes.crypt) { String command = user + "\n" + UnixCrypt.crypt(pwd, key); Rmessage msg = new Rmessage(command); Rpacket packet = new Rpacket(RCommands.login, msg); s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None); RpacketResponse response = new RpacketResponse(this.GetResponse()); if (response.IsError) { throw new RconnectionException(response.ErrorCode); //try {s.close();} catch (Exception e) {}; //is=null; os=null; s=null; connected=false; } } else { String command = user + "\n" + pwd; Rmessage msg = new Rmessage(command); Rpacket packet = new Rpacket(RCommands.login, msg); s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None); RpacketResponse response = new RpacketResponse(this.GetResponse()); if (response.IsError) { throw new RconnectionException(response.ErrorCode); //try {s.close();} catch (Exception e) {}; //is=null; os=null; s=null; connected=false; } } } }
///** login using supplied user/pwd. Note that login must be the first ///command if used ///@param user username ///@param pwd password ///@return returns <code>true</code> on success */ public void login(String user, String pwd) { if(authReq) { //if(!s.Connected);not connected if (ARtype == authTypes.crypt) { String command = user + "\n" + UnixCrypt.crypt(pwd, key); Rmessage msg = new Rmessage(command); Rpacket packet = new Rpacket(RCommands.login, msg); s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None); RpacketResponse response = new RpacketResponse(this.GetResponse()); if (response.IsError) { throw new RconnectionException(response.ErrorCode); //try {s.close();} catch (Exception e) {}; //is=null; os=null; s=null; connected=false; } } else { String command = user + "\n" + pwd; Rmessage msg = new Rmessage(command); Rpacket packet = new Rpacket(RCommands.login, msg); s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None); RpacketResponse response = new RpacketResponse(this.GetResponse()); if (response.IsError) { throw new RconnectionException(response.ErrorCode); //try {s.close();} catch (Exception e) {}; //is=null; os=null; s=null; connected=false; } } } }
/// <summary>Evaluates the given command and retrieves the result</summary> /// <param name="Cmd">command</param> /// <returns>R-xpression</returns> public Rexp Eval(String Cmd) { Rmessage message = new Rmessage(Cmd); //Make message(command with header) Rpacket packet = new Rpacket(RCommands.eval, message); sendedBytesCount += s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None); RpacketResponse response = new RpacketResponse(GetResponse()); if (response.IsOk && (response.ContentLength > 0)) { return (parseEvalResponse(response)); } else throw new RconnectionException("Eval failed"); }
/// <summary> /// Method write string value to the remote file /// </summary> /// <param name="input">value to send</param> public void WriteFile(Stream input) { while (input.Position != input.Length) { Byte[] data; if ((input.Length - input.Position) > packetSize) data = new Byte[packetSize]; else data = new Byte[input.Length - input.Position]; input.Read(data, 0, data.Length); Rmessage msg = new Rmessage(data); Rpacket packet = new Rpacket(RCommands.writeFile, msg); sendedBytesCount += s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None); RpacketResponse response = new RpacketResponse(this.GetResponse()); if (response.IsError) { throw new RconnectionException(response.ErrorCode); } } }
private Rexp parseEvalResponse(RpacketResponse packet) { Int32 rxo = 0; Byte[] data = packet.Content; if (rserveVersion > 100) { /* since 0101 eval responds correctly by using DT_SEXP type/len header which is 4 bytes long */ rxo = 4; /* we should check parameter type (should be DT_SEXP) and fail if it's not */ if (data[0] != (byte)DataTypes.SEXP && data[0] != ((byte)DataTypes.SEXP | (byte)DataTypes.LARGE)) { throw new RconnectionException("Error while processing eval output: SEXP (type " + DataTypes.SEXP.ToString() + ") expected but found result type " + data[0].ToString() + "."); } if (data[0] == ((byte)DataTypes.SEXP | (byte)DataTypes.LARGE)) { rxo = 8; // large data need skip of 8 bytes } /* warning: we are not checking or using the length - we assume that only the one SEXP is returned. This is true for the current CMD_eval implementation, but may not be in the future. */ } Rexp rx = new Rexp(); if (data.Length > rxo) { Rexp.parseREXP(out rx, ref data, rxo); } return rx; }