Programming - Software Engineering The Practice of Programming phần 9

bằng cách thêm mã số đặc biệt, thay vào đó, điều chỉnh phần mềm để làm việc trong các khó khăn mới. Sử dụng trừu tượng và đóng gói để hạn chế và kiểm soát mã di động không thể tránh khỏi. Bởi ở trong các giao điểm của khó khăn và địa hóa phụ thuộc hệ thống, mã của bạn sẽ trở nên sạch hơn và nói chung vì nó là chuyển. | SECTION FORMATTING DATA 219 Each pack-type routine will now be one line long marshaling its arguments into a call of pack pack-typel pack format 1 packet int pack_typel uchar abuf ushort count uchar val ulong data . . return pack buf cscl 0x01 count val data T o unpack we can do the same thing rather than write separate code to crack each packet format we call a single unpack with a format string. This centralizes the conversion in one place a unpack unpack packed items from buf return length int unpackfuchar abuf char afmt . va_li s t args char uchar abp pc ushort ps ulong p1 bp buf va_start args fmt for p fmt ap 0 P switch Op case c char pc va_arg args uchar pc bp break case s short ps va_arg args ushort ps bp 8 ps 1 bp break case 1 long pl vaargfargs ulong pl bp 24 apl 1 bp 16 p1 1 bp 8 pl 1 bp break default illegal type character va_end args return -1 va_endCargs return bp - buf 220 NOTATION CHAPTER 9 Like scanf unpack must return multiple values to its caller so its arguments are pointers to the variables where the results are to be stored. Its function value is the number of bytes in the packet which can be used for error checking. Because the values are unsigned and because we stayed within the sizes that ANSI C fines for the data types this code transfers data portably even between machines with different sizes for short and long. Provided the program that uses pack does not try to send as a long for example a value that cannot be represented in 32 bits the value will be received correctly. In effect we transfer the low 32 bits of the value. If we need to send larger values we could define another format. The type-specific unpacking routines that call unpack are easy unpack-type2 unpack and process type 2 packet int unpack_type2 int n uchar buf I uchar c ushort count ulong dwl dw2 if unpack buf csll c count dwl dw2 n return -1 assert c 0x02 return process_type2 count dwl dw2 I To call unpack_type2 we must first recognize that we have a type 2 packet. .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.