📄 obexlistenerrequest.cs
字号:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using bluetoothX;
namespace bluetoothX
{
public class ObexListenerRequest
{
private byte[] input;
private WebHeaderCollection headers;
private EndPoint localEndPoint;
private EndPoint remoteEndPoint;
private Uri uri;
#region Constructor
internal ObexListenerRequest(byte[] input, WebHeaderCollection headers, EndPoint localEndPoint, EndPoint remoteEndPoint)
{
this.input = input;
this.headers = headers;
this.localEndPoint = localEndPoint;
this.remoteEndPoint = remoteEndPoint;
}
#endregion
#region Content Length 64
public long ContentLength64
{
get
{
string len = headers["LENGTH"];
if(len!=null&&len!="")
{
return long.Parse(len);
}
return -1;
}
}
#endregion
#region Content Type
public string ContentType
{
get
{
return headers["TYPE"];
}
}
#endregion
#region Headers
public WebHeaderCollection Headers
{
get
{
return headers;
}
}
#endregion
#region Local Endpoint
public EndPoint LocalEndPoint
{
get
{
return localEndPoint;
}
}
#endregion
#region Method
public string ObexMethod
{
get
{
return "PUT";
}
}
#endregion
#region Input Stream
public Stream InputStream
{
get
{
return new MemoryStream(this.input);
}
}
#endregion
#region Protocol Version
public Version ProtocolVersion
{
get
{
return new Version(1,0);
}
}
#endregion
#region Raw Url
public string RawUrl
{
get
{
return Url.PathAndQuery;
}
}
#endregion
#region Remote End Point
public EndPoint RemoteEndPoint
{
get
{
return remoteEndPoint;
}
}
#endregion
#region User Host Address
public string UserHostAddress
{
get
{
if(localEndPoint is BluetoothEndPoint)
{
BluetoothEndPoint bep = localEndPoint as BluetoothEndPoint;
return bep.Address.ToString("P");
}
else if(localEndPoint is IPEndPoint)
{
IPEndPoint ipep = localEndPoint as IPEndPoint;
return ipep.Address.ToString() + ":" + ipep.Port;
}
return "";
}
}
#endregion
#region Url
public Uri Url
{
get
{
if(uri==null)
{
string address = null;
if(this.localEndPoint is BluetoothEndPoint)
{
address = ((BluetoothEndPoint)this.localEndPoint).Address.ToString();
}
uri = new Uri("obex-push://" + address + "/" + headers["NAME"]);
}
return uri;
}
}
#endregion
#region Write File
public void WriteFile(string fileName)
{
System.IO.FileStream fs = System.IO.File.Create(fileName);
System.IO.MemoryStream ms = new MemoryStream(input);
int bytesToRead = (int)this.ContentLength64;
int bytesRead = 0;
if(bytesToRead == -1)
{
bytesToRead = input.Length;
}
int read;
byte[] buffer = new byte[1024];
do
{
read = ms.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, read);
bytesRead += read;
}while(bytesRead < bytesToRead);
ms.Close();
fs.Close();
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -