2008-08-05 15 views
19

में डेटा संरचनाओं के लिए स्ट्रीम डेटा मैपिंग क्या डेटा संरचना या इसके विपरीत किसी स्ट्रीम या सरणी पर एकत्रित डेटा मैप करने का कोई तरीका है? C++सी #

Mystruct * pMyStrct = (Mystruct*)&SomeDataStream; 
pMyStrct->Item1 = 25; 

int iReadData = pMyStrct->Item2; 

जाहिर सी ++ रास्ता है: C++ में यह बस मैं उपयोग करने के लिए (या रिवर्स के लिए इसके विपरीत) जैसे चाहते हैं एक डेटा प्रकार के रूप में धारा के सूचक के कास्टिंग की बात होगी जब तक आप आने वाले डेटा को पढ़ते समय स्ट्रीम डेटा की गुणवत्ता सुनिश्चित नहीं करते हैं, लेकिन आउटगोइंग डेटा सुपर त्वरित और आसान है, तब तक असुरक्षित।

उत्तर

16

अधिकांश लोगों को नेट क्रमबद्धता का उपयोग करें (तेजी से बाइनरी और धीमी एक्सएमएल फ़ॉर्मेटर है, वे दोनों प्रतिबिंब पर निर्भर करते हैं और संस्करण निश्चित डिग्री के सहिष्णु हैं) अगर आप चाहते हैं सबसे तेजी से (असुरक्षित)

हालांकि, तरीका है - क्यों नहीं:

लेखन:

YourStruct o = new YourStruct(); 
byte[] buffer = new byte[Marshal.SizeOf(typeof(YourStruct))]; 
GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); 
Marshal.StructureToPtr(o, handle.AddrOfPinnedObject(), false); 
handle.Free(); 

पढ़ना:

handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); 
o = (YourStruct)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(YourStruct)); 
handle.Free(); 
2

अगर दोनों पक्षों पर अपने .net:

लगता है कि आप द्विआधारी क्रमांकन का उपयोग करें और बाइट [] परिणाम भेजना चाहिए।

आपकी संरचना को पूरी तरह से ब्लिटेबल होने पर भरोसा करना परेशानी हो सकती है।

आप कुछ ओवरहेड (सीपीयू और नेटवर्क दोनों) में भुगतान करेंगे लेकिन सुरक्षित रहेगा।

2

यदि आपको प्रत्येक सदस्य चर को हाथ से पॉप्युलेट करने की आवश्यकता है तो आप इसे ऑब्जेक्ट से जुड़े परिवर्तनीय प्रकारों की सूची क्रम में पुनर्प्राप्त करने के लिए फॉर्मेटर्स सर्विसेज का उपयोग करके चिंतित हैं। मुझे इसे एक ऐसे प्रोजेक्ट में करना पड़ा जहां मेरे पास स्ट्रीम से आने वाले कई अलग-अलग संदेश प्रकार थे और मैं निश्चित रूप से प्रत्येक संदेश के लिए धारावाहिक/deserializer लिखना नहीं चाहता था।

यहां कोड है जिसे मैं बाइट [] से deserialization को सामान्यीकृत करने के लिए उपयोग किया जाता है।

public virtual bool SetMessageBytes(byte[] message) 
    { 
     MemberInfo[] members = FormatterServices.GetSerializableMembers(this.GetType()); 
     object[] values = FormatterServices.GetObjectData(this, members); 
     int j = 0; 

     for (int i = 0; i < members.Length; i++) 
     { 
      string[] var = members[i].ToString().Split(new char[] { ' ' }); 
      switch (var[0]) 
      { 
       case "UInt32": 
        values[i] = (UInt32)((message[j] << 24) + (message[j + 1] << 16) + (message[j + 2] << 8) + message[j + 3]); 
        j += 4; 
        break; 
       case "UInt16": 
        values[i] = (UInt16)((message[j] << 8) + message[j + 1]); 
        j += 2; 
        break; 
       case "Byte": 
        values[i] = (byte)message[j++]; 
        break; 
       case "UInt32[]": 
        if (values[i] != null) 
        { 
         int len = ((UInt32[])values[i]).Length; 
         byte[] b = new byte[len * 4]; 
         Array.Copy(message, j, b, 0, len * 4); 
         Array.Copy(Utilities.ByteArrayToUInt32Array(b), (UInt32[])values[i], len); 
         j += len * 4; 
        } 
        break; 
       case "Byte[]": 
        if (values[i] != null) 
        { 
         int len = ((byte[])values[i]).Length; 
         Array.Copy(message, j, (byte[])(values[i]), 0, len); 
         j += len; 
        } 
        break; 
       default: 
        throw new Exception("ByteExtractable::SetMessageBytes Unsupported Type: " + var[1] + " is of type " + var[0]); 
      } 
     } 
     FormatterServices.PopulateObjectMembers(this, members, values); 
     return true; 
    } 
