1. Trang chủ
  2. » Công Nghệ Thông Tin

iPhone Cool Projects phần 3 pps

23 137 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 23
Dung lượng 210,73 KB

Nội dung

[...]... typedef struct { uint32_t identifier; uint32_t datatype; } PacketHeader; Figure 2 -3 shows how this structure will look on the network 0 1 2 3 01 234 5678901 234 5678901 234 5678901 identifier datatype Figure 2 -3 The packet header structure as it will appear on the network The identifier field will always be the same for any SphereNet packet, and let’s define what it will contain: static const uint32_t kSphereNetPacketIdentifier... Figure 2-4 shows the original packet structure without the #pragma, and Figure 2-5 shows the corrected version 0 1 2 3 01 234 5678901 234 5678901 234 5678901 identifier datatype x y r g b padding Figure 2-4 The packet structure on the network with a typical compiler 0 1 2 3 01 234 5678901 234 5678901 234 5678901 identifier datatype x y r g b Figure 2-5 The layout of the packet structure with #pragma pack(1) enabled... application’s name 37 38 CHAPTER 2: Mike Ash’s Deep Dive Into Peer-to-Peer Networking If you haven’t seen this sort of thing before, this is just a multicharacter integer constant It’s conceptually similar to a standard character constant such as 'S', except that it builds a longer integer by stringing the characters together The constant 'SpHn' is simply a convenient way to write 0x 537 0486E or 139 9867502 The... works on an iPhone but could, at least in theory, work on anything We’ll also need to specify exactly how everything gets encoded in the network packets rather than simply using whatever format is the default for the platform While this may sound rather theoretical, it’s actually quite important even if the code never leaves the iPhone: the iPhone Simulator is different enough from a real iPhone that... then look like this: typedef struct { PacketHeader header; int32_t x; int32_t y; uint8_t r; uint8_t g; uint8_t b; } PositionPacket; First comes the header which we defined previously, followed by the coordinates, and then the three color components We’ll also need a datatype constant to identify this particular kind of packet: static const uint32_t kSphereNetPositionPacketType = 'posn'; One more refinement:... integers, which are easier to work with and, when it comes to debugging, easier to read when you’re poring over a hex dump of a packet The coordinates will just be 32 -bit signed integers This is a bit of overkill, as the iPhone screen is only 32 0 480, but it adds some future-proofing When it comes to the colors, there’s no point in using anything bigger than a single byte for each color component That... which the bytes of a multibyte integer are written Take the integer 30 5,419,896 for example In hexadecimal, this integer would be written out as 0x1 234 5678 The question is, how does it look in memory, for example, as an array of unsigned chars? One obvious way would be to simply write it down in order: unsigned char myInt[4] = { 0x12, 0x34, 0x56, 0x78 }; But it’s just as reasonable, albeit somewhat less... appropriate struct and fill out the header: - (void)sendUpdates { PositionPacket packet; packet.header.identifier = CFSwapInt32HostToBig( kSphereNetPacketIdentifier); packet.header.datatype = CFSwapInt32HostToBig( kSphereNetPositionPacketType); As you may have guessed, the calls to CFSwapInt32HostToBig() are due to endianness again This time we’re using a Core Foundation function to do the byte swapping instead... it down in the opposite order, with the least-significant byte first: unsigned char myInt[4] = { 0x78, 0x56, 0x34, 0x12 }; The former system is called big-endian, and the latter is called little-endian The Intel x86 CPU used in Macs these days is little-endian, as is the ARM CPU used in the iPhone The PowerPC processors used in older Macs are big-endian, and in general, it’s common to find either version... the world: middle endian! On some old, rare architectures, neither the forward nor the backward ordering was used, but rather a strange mixed-up ordering that would write the example integer of 30 5,419,896 as { 0x34, 0x12, 0x78, 0x56 } The problem of differing endianness is sometimes referred to as the “NUXI problem,” due to what happens when storing the string “UNIX” on some of these old systems Coding . uint32_t identifier; uint32_t datatype; } PacketHeader; Figure 2 -3 shows how this structure will look on the network. 0 01 234 5678901 234 5678901 234 5678901 1 23 identifier datatype Figure 2 -3. The. version. 0 01 234 5678901 234 5678901 234 5678901 1 23 identifier datatype x y r g b padding Figure 2-4. The packet structure on the network with a typical compiler 0 01 234 5678901 234 5678901 234 5678901 1 23 identifier datatype x y rgb Figure. over a hex dump of a packet. The coordinates will just be 32 -bit signed integers. This is a bit of overkill, as the iPhone screen is only 32 0480, but it adds some future-proofing. When it comes

Ngày đăng: 13/08/2014, 18:20