Esempio n. 1
0
        /// <summary>
        /// from HeadPacket to byte[]
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public static byte[] getBytes(HeadPacket p)
        {
            int size = Marshal.SizeOf(p);

            byte[] arr = new byte[size];
            IntPtr ptr = Marshal.AllocHGlobal(size);

            Marshal.StructureToPtr(p, ptr, true);
            Marshal.Copy(ptr, arr, 0, size);
            Marshal.FreeHGlobal(ptr);

            return(arr);
        }
Esempio n. 2
0
        /// <summary>
        /// from byte[] to HeadPacket
        /// </summary>
        /// <param name="arr"></param>
        /// <returns></returns>
        public static HeadPacket fromBytes(byte[] arr)
        {
            HeadPacket str = new HeadPacket();

            int    size = Marshal.SizeOf(str);
            IntPtr ptr  = Marshal.AllocHGlobal(size);

            Marshal.Copy(arr, 0, ptr, size);

            str = (HeadPacket)Marshal.PtrToStructure(ptr, str.GetType());
            Marshal.FreeHGlobal(ptr);

            return(str);
        }
Esempio n. 3
0
        /// <summary>
        ///  read from server
        /// </summary>
        /// <returns></returns>
        protected override serverReturnEventArgs receive()
        {
            try
            {
                Packet packet = new Packet();

                int        len         = Marshal.SizeOf(new HeadPacket());
                byte[]     receiveData = new byte[len];
                HeadPacket headpacket  = new HeadPacket();

                clientStream.Read(receiveData, 0, len - 4);
                clientStream.Flush();

                String headdata = System.Text.Encoding.UTF8.GetString(receiveData);
                headpacket        = fromBytes(receiveData);
                packet.headpacket = headpacket;

                receiveData = new byte[packet.headpacket.size];
                clientStream.Read(receiveData, 0, packet.headpacket.size);
                clientStream.Flush();

                String data = System.Text.Encoding.UTF8.GetString(receiveData);


                return(new serverReturnEventArgs(packet.headpacket.source, packet.headpacket.destination,
                                                 packet.headpacket.function, packet.headpacket.size, System.Text.Encoding.UTF8.GetString(receiveData)));
            }
            catch (System.OutOfMemoryException)
            {
                // Server gone wild :/
            }
            catch (System.IO.IOException)
            {
                //disconected because you try to reach an other serv
            }
            catch (ObjectDisposedException)
            {
                //disconected because you try to reach an other serv
            }
            return(new serverReturnEventArgs(0, 0, "EERR", 2, "KO"));
        }
Esempio n. 4
0
        private serverReturnEventArgs receive()
        {
            Packet packet = new Packet();

            int len = Marshal.SizeOf(new HeadPacket());

            byte[]     receiveData = new byte[len];
            HeadPacket headpacket  = new HeadPacket();

            clientStream.Read(receiveData, 0, len);
            clientStream.Flush();

            headpacket        = fromBytes(receiveData);
            packet.headpacket = headpacket;

            receiveData = new byte[packet.headpacket.size];
            clientStream.Read(receiveData, 0, packet.headpacket.size);
            clientStream.Flush();

            return(new serverReturnEventArgs(packet.headpacket.source, packet.headpacket.destination,
                                             packet.headpacket.function, packet.headpacket.size, System.Text.Encoding.UTF8.GetString(receiveData)));
        }
Esempio n. 5
0
        /// <summary>
        ///  read from server
        /// </summary>
        /// <returns></returns>
        protected override serverReturnEventArgs receive()
        {
            try
            {
                Packet packet = new Packet();

                int len = Marshal.SizeOf(new HeadPacket());
                byte[] receiveData = new byte[len];
                HeadPacket headpacket = new HeadPacket();

                clientStream.Read(receiveData, 0, len - 4);
                clientStream.Flush();

                String headdata = System.Text.Encoding.UTF8.GetString(receiveData);
                headpacket = fromBytes(receiveData);
                packet.headpacket = headpacket;

                receiveData = new byte[packet.headpacket.size];
                clientStream.Read(receiveData, 0, packet.headpacket.size);
                clientStream.Flush();

                String data = System.Text.Encoding.UTF8.GetString(receiveData);


                return new serverReturnEventArgs(packet.headpacket.source, packet.headpacket.destination,
                    packet.headpacket.function, packet.headpacket.size, System.Text.Encoding.UTF8.GetString(receiveData));
            }
            catch (System.OutOfMemoryException)
            {
                // Server gone wild :/
            }
            catch (System.IO.IOException)
            {
                //disconected because you try to reach an other serv
            }
            catch (ObjectDisposedException)
            {
                //disconected because you try to reach an other serv
            }
            return new serverReturnEventArgs(0,0,"EERR", 2, "KO");
        }
Esempio n. 6
0
        /// <summary>
        /// from byte[] to HeadPacket
        /// </summary>
        /// <param name="arr"></param>
        /// <returns></returns>
        public static HeadPacket fromBytes(byte[] arr)
        {
            HeadPacket str = new HeadPacket();

            int size = Marshal.SizeOf(str);
            IntPtr ptr = Marshal.AllocHGlobal(size);

            Marshal.Copy(arr, 0, ptr, size);

            str = (HeadPacket)Marshal.PtrToStructure(ptr, str.GetType());
            Marshal.FreeHGlobal(ptr);

            return str;
        }
Esempio n. 7
0
        /// <summary>
        /// from HeadPacket to byte[]
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public static byte[] getBytes(HeadPacket p)
        {
            int size = Marshal.SizeOf(p);
            byte[] arr = new byte[size];
            IntPtr ptr = Marshal.AllocHGlobal(size);

            Marshal.StructureToPtr(p, ptr, true);
            Marshal.Copy(ptr, arr, 0, size);
            Marshal.FreeHGlobal(ptr);

            return arr;
        }
Esempio n. 8
0
        private serverReturnEventArgs receive()
        {
            Packet packet = new Packet();

            int len = Marshal.SizeOf(new HeadPacket());
            byte[] receiveData = new byte[len];
            HeadPacket headpacket = new HeadPacket();

            clientStream.Read(receiveData, 0, len);
            clientStream.Flush();

            headpacket = fromBytes(receiveData);
            packet.headpacket = headpacket;

            receiveData = new byte[packet.headpacket.size];
            clientStream.Read(receiveData, 0, packet.headpacket.size);
            clientStream.Flush();

            return new serverReturnEventArgs(packet.headpacket.source, packet.headpacket.destination,
                packet.headpacket.function, packet.headpacket.size, System.Text.Encoding.UTF8.GetString(receiveData));
        }