5

यदि लुब्स हैको का जवाब पर्याप्त असुरक्षित नहीं था, तो वास्तव में असुरक्षित तरीके से सी # में पॉइंटर्स का उपयोग करके असुरक्षित तरीके भी है। यहां कुछ युक्तियां और समस्याएं हैं जिन्हें मैंने चलाया है:

using System; 
using System.Runtime.InteropServices; 
using System.IO; 
using System.Diagnostics; 

// Use LayoutKind.Sequential to prevent the CLR from reordering your fields. 
[StructLayout(LayoutKind.Sequential)] 
unsafe struct MeshDesc 
{ 
    public byte NameLen; 
    // Here fixed means store the array by value, like in C, 
    // though C# exposes access to Name as a char*. 
    // fixed also requires 'unsafe' on the struct definition. 
    public fixed char Name[16]; 
    // You can include other structs like in C as well. 
    public Matrix Transform; 
    public uint VertexCount; 
    // But not both, you can't store an array of structs. 
    //public fixed Vector Vertices[512]; 
} 

[StructLayout(LayoutKind.Sequential)] 
unsafe struct Matrix 
{ 
    public fixed float M[16]; 
} 

// This is how you do unions 
[StructLayout(LayoutKind.Explicit)] 
unsafe struct Vector 
{ 
    [FieldOffset(0)] 
    public fixed float Items[16]; 
    [FieldOffset(0)] 
    public float X; 
    [FieldOffset(4)] 
    public float Y; 
    [FieldOffset(8)] 
    public float Z; 
} 

class Program 
{ 
    unsafe static void Main(string[] args) 
    { 
     var mesh = new MeshDesc(); 
     var buffer = new byte[Marshal.SizeOf(mesh)]; 

     // Set where NameLen will be read from. 
     buffer[0] = 12; 
     // Use Buffer.BlockCopy to raw copy data across arrays of primitives. 
     // Note we copy to offset 2 here: char's have alignment of 2, so there is 
     // a padding byte after NameLen: just like in C. 
     Buffer.BlockCopy("Hello!".ToCharArray(), 0, buffer, 2, 12); 

     // Copy data to struct 
     Read(buffer, out mesh); 

     // Print the Name we wrote above: 
     var name = new char[mesh.NameLen]; 
     // Use Marsal.Copy to copy between arrays and pointers to arrays. 
     unsafe { Marshal.Copy((IntPtr)mesh.Name, name, 0, mesh.NameLen); } 
     // Note you can also use the String.String(char*) overloads 
     Console.WriteLine("Name: " + new string(name)); 

     // If Erik Myers likes it... 
     mesh.VertexCount = 4711; 

     // Copy data from struct: 
     // MeshDesc is a struct, and is on the stack, so it's 
     // memory is effectively pinned by the stack pointer. 
     // This means '&' is sufficient to get a pointer. 
     Write(&mesh, buffer); 

     // Watch for alignment again, and note you have endianess to worry about... 
     int vc = buffer[100] | (buffer[101] << 8) | (buffer[102] << 16) | (buffer[103] << 24); 
     Console.WriteLine("VertexCount = " + vc); 
    } 

    unsafe static void Write(MeshDesc* pMesh, byte[] buffer) 
    { 
     // But byte[] is on the heap, and therefore needs 
     // to be flagged as pinned so the GC won't try to move it 
     // from under you - this can be done most efficiently with 
     // 'fixed', but can also be done with GCHandleType.Pinned. 
     fixed (byte* pBuffer = buffer) 
      *(MeshDesc*)pBuffer = *pMesh; 
    } 

    unsafe static void Read(byte[] buffer, out MeshDesc mesh) 
    { 
     fixed (byte* pBuffer = buffer) 
      mesh = *(MeshDesc*)pBuffer; 
    } 
} 

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^