Beispiel #1
0
        public void StartServer()
        {
            Task.Run(async() =>
            {
                while (true)
                {
                    _cts.Token.ThrowIfCancellationRequested();

                    ObexPacket packet = await ObexPacket.ReadFromStream(_reader, new ObexConnectPacket());
                    if (packet.Opcode == Opcode.Connect)
                    {
                        if (packet.Headers.ContainsKey(HeaderId.Target))
                        {
                            BytesHeader targetHeader = (BytesHeader)packet.Headers[HeaderId.Target];
                            if (targetHeader.Value != null)
                            {
                                if (Enumerable.SequenceEqual(targetHeader.Value, _serviceUuid.Value))
                                {
                                    packet.Opcode = Opcode.Success;
                                    _writer.WriteBuffer(packet.ToBuffer());
                                    await _writer.StoreAsync();
                                    break;
                                }
                            }
                        }
                    }

                    Console.WriteLine("Not support operation code: " + packet.Opcode);
                    Console.WriteLine("MSE should send Connect request first");
                    packet = new ObexPacket(Opcode.OBEX_UNAVAILABLE);
                    _writer.WriteBuffer(packet.ToBuffer());
                }

                while (true)
                {
                    _cts.Token.ThrowIfCancellationRequested();

                    ObexPacket packet = await ObexPacket.ReadFromStream(_reader);

                    ObexPacket?response = OnClientRequest(packet);
                    if (response != null)
                    {
                        _writer.WriteBuffer(response.ToBuffer());
                        await _writer.StoreAsync();
                    }
                    else
                    {
                        _writer.WriteByte(0xC6); // Not Acceptable
                        _writer.WriteUInt16(3);
                        await _writer.StoreAsync();
                    }
                }
            }, _cts.Token);
        }
Beispiel #2
0
        public static IObexHeader ObexHeaderFromByte(byte b)
        {
            HeaderId    headerId = (HeaderId)b;
            IObexHeader header;

            switch (headerId)
            {
            case HeaderId.ConnectionId:
            case HeaderId.SingleResponseMode:
                header = new Int32ValueHeader(headerId);
                break;

            case HeaderId.ApplicationParameters:
                header = new AppParamHeader();
                break;

            case HeaderId.Type:
            case HeaderId.Name:
                header = new Utf8StringValueHeader(headerId);
                break;

            case HeaderId.EndOfBody:
            case HeaderId.Body:
                header = new BodyHeader(headerId);
                break;

            case HeaderId.Who:
            case HeaderId.Target:
                header = new BytesHeader(headerId);
                break;

            default:
                //throw new NotSupportedException($"Input byte '{b}' does not match HeaderId definition. ");
                Console.WriteLine($"Input byte '{b}' does not match HeaderId definition. ");
                header = new AsciiStringValueHeader(headerId);
                break;
            }

            return(header);
        }