In all honesty the easiest way is to do it in C. Add a 4 byte variable to the top of your packet header.. When building the packet set it to 0.. That solves your problem of getting data into the packet.

But if you want to add something to the beginning, using NSMutableData, you have to first create the NSMutableData packet with 4 bytes of 0s then call [data appendBytes].. So something like so (not perfect)..

char bytes[4] = {0,0,0,0};

NSMutableData* data = [NSMutableData withBytes:bytes length:sizeof (bytes)];

[data appendBytes:&packet length:sizeOfPacket];

But i think you better way is to just put the 4 bytes at the top of your packet description. When creating you can set it to 0's (first call should be memset in your make packet call to set things to 0). If you are reading the packet from another location you can then put the pointer for the read or memcpy to the location of the first field. NSData is nice but i still think most memory management things (like creating and dealing with buffers) are better in C then thrown into an NSData if needed.. I generally don't use NSData as 1st class citizen for generating buffers.

Scott Andrew


On Jul 4, 2009, at 9:23 AM, Carlo Gulliani wrote:

thanks for your reply, could you show me simple example how to add binary data to existing data and also to add header of 2 bytes

i've been trying to use buffer but i have a troubles with result

my code:

#define PROTO_VERSION_MAJOR     1
#define PROTO_VERSION_MINOR     7
#define PROTO_VERSION ((((u_long)(PROTO_VERSION_MAJOR))<<16)|(u_long) (PROTO_VERSION_MINOR))


#define PROTO_MAJOR(p) (((p)&0xFFFF0000)>>16)
#define PROTO_MINOR(p) ((p)&0x0000FFFF)


typedef struct mrim_packet_header_t
{
   u_long      magic;
   u_long      proto;
   u_long      seq;
   u_long      msg;
   u_long      dlen;
   u_longfrom;
   u_longfromport;
   u_charreserved[16];
}
mrim_packet_header_t;

#define CS_MAGIC    0xDEADBEEF

#define MRIM_CS_HELLO       0x1001

structmrim_packet_header_tpacket;
packet = [self makePacket:MRIM_CS_HELLO length:0];
NSMutableData *data = [NSData dataWithBytes:&packet length:sizeof (packet)];
int c = 4;
char buf[c];
[data getBytes:buf];
int i;
for (i=20; i<[data length]+c; i++)
{
buf[i] = 0;
}
data = [NSData dataWithBytes:buf length:[data length]+c];
}

-(struct mrim_packet_header_t) makePacket:(unsigned long)key length: (unsigned long)len
{
staticunsignedlongseq = 2;
mrim_packet_header_tmrim_head;
//memset(&mrim_head, 0, sizeof (mrim_head));
mrim_head.magic = CS_MAGIC;
mrim_head.proto = PROTO_VERSION;
mrim_head.seq = seq;
mrim_head.msg = key;
mrim_head.dlen = len;
//mrim_head.from = h;
//mrim_head.fromport = 2041;
seq++;
   return mrim_head;
}

so, i found a dump of binary which must to result

0000 efbeadde 13000100 02000000 01100000 00100000 00000000 00000000 00000000 00000020 00000000 00000000 00000000

but i got

efbeadde 07000100 00000000 01100000 00000000 00000000 00000000 00286930 4cc40000 58e4ffbf 36c90091 00000000

my data is different of correct data, why? I use the same variables like there



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/scottandrew%40roadrunner.com

This email sent to scottand...@roadrunner.com

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to