Example #1
0
        public void ReceiveImpl()
        {
            byte[] bytes = new byte[1024];
            Console.WriteLine($"created buffer");
            int bytesRec = m_socket.Receive(bytes);

            Console.WriteLine($"received");
            int size = PrecheckAllSize(bytes);

            if (size > 1024)
            {
                bytes = new byte[size];
                Console.WriteLine($"reallocate buffer (data size: {size})");
            }
            Desc desc = FromByteDataRevenge(bytes);

            // if (desc == null) { Console.WriteLine($"data is too long to receive"); return; }
            Console.WriteLine($"to desc");
            switch (desc.header.dataType)
            {
            case DataType.Int: Console.WriteLine($"Int: {BitConverter.ToInt32(desc.rawData)}"); break;

            case DataType.Float: Console.WriteLine($"Float: {BitConverter.ToSingle(desc.rawData)}"); break;

            case DataType.String: Console.WriteLine($"String: {Encoding.UTF8.GetString(desc.rawData)}"); break;

            case DataType.Image: Console.WriteLine($"Image: can't open here"); break;
            }
        }
Example #2
0
        public static Desc FromByteData(byte[] data)
        {
            Desc desc = new Desc();
            var  size = Marshal.SizeOf(desc);

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

            Marshal.Copy(buffer, 0, ptr, size);
            desc = (Desc)Marshal.PtrToStructure(ptr, typeof(Desc));
            Marshal.FreeHGlobal(ptr);
            return(desc);
        }
Example #3
0
        public void SendWithHeader(DataType dataType, Ex.LogLevel level, string category, byte[] data)
        {
            Desc desc = new Desc {
                header = new Header(dataType, data.Length),
                // level = level,
                // category = category,
                rawData = data,
            };

            var rtn = m_socket.Send(ToByteRevenge(desc));

            Console.WriteLine($"send with header: {rtn}");
        }
Example #4
0
 public static byte[] ToByteRevenge(Desc desc)
 {
     byte[] type = BitConverter.GetBytes((int)desc.header.dataType);
     // byte[] headerLength = BitConverter.GetBytes(desc.header.dataType);
     byte[] dataLength = BitConverter.GetBytes(desc.header.rawDataSize);
     byte[] data       = desc.rawData;
     Console.WriteLine($"[len vs sizeof] type:{type.Length} vs {sizeof(DataType)}");
     Console.WriteLine($"[len vs sizeof] dataLen:{dataLength.Length} vs {sizeof(int)}");
     Console.WriteLine($"[len vs sizeof] data:{data.Length}");
     byte[] all = new byte[type.Length + dataLength.Length + data.Length];
     Array.Copy(type, all, type.Length);
     Array.Copy(dataLength, 0, all, type.Length, dataLength.Length);
     Array.Copy(data, 0, all, type.Length + dataLength.Length, data.Length);
     Console.WriteLine($"[all in] all:{all.Length}");
     return(all);
 }
Example #5
0
        public static byte[] ToByteData(Desc desc)
        {
            /*
             * int size = Marshal.SizeOf(desc);
             * byte[] bytes = new byte[size];
             * IntPtr ptr = Marshal.AllocHGlobal(size);
             * Marshal.StructureToPtr(desc, ptr, false);
             * Marshal.Copy(ptr, bytes, 0, size);
             * return bytes;
             */
            int size = Marshal.SizeOf(desc);

            byte[] bytes = new byte[size];
            //アンマネージメモリからメモリを割り当て
            IntPtr ptr = Marshal.AllocHGlobal(size);

            //マネージオブジェクトからアンマネージメモリにデータをマーシャリング
            Marshal.StructureToPtr(desc, ptr, false);
            //アンマネージデータをマネージのbyte[]にコピーする
            Marshal.Copy(ptr, bytes, 0, size);
            Marshal.FreeHGlobal(ptr);
            return(bytes);
        }
Example #6
0
 public static string FromDescTemp(Desc desc)
 {
     return(System.Text.Encoding.UTF8.GetString(desc.rawData));
 }