c# - Transmit xml serialized objects thru named pipe (without WCF) -
i want send specific objects thru simple named pipe. think receiver not recognize cut between 2 sent objects. in result, xml serialization error. here code:
server process:
using system; using system.io; using system.io.pipes; using system.threading; using system.xml.serialization; namespace namedpipe { class programserver { static void main(string[] args) { namedpipeserverstream server; server = new namedpipeserverstream("pipesofpiece"); server.waitforconnection(); streamreader reader = new streamreader(server); streamwriter writer = new streamwriter(server); console.writeline("start loop. "); thread.sleep(1000); while (true) { xmlserializer receiver = new xmlserializer(typeof(payload)); object testobj = receiver.deserialize(reader); console.writeline("testobject: " + ((payload)(testobj)).name + ((payload)(testobj)).count); } } } }
client process:
using system; using system.collections.generic; using system.io; using system.io.pipes; using system.linq; using system.text; using system.threading.tasks; using system.xml.serialization; namespace namedpipe { class programclient { static void main(string[] args) { //client var client = new namedpipeclientstream("pipesofpiece"); client.connect(); streamreader reader = new streamreader(client); while (true) { streamwriter writer = new streamwriter(client); payload test = new payload () { name = "test", count = 42 }; xmlserializer sendserializer = new xmlserializer(typeof(payload)); sendserializer.serialize(writer, test); } } } }
i have tried using namedpipes without wcf. can code might give idea. here, client enabled callback functionality server can callback client.
static class program { /// <summary> /// main entry point application. /// </summary> [stathread] static void main(string[] args) { const string pipe_name = "testpipename33"; const string object_name = "test"; const string callback_pipe_name = "testpipename34"; const string callback_object_name = "testclient"; application.enablevisualstyles(); application.setcompatibletextrenderingdefault(false); if ((args.length == 0 || args[0] == "s")) { try { ipcregistration.registerserver(pipe_name,object_name); } catch (remotingexception) { remoteobject = ipcregistration.registerclient(typeof(remoteobject),pipe_name,object_name); remoteobject.onnewprocessstarted("test"); application.exit(); return; } messagebox.show("server:" + process.getcurrentprocess().id); process.start(application.executablepath, "c"); application.run(new form1("server")); } else { isclient = true; remoteobject = ipcregistration.registerclient(typeof(remoteobject), pipe_name, object_name); ipcregistration.registerserver(callback_pipe_name, callback_object_name); // here client listen on channel. remoteobject.setonnewprocessstarted(onnewprocessstarted,process.getcurrentprocess().id.tostring()); messagebox.show("client:" + process.getcurrentprocess().id); application.run(new form1("client")); } } static remoteobject remoteobject; static bool isclient = false; static bool onnewprocessstarted(string commandline) { messagebox.show("saved:"+commandline+" currrent:"+process.getcurrentprocess().id); messagebox.show("is client : " + isclient);//problem here, isclient should true return true; } } public delegate bool onnewprocessstarteddelegate(string text); internal class remoteobject : marshalbyrefobject { public onnewprocessstarteddelegate onnewprocessstartedhandler; public string value; public bool iscallback = false; const string pipe_name = "testpipename33"; const string object_name = "test"; const string callback_pipe_name = "testpipename34"; const string callback_object_name = "testclient"; remoteobject remoteobject; public bool onnewprocessstarted(string commandline) { if (!iscallback) { remoteobject.iscallback = true; return remoteobject.onnewprocessstarted(commandline); } if (onnewprocessstartedhandler != null) return onnewprocessstartedhandler(value); return false; } public void setonnewprocessstarted(onnewprocessstarteddelegate onnewprocessstarted,string value) { this.value = value; onnewprocessstartedhandler = onnewprocessstarted; if (!iscallback) { remoteobject = ipcregistration.registerclient(typeof(remoteobject), callback_pipe_name, callback_object_name); remoteobject.iscallback = true; remoteobject.setonnewprocessstarted(onnewprocessstarted, process.getcurrentprocess().id.tostring()); } } public override object initializelifetimeservice() { return null; } } internal class ipcregistration { public static remoteobject registerclient(type remoteobject,string pipe_name,string object_name) { ipcclientchannel chan = new ipcclientchannel(); channelservices.registerchannel(chan, false); remoteobject remoteobjectinstance = (remoteobject)activator.getobject(remoteobject, string.format("ipc://{0}/{1}", pipe_name, object_name)); return remoteobjectinstance; } public static void registerserver(string pipename, string objectname) { binaryserverformattersinkprovider serverprovider = new binaryserverformattersinkprovider(); serverprovider.typefilterlevel = typefilterlevel.full; ipcserverchannel chan = new ipcserverchannel("", pipename, serverprovider); channelservices.registerchannel(chan, false); remotingservices.marshal(new remoteobject(), objectname); } }
Comments
Post a Comment