Exemple #1
0
        /// <summary>
        /// This function serves command event streams.
        /// </summary>
        /// <param name="s"></param>
        void ServeCommand(NetworkStream inStream, NetworkStream outStream)
        {
            while (true)
            {
                int            command    = ReadInt(inStream);
                TcpTpmCommands theCommand = (TcpTpmCommands)command;
                //Console.WriteLine("command " + theCommand.ToString());
                if (theCommand == TcpTpmCommands.RemoteHandshake)
                {
                    int clientVersion = ReadInt(inStream);
                    if (clientVersion == 0)
                    {
                        throw new Exception("Incompatible client (version 0, expected version 1 or higher)");
                    }
                }
                if (TheDeviceType == DeviceType.Tcp)
                {
                    WriteInt((int)theCommand, outStream);
                    switch (theCommand)
                    {
                    case TcpTpmCommands.SignalHashStart:
                    case TcpTpmCommands.SignalHashEnd:
                        break;

                    case TcpTpmCommands.SendCommand:
                        byte[]          loc    = Read(1, inStream);
                        byte[]          cmd    = ReadVarArray(inStream);
                        CommandModifier active = new CommandModifier();
                        Write(loc, outStream);
                        WriteVarArray(cmd, outStream);

                        WriteVarArray(ReadVarArray(outStream), inStream);
                        break;

                    case TcpTpmCommands.SignalHashData:
                        byte[] data = ReadVarArray(inStream);
                        WriteVarArray(data, outStream);
                        break;

                    case TcpTpmCommands.RemoteHandshake:
                        WriteInt(Version, outStream);
                        int endPointVersion = ReadInt(outStream);
                        if (endPointVersion == 0)
                        {
                            throw new Exception("Incompatible end point (version 0, expected version 1 or higher)");
                        }
                        tpmEndPointInfo = ReadInt(outStream);
                        WriteInt(Version, inStream);
                        WriteInt(tpmEndPointInfo, inStream);
                        break;

                    case TcpTpmCommands.SessionEnd:
                    case TcpTpmCommands.Stop:
                        if (TheDeviceType == DeviceType.Tcp)
                        {
                            // Forward session end request and do not wait for ACK
                            WriteInt((int)theCommand, outStream);
                            //GetAndSendAck(outStream, inStream);
                        }
                        // Send back ACK and exit the communication loop
                        Write(ZeroInt, inStream);
                        return;

                    default:
                        throw new NotImplementedException("");
                    }
                    GetAndSendAck(outStream, inStream);
                }
                else // TBS device
                {
                    switch (theCommand)
                    {
                    case TcpTpmCommands.SignalHashStart:
                    case TcpTpmCommands.SignalHashEnd:
                        NotImplemented(theCommand);
                        Write(OneInt, inStream);
                        break;

                    case TcpTpmCommands.SignalHashData:
                        byte[] data = ReadVarArray(inStream);
                        NotImplemented(theCommand);
                        Write(OneInt, inStream);
                        break;

                    case TcpTpmCommands.SendCommand:
                        byte[]          loc    = Read(1, inStream);
                        byte[]          cmd    = ReadVarArray(inStream);
                        CommandModifier active = new CommandModifier();
                        byte[]          outBuf;

                        tbsDevice.DispatchCommand(active, cmd, out outBuf);

                        WriteVarArray(outBuf, inStream);
                        Write(ZeroInt, inStream);
                        break;

                    case TcpTpmCommands.RemoteHandshake:
                        WriteInt(Version, inStream);
                        WriteInt(tpmEndPointInfo, inStream);
                        Write(ZeroInt, inStream);
                        break;

                    case TcpTpmCommands.SessionEnd:
                    case TcpTpmCommands.Stop:
                        // Send back ACK and exit the communication loop
                        Write(ZeroInt, inStream);
                        return;

                    default:
                        throw new NotImplementedException("");
                    }
                }
            }
        }