Bonjour Networking Help for iPhone WiTap

I can follow most of the Apple WiTap examples, but I'm a bit stuck on this bit in the send method:

- (void) send:(const uint8_t)message
{
if (_outStream && [_outStream hasSpaceAvailable])
    if([_outStream write:(const uint8_t *)&message maxLength:sizeof(const uint8_t)] == -1)
        [self _showAlert:@"Failed sending data to peer"];
 }

 - (void) activateView:(TapView*)view
{
    NSLog(@"ACTIVATE TAG: %d", [view tag]); 
    //[self send:[view tag] | 0x80];
    [self send:[view tag]];
 }

 - (void) deactivateView:(TapView*)view
 {
    NSLog(@"DEACTIVATE TAG: %d", [view tag]); 
    //[self send:[view tag] & 0x7f];
    [self send:[view tag]];

 }

      

Note that I have changed the send argument: only the tag of the views, which are numbered 1-9. The code originally had AND and OR bitwise adjustments.

Why?

I get the fact that the dispatch method is required uint8_t

, but is it therefore bitwise stuff? To enable NSInteger in unint8_t?

The code doesn't work with my changes above. It will be recorded correctly and visually that the client will work correctly, but messages are not being sent / received correctly from client to client.

Can someone explain in little words what the bitwise stuff does? Or am I right?

Thank! This is my first SO question, so please be kind.


thanks for the answer. I'm still a little confused. Get it?

Basically why?

Is this just an ugly way to pass an ID? Each of these views has a # tag, why not just pass that and switch state (up / down) from the view class?

Is this just an example of how the person who wrote this did it or am I missing a key piece of the puzzle since I have to structure my code as well.

I would just like to pass in a # tag and then that tag decides what to do in a clearly readable function, like toggleUpOrDownState

or something.

This bitwise stuff always makes me feel stupid, I guess if it's not necessary, etc. Then I feel stupid, but I can still get confused. :)

0


source to share


3 answers


Basically, [view tag] | 0x80

sets the bit high on that value (so 00010011 becomes 10010011) and [view tag] & 0x7f

removes it (10010011 → 00010011).

Take a look at the method [AppController stream:handleEvent:]

. You will see this code:



        //We received a remote tap update, forward it to the appropriate view
    if(b & 0x80)
        [(TapView*)[_window viewWithTag:b & 0x7f] touchDown:YES];
    else
        [(TapView*)[_window viewWithTag:b] touchUp:YES];

      

So the stream receiver checks for this high bit.

+3


source


I believe the reason is that the most significant bit is another piece of data transferred from one peer to another. It indicates whether the message is the "start" of the tap or the "end" of the tap.

Based on this bit, the receiver enables the display or disables it.



So, in essence, they put two pieces of information into one single unsigned integer - which square was punched out (low-order bits) and was a touch of a start or end (high bit).

0


source


I think the messages can be dropped or come in a different order. Therefore, if you do not say that it is a tap or press, it is possible that the toggle will be reset.

0


source







All Articles