/// <summary> /// Handle an incoming byte, adding it to any in-progress Packet /// </summary> /// <param name="byteIn">The byte to process</param> public void processByte(byte byteIn) { _currentPacket[_currentPacketPos] = byteIn; _currentPacketPos++; // We need to keep count of how many consecutive sync bytes we have seen, since if we // are out of sync they will not appear to be in the same packet. // For example, with a sync token of 0xAA: // Packet 1: 00 AA AA AA AA AA AA AA // Packet 2: AA .. if (byteIn == _synchSymbol) { // OK, We've got another sync byte.. _consecutiveSyncBytes++; // If we have an entire sync symbol then we need to rewind to the start of our packet // buffer, ready to accept new bytes form the start of the packet. _currentPacketPos = 0; syncPacket(); } else { _consecutiveSyncBytes = 0; // If we have read an entire packet, then we need to parse the packet and process it. if (_currentPacketPos == networkPacket.lengthInBytes) { networkPacket packet = new networkPacket(_currentPacket); processPacket(packet); _currentPacketPos = 0; } } }
protected networkPacket(networkPacket newPacket) { commonConstructorStuff(newPacket.rawBytes); }
public commandPacket(networkPacket packet) : base(packet) { }
public doIdentifyRequestPacket(networkPacket packet) : base(packet) { }
/// <summary> /// Called when we need to send a networkPacket to a child node. /// </summary> /// <param name="toSend"></param> private new void sendPacket(networkPacket toSend) { base.sendPacket(toSend); lock (_pipe) { toSend.writeToPipe(_pipe); } }
/// <summary> /// Fire the onSendPacket event /// </summary> /// <param name="toSend"></param> protected void sendPacket(networkPacket toSend) { if (onSendPacket != null) onSendPacket(toSend); }
public initialChallengePacket(networkPacket packet) : base(packet) { }
/// <summary> /// Fire the onSendPacket event /// </summary> /// <param name="toSend"></param> protected void sendPacket(networkPacket toSend) { log(logLevel.info, "Controller sending packet: " + toSend.ToString()); if (onSendPacket != null) onSendPacket.Invoke(); }
private void processCommandPacket(networkPacket packet) { // We received a command packet. Authenticate it and then process it. commandPacket cmdPkt = new commandPacket(packet); log("Packet is a command packet. Decoded packet:"); log(cmdPkt.ToString()); // Verify the sequence number if (cmdPkt.challengeResponse != p + 1) { // Bad auth! drop the packet and reset to our first state. log("A crypto error has occurred"); cryptoError(); stateChange(nodeState.idle); return; } switch (cmdPkt.findCommandByteMeaning()) { case commandByte.unknown: log("Ignoring unknown command"); break; case commandByte.ping: processPing(cmdPkt); break; case commandByte.identify: processIdentify(cmdPkt); break; case commandByte.getSensor: processGetSensor(cmdPkt); break; case commandByte.setSensor: processSetSensor(cmdPkt); break; case commandByte.getSensorType: processGetSensorType(cmdPkt); break; default: throw new ArgumentOutOfRangeException(); } }
/// <summary> /// Handle an incoming networkPacket /// </summary> /// <param name="packet"></param> private void processPacket(networkPacket packet) { if (packet.destinationNodeID != id) return; switch (state) { case nodeState.idle: processFirstPacket(packet); break; case nodeState.firstHandshakeInProgress: processCommandPacket(packet); break; default: throw new ArgumentException(); } }
private void processFirstPacket(networkPacket packet) { // We have just got the first packet of our handshake. We should inc the sequence number, and place // our challenge P in the data payload, and send the challenge back to the controller. initialChallengePacket chal = new initialChallengePacket(packet); log("Packet is initial challenge. Decoded packet:"); log(chal.ToString()); initialChallengeResponsePacket resp = new initialChallengeResponsePacket(CSharpNetwork.controllerID, chal.challenge + 1, p); log("Sending response:"); log(resp.ToString()); sendPacket(resp); stateChange( nodeState.firstHandshakeInProgress ); }