public static byte[] Serialize(Flux flux)
        {
            Console.WriteLine("Trying to serialize a flux");
            BinaryFormatter bin = new BinaryFormatter();
            MemoryStream    mem = new MemoryStream();

            bin.Serialize(mem, flux);
            Console.WriteLine("Flux has been serialized");
            return(mem.GetBuffer());
        }
        public static Flux DeSerialize(byte[] bytes)
        {
            Console.WriteLine("Trying to deserialize a flux");
            List <byte> TransmissionBuffer = new List <byte>(bytes);

            byte[]          dataBuffer = TransmissionBuffer.ToArray();
            BinaryFormatter bin        = new BinaryFormatter();
            MemoryStream    mem        = new MemoryStream();

            mem.Write(dataBuffer, 0, dataBuffer.Length);
            mem.Seek(0, 0);
            Flux flux = (Flux)bin.Deserialize(mem);

            Console.WriteLine("Flux has been deserialized");
            return(flux);
        }
        private void Handler()
        {
            Console.WriteLine("Client thread has started");
            byte[]        bytesFrom     = new byte[clientSocket.ReceiveBufferSize];
            NetworkStream networkStream = clientSocket.GetStream();

            networkStream.Read(bytesFrom, 0, clientSocket.ReceiveBufferSize);
            Flux flux = Flux.DeSerialize(bytesFrom);

            Console.WriteLine("Recieved flux has been deserialized");
            object result = executor.Execute(adapter.GenerateInstance(flux.ReadData()));

            Console.WriteLine("Command has been executed");
            flux.WriteData(ObjectToBytes(result));
            byte[] sendBytes = Flux.Serialize(flux);
            networkStream.Write(sendBytes, 0, sendBytes.Length);
            Console.WriteLine("Response has been sent");
            networkStream.Flush();
            clientSocket.Close();
            Console.WriteLine("Socket has been closed");
        }
示例#4
0
        public Flux Send(Flux flux, List <object> parameters)
        {
            byte[]      bytes      = new byte[100000];
            IPHostEntry ipHostInfo = Dns.GetHostEntry((string)parameters[0]);
            IPAddress   ipAddress  = ipHostInfo.AddressList[0];
            IPEndPoint  remoteEP   = new IPEndPoint(ipAddress, (int)parameters[1]);
            Socket      client     = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                client.Connect(remoteEP);
                int bytesSent = client.Send(Flux.Serialize(flux));
                int bytesRec  = client.Receive(bytes);
                flux = Flux.DeSerialize(bytes);
                client.Shutdown(SocketShutdown.Both);
                client.Close();
                return(flux);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unexpected exception : {0}", e.ToString());
                return(null);
            }
        }