using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.IO.MemoryMappedFiles;
namespace server
{
    class Program
    {
        public void Main(string name,object objectData)
        {
            Console.WriteLine("server_start");
            byte[] buffer = ObjectToByteArray(objectData);
            Console.WriteLine(name);
            using (var file = MemoryMappedFile.CreateNew(name, buffer.Length))
            {
                using (var mmfWriter = file.CreateViewAccessor(0, buffer.Length))
                {
                    Console.WriteLine("====================================");
                    // Write the data
                    mmfWriter.WriteArray<byte>(0, buffer, 0, buffer.Length);
                }
                Console.WriteLine("Run memory mapped file reader before exit");
                Console.WriteLine("Press any key to exit ...");
                Console.ReadLine();
            }

        }

        public byte[] ObjectToByteArray(object inputObject)
        {
            BinaryFormatter binaryFormatter = new BinaryFormatter();    // Create new BinaryFormatter
            MemoryStream memoryStream = new MemoryStream();             // Create target memory stream
            binaryFormatter.Serialize(memoryStream, inputObject);       // Serialize object to stream
            return memoryStream.ToArray();                              // Return stream as byte array
        }


    }
}
