I'm using a NamedPipeStream, client and server, I'm sending data from client to server, the data is a serialize object which include binary data.
When the server side receive the data, I try to deserialize the data I received: TransmitDataCommand dataR= JsonConvert.DeserializeObject(e.Message));
I tried to save the data in base64 string instead of byte[] but still not work!!
But I get an exception:
"Unterminated string. Expected delimiter: ". Path 'Data', line 1, position 1024."
Why? how to solve this :(?
Send from client: write(lTransmitDataCommand.Message())
The pipe read the data: OnReceivedMessage(this,new ReceivedMessageEventArgs(Encoding.ASCII.GetString(message)));
and try: JsonConvert.DeserializeObject(e.Message)); // wher the exception happen!
The Object:
[Serializable]
public abstract class AgentConnectorAbstractCommand
{
public virtual eAGENTCONNECTOR_CommandOpcode Opcode { get; set; }
public virtual void Dispose()
{
return;
}
public abstract string Message();
}
public enum eComandType{
/// <summary>
/// This command is from client to Agent
/// </summary>
eCLIENT2AGENT_CMD=0,
/// <summary>
/// This command is from agent to client
/// </summary>
eAGENT2CLIENT_CMD=1,
/// <summary>
/// This command is from client to Agent
/// </summary>
eRESPONSE_CMD=2,
}
[Serializable]
public class TransmitDataCommand : AgentConnectorAbstractCommand
{
public TransmitDataCommand()
{
IsBinary = false;
}
private eAGENTCONNECTOR_CommandOpcode _Opcode = eAGENTCONNECTOR_CommandOpcode.eTRANSMITDATA;
public override eAGENTCONNECTOR_CommandOpcode Opcode { get { return _Opcode; } }
/// <summary>
/// Define what is the type of the data (is it command or response)
/// </summary>
public eComandType ComandType { get; set; }
/// <summary>
/// indicate if the data is byte array or text
/// </summary>
public bool IsBinary { get; set; }
/// <summary>
/// Hold the byte data
/// </summary>
public byte[] Data
{
get
{
if ((DataTxT == null)||(IsBinary==false))
{
return null;
}
return Convert.FromBase64String(DataTxT);
}
set
{
if (value == null)
{
IsBinary = false;
this.DataTxT = null;
return;
}
try
{
IsBinary = true;
//keep a clone of the image to protect from any changes outside!!!
this.DataTxT = Convert.ToBase64String(value);
}
catch { }
}
}
/// <summary>
/// Hold the string data
/// </summary>
private string lTextData = null;
public string DataTxT
{
get
{
return lTextData;
}
set
{
this.lTextData = value;
}
}
public override void Dispose()
{
lTextData= null;
GC.Collect();
}
/// <summary>
/// Return serialized object
/// </summary>
public override string Message()
{
TransmitDataCommand l = new TransmitDataCommand
{
Opcode = this._Opcode,
ComandType = this.ComandType,
DataTxT = this.DataTxT,
IsBinary = this.IsBinary
};
return JsonConvert.SerializeObject(l);
}
}