Exemplo n.º 1
0
 static private bool ProcessCommandC(String dcommand)
 {
     try
     {
         CommandMessage dscommand = CommandSerializers.Deserialize <CommandMessage>(dcommand);
         Console.WriteLine("C:Deserialized: {0}", dscommand);
     }catch (Exception e)
     {
         Console.WriteLine("C:Deserialization Error: {0}", e.Message);
         return(false);
     }
     return(true);
 }
Exemplo n.º 2
0
        static void Client()
        {
            using (NamedPipeClientStream client = new NamedPipeClientStream(".", NAME,
                                                                            PipeDirection.InOut
                                                                            ))
            {
                Console.WriteLine("Connecting to server ...");
                client.Connect();
                client.ReadMode = PipeTransmissionMode.Message;
                Console.WriteLine("Connected.");

                while (true)
                {
                    Byte[] randbytes = File.ReadAllBytes(@".\MStore.pdb");

                    CommandMessage cm = new CommandMessage {
                        meta = "Q", data = "Data", blob = randbytes
                    };
                    String cms      = CommandSerializers.Serialize(cm);
                    var    cmsBytes = Encoding.ASCII.GetBytes(cms);
                    CommandSerializers.WriteGreen(
                        "Client sends REQUEST of ({0}: Serialized as: {1}\n", cmsBytes.Length, cms);
                    try
                    {
                        client.Write(cmsBytes, 0, cms.Length);
                        client.Flush();
                    }catch (IOException sio)
                    {
                        Console.WriteLine("Send: IO exception {0}", sio.Message);
                    }

                    try
                    {
                        MemoryStream ms     = new MemoryStream();
                        byte[]       buffer = new byte[1024];
                        do
                        {
                            ms.Write(buffer, 0, client.Read(buffer, 0, buffer.Length));
                        } while (!client.IsMessageComplete);

                        string stringData = Encoding.UTF8.GetString(ms.ToArray());
                        CommandSerializers.WriteGreen("Client received RESPONSE: De-Serialized as: {0}\n", stringData);
                        ProcessCommandC(stringData);
                    }catch (IOException sio)
                    {
                        Console.WriteLine("Receive: IO exception {0}", sio.Message);
                    }
                }
            }
        }
Exemplo n.º 3
0
        static void Server()
        {
            using (NamedPipeServerStream server = new NamedPipeServerStream(NAME,
                                                                            PipeDirection.InOut,
                                                                            1,
                                                                            PipeTransmissionMode.Message
                                                                            ))
            {
                evt.Set();
                Console.WriteLine("Waiting for connection ...");
                server.WaitForConnection();
                Console.WriteLine("Connection established.");

                while (true)
                {
                    // TODO: We can leave Memory streams with data, as we could:
                    // - write memory stream to MemoryMappedIO non-file backed store and share over IPC
                    // - convert to something else
                    MemoryStream ms = new MemoryStream();
                    try
                    {
                        byte[] buffer = new byte[1024];
                        var    sb     = new StringBuilder();
                        int    read;

                        while ((read = server.Read(buffer, 0, buffer.Length)) > 0 &&
                               !server.IsMessageComplete)
                        {
                            CommandSerializers.WriteYellow("Server received length {0}\n", read);
                            sb.Append(Encoding.ASCII.GetString(buffer, 0, read));
                        }

                        using (var writer = new StreamWriter(ms))
                        {
                            writer.Write(sb);
                        }
                    } catch (IOException sio) {
                        Console.WriteLine("Receive: IO exception {0}", sio.Message);
                    }

                    string stringData = Encoding.UTF8.GetString(ms.ToArray());
                    CommandSerializers.WriteYellow("Server received REQUEST: Serialized as: {0}\n", stringData);

                    CommandMessage sm = new CommandMessage();
                    if (ProcessCommandS(stringData))
                    {
                        sm.meta = "A";
                    }
                    else
                    {
                        sm.meta = "Q";
                    }

                    sm.data = "Some data";
                    sm.data = null;
                    String sms = CommandSerializers.Serialize(sm);

                    var smsBytes = Encoding.ASCII.GetBytes(sms);
                    CommandSerializers.WriteYellow("Server sends RESPONSE: Serialized as: {0}\n", sms);

                    try
                    {
                        server.Write(smsBytes, 0, smsBytes.Length);
                        server.Flush();
                    }catch (IOException sio) {
                        Console.WriteLine("Receive: IO exception {0}", sio.Message);
                    }

                    Thread.Sleep(1000);     // TODO: tune
                }
            }
        }