Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added ObexListenerContext(Stream rs) for Android file transfer #356

Open
wants to merge 2 commits into
base: obex-development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions InTheHand.Net.Obex/ObexHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// InTheHand.Net.ObexHeader
//
// Copyright (c) 2003-2023 In The Hand Ltd, All rights reserved.
// Copyright (c) 2003-2024 In The Hand Ltd, All rights reserved.
// This source code is licensed under the MIT License

using System;
Expand Down Expand Up @@ -36,7 +36,7 @@ public enum ObexHeader : byte
Type = 0x42,

/// <summary>
/// Date/time stamp ISO 8601 version - preferred
/// Date/time stamp ISO 8601 version - preferred
/// </summary>
/// <remarks>Byte sequence. ASCII encoded ISO 8601 DateTime string.</remarks>
Time = 0x44,
Expand Down Expand Up @@ -132,7 +132,7 @@ public enum ObexHeader : byte
Length = 0xC3,

/// <summary>
/// Date/time stamp 4-byte version.
/// Date/time stamp 4-byte version.
/// </summary>
/// <remarks>4-byte unsigned integer.</remarks>
[Obsolete("Use ObexHeader.Time instead", false)]
Expand Down
132 changes: 132 additions & 0 deletions InTheHand.Net.Obex/ObexListenerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class ObexListenerContext
private MemoryStream bodyStream = new MemoryStream();
private EndPoint localEndPoint;
private EndPoint remoteEndPoint;
private string _path;
ushort remoteMaxPacket = 0;

internal ObexListenerContext(Socket s)
Expand Down Expand Up @@ -133,7 +134,138 @@ internal ObexListenerContext(Socket s)
request = new ObexListenerRequest(bodyStream.ToArray(), headers, localEndPoint, remoteEndPoint);

}
#region David Rodgers added
// David Rodgers - Added
// Uses stream communication rather than socket for compatability with Android
// public allows bypassing ObexListener and using BluetoothListener directly for using custom service id for app to app transfer
// TODO - extend ObexListener to handle custom service id's
public ObexListenerContext(Stream rs)
{

buffer = new byte[0x2000];
bool moretoreceive = true;
bool putCompleted = false;

while (moretoreceive)
{
//receive the request and store the data for the request object
int received = 0;

try
{

while (received < 3)
{
//int readLen = s.Receive(buffer, received, 3 - received, SocketFlags.None);
int readLen = rs.Read(buffer, received, 3 - received);
if (readLen == 0)
{
moretoreceive = false;
if (received == 0)
{
break; // Waiting for first byte of packet -- OK to close then.
}
else
{
throw new EndOfStreamException("Connection lost.");
}
}
received += readLen;
}
//Debug.WriteLine(s.GetHashCode().ToString("X8") + ": RecvH", "ObexListener");
}
catch (Exception se)
{
//Console.Write(se.Message);
HandleConnectionError(se);
}

if (received == 3)
{

ObexMethod method = (ObexMethod)buffer[0];
//get length (excluding the 3 byte header)

short len = (short)(IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, 1)) - 3);
if (len > 0)
{
int iPos = 0;

while (iPos < len)
{
int wanted = len - iPos;
Debug.Assert(wanted > 0, "NOT wanted > 0, is: " + wanted);
int receivedBytes = rs.Read(buffer, iPos + 3, wanted);
if (receivedBytes == 0)
{
moretoreceive = false;
throw new EndOfStreamException("Connection lost.");
}
iPos += receivedBytes;
}
}

byte[] responsePacket; // Don't init, then the compiler will check that it's set below.
//Debug.WriteLine(s.GetHashCode().ToString("X8") + ": Method: " + method, "ObexListener");
responsePacket = HandleAndMakeResponse(ref moretoreceive, ref putCompleted, method);

try
{
System.Diagnostics.Debug.Assert(responsePacket != null, "Must always respond to the peer.");
if (responsePacket != null)
{
rs.Write(responsePacket, 0, responsePacket.Length);
rs.Flush();

}

}
catch (Exception se)
{
//Console.WriteLine(se.Message);
HandleConnectionError(se);
}
}
else
{
moretoreceive = false;
}

}//while

Debug.WriteLine(rs.GetHashCode().ToString("X8") + ": Completed", "ObexListener");
rs.Close();
rs = null;

if (!putCompleted)
{
// Should not return the request.
throw new ProtocolViolationException("No PutFinal received.");
}
request = new ObexListenerRequest(bodyStream.ToArray(), headers, localEndPoint, remoteEndPoint);
_path = headers["NAME"];
}

// David Rodgers added
// retrieve the Md5 checksum generated by the sender from the headers
public string getSentMD5()
DavidR5 marked this conversation as resolved.
Show resolved Hide resolved
{
return headers["ContentMd5"];
}

/// <summary>
/// Gets the filename of the received file from header
/// </summary>
// David Rodgers added
//
public string getPath
DavidR5 marked this conversation as resolved.
Show resolved Hide resolved
{
get
{
return _path;
}
}
#endregion
private byte[] HandleAndMakeResponse(ref bool moretoreceive, ref bool putCompleted, ObexMethod method)
{
byte[] responsePacket;
Expand Down
12 changes: 10 additions & 2 deletions InTheHand.Net.Obex/ObexWebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// InTheHand.Net.ObexWebRequest
//
// Copyright (c) 2003-2023 In The Hand Ltd, All rights reserved.
// Copyright (c) 2003-2024 In The Hand Ltd, All rights reserved.
// This source code is licensed under the MIT License

using System;
Expand Down Expand Up @@ -1056,8 +1056,16 @@ public override WebResponse GetResponse()
ObexStatusCode status;
MemoryStream ms = new MemoryStream();
WebHeaderCollection responseHeaders = new WebHeaderCollection();
// Added David Rodgers to facilitate sending an Md5 checksum in the header
// could be expanded to loop through Headers and add any headers that are set by the user
if (Headers["ContentMd5"] != null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to add the existing request header here? - because the responseHeaders should contain the headers as sent by the remote device along with the retrieved file.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When sending the file I set the user defined header request.Headers.Add("ContentMd5", md5); ("ContentMd5" would change to "User0" with your new changes) before calling request.GetResponse(); The call to GetResponse() then creates responseHeaders as a new WebHeaderCollection() instance effectively wiping out the previously set user defined header. Other headers are added in ObexWebRequest:DoPut(); With your new user defined headers where would we set them when sending a file?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will wait until I find out how to set a user defined header so I can implement and test before resubmitting.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have not commented about public ObexListenerContext(Stream rs) with a stream parameter instead of socket, what are your thoughts about it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I've been distracted and forgot to follow up on this. Now that the listener can work from either a stream or a socket, I don't think the public constructor is needed because this can be created through the normal listener flow.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see where you test for socket == null in ObexListenerContext at line 257 and assign stream on line 259 but this is not used. Line 283 says "// call to stream implementation to go here" so there is no ObexListenerContext returned. So this does not seem to be any further ahead with regard to getting the ObexListenerContext "through the normal listener flow". My other issue is that I have to use BluetoothListener rather than ObexListener because I need to set a custom service GUID. It would be great if we could pass a custom GUID to the BluetoothListener constructor. This is why I created the public ObexListenerContext constructor which took a stream argument. I want to use ObexListenerContext but also need a custom service GUID.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, then it should expose the public constructor for those cases. There is definitely scope for more flexibility in the listener so it can support custom services

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will you be making changes to support custom services and when would you expect to include them?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think based on time available to this at the moment we assume that it won't happen soon. It'll possibly require work to both the BluetoothListener and ObexListener to fully support.

{
responseHeaders.Add("ContentMd5", Headers["ContentMd5"]);
}
// end Added David Rodgers
// try connecting if not already
try {
try
{
status = Connect();
Debug.Assert(status == ObexStatus_OK, "connect was: " + status);
} catch (Exception se) {
Expand Down