# The Network Kit: BPacketHandler

Derived from: none

Declared in: be/addons/net_server/NetProtocol.h

Library: libnetdev.so

## Overview

When you register a protocol with the Net Server (by calling `register_packet_handler()`), you establish a connection between the Net Server and a packet handler object.

Your protocol add-on should include a class derived from BPacketHandler. An object of this class is then passed to `register_packet_handler()` to tell the Net Server how to dispatch packets to your protocol.

Once your packet handler has been registered with the Net Server, packets will be sent to your packet handler. The Net Server does this by calling the packet handler's `PacketReceived()` function:

          virtual bool PacketReceived(BNetPacket *packet, BNetDevice *device) = 0

Your `PacketReceived()` implementation can analyze the contents of the packet and the device on which the packet was received and decide whether or not the packet is one that your protocol will handle, returning `true` if the packet was handled and `false` if it was not. If you handle the packet (and return `true`), you must also delete the packet.

Let's look at a sample BPacketHandler-derived class:

       class QuickPacketHandler : public BPacketHandler {
          public:
             virtual bool PacketReceived(BNetPacket *packet,
                               BNetDevice *device);
             void ProcessPacket(BNetPacket *packet);
       }

       bool QuickPacketHandler::PacketReceived(BNetPacket *packet,
                            BNetDevice *device) {
          if (/* packet is one we want */) {
             ProcessPacket(packet);
             delete packet;
             return true;
          }
          return false;
       }

In this sample, we first check to see if the packet is one that our handler wants to receive. If it is, we process the packet in some way, delete the packet, and return `true`, which indicates to the Net Server that the packet has been handled. If we don't want the packet, we return `false`, so the Net Server knows to send the packet to the next protocol add-on.

## Member Functions

### PacketReceived()

          virtual bool PacketReceived(BNetPacket *packet, BNetDevice *device) = 0

Your derived class must define this function. The Net Server calls it when a packet is received by an interface with which your protocol has registered. If your protocol handles the packet, you should delete it and return `true`. If you don't accept the packet, return `false` so the Net Server knows to pass it along to another protocol.

The packet parameter points to the actual packet that has been received by your protocol add-on. The device parameter is a pointer to the device add-on that received the packet.

The Be Book, in lovely HTML, for BeOS Release 3.

Copyright © 1998 Be, Inc. All rights reserved.

Last modified March 26, 1998.