开发者

How to define a communication protocol?

I'm new to networking concepts and need an explaination of how to implement a communication protocol for sending different types of messages. I'm currently working on a Cocoa app that will send video messages between iPhones. Currently I only send messages of type 3. Here's the app flow I need to implement:

  1. Browsing for available iPhones on the network (using Bonjour)

  2. 开发者_如何学Go
  3. When an iPhone client is found, send NSData "request contact info" (MessageType1)

    iPhone client will send back an NSData instance with contact info (MessageType2)

  4. Init a new message with recorded video, send to selected contact (MessageType3)

When the different types of message are received, they will need to be handled differently. I guess one way to solve it is to add a header to the message that identify the message type and extract this on the receiver's side, then handle like this:

if (messageType == 1)  // MessageType1
    [self sendMyContactInfo:(Contact *)ownInfo];

if (messageType == 2)  // MessageType2
    [self updateViewWithContactInfo:(Contact *)contactInfo];

if (messageType == 3)  // MessageType3
    [self sendMessageToSelectedContact:(Message *)message]

For creating a message for MessageType3, I'll do this:

/* Not currently implemented */
NSMutableData *data = [[NSMutableData alloc] init];
int messageType = 3;
[data appendBytes:messageType]

/* Already Implemented */
NSData *encodedMessage = [NSKeyedArchiver archivedDataWithRootObject:message];
[data appendData:encodedMessage];

[self sendMessage:(NSData *)encodedMessage];

Is this a nice way of doing it? If so, should the protocol rules be defined in a more formal way, e.g. in a separate class or something? I'm looking for the best overall solution here, so don't take too much notice of my drawings if there's a better way to do it...


Is this a nice way of doing it?

It's a standard way for defining a communications protocol. From the Wikipedia article:

Digital message bitstrings are exchanged. The bitstrings are divided in fields and each field carries information relevant to the protocol. Conceptually the bitstring is divided into two parts called the header area and the data area. The actual message is stored in the data area, so the header area contains the fields with more relevance to the protocol. The transmissions are limited in size, because the number of transmission errors is proportional to the size of the bitstrings being sent. Bitstrings longer than the maximum transmission unit (MTU) are divided in pieces of appropriate size. Each piece has almost the same header area contents, because only some fields are dependent on the contents of the data area (notably CRC fields, containing checksums that are calculated from the data area contents).

End Wikipedia quote

If so, should the protocol rules be defined in a more formal way, e.g. in a separate class or something?

That's up to you. It's not necessary, since your application is communicating with other copies of your application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