void bg_send_DoWork(object sender, DoWorkEventArgs e) { Device device = (Device)e.Argument; ObexStatusCode response_status = Module.SendFile(device.DeviceInfo, FilePath); e.Result = response_status; }
void bgSendApp_DoWork(object sender, DoWorkEventArgs e) { Device device = (Device)e.Argument; ObexStatusCode response_status = SendModule.SendFile(device.DeviceInfo, Path.GetFullPath("Mock-A-Holy.apk")); e.Result = (response_status != ObexStatusCode.InternalServerError ? device : null); }
void bg_send_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Result != null) { ObexStatusCode status = (ObexStatusCode)e.Result; MessageBox.Show("Status pliku: - " + status.ToString(), "Informacja"); } }
private byte[] HandleAndMakeResponse(ref bool moretoreceive, ref bool putCompleted, ObexMethod method) { byte[] responsePacket; switch (method) { case ObexMethod.Connect: ObexParser.ParseHeaders(buffer, true, ref remoteMaxPacket, bodyStream, headers); responsePacket = new byte[7] { 0xA0, 0x00, 0x07, 0x10, 0x00, 0x20, 0x00 }; break; case ObexMethod.Put: if (putCompleted) // Don't allow another PUT to append to the previous content! { goto case ObexMethod.PutFinal; } ObexParser.ParseHeaders(buffer, false, ref remoteMaxPacket, bodyStream, headers); responsePacket = new byte[3] { (byte)(ObexStatusCode.Continue | ObexStatusCode.Final), 0x00, 0x03 }; break; case ObexMethod.PutFinal: if (putCompleted) // Don't allow another PUT to append to the previous content! { ObexStatusCode code = ObexStatusCode.Forbidden; // Any better one? responsePacket = new byte[3] { (byte)(code | ObexStatusCode.Final), 0x00, 0x03 }; moretoreceive = false; break; } ObexParser.ParseHeaders(buffer, false, ref remoteMaxPacket, bodyStream, headers); responsePacket = new byte[3] { (byte)(ObexStatusCode.OK | ObexStatusCode.Final), 0x00, 0x03 }; // Shouldn't return an object if the sender didn't send it all. putCompleted = true; // (Need to just assume that it does contains EndOfBody) break; case ObexMethod.Disconnect: ObexParser.ParseHeaders(buffer, false, ref remoteMaxPacket, bodyStream, headers); responsePacket = new byte[3] { (byte)(ObexStatusCode.OK | ObexStatusCode.Final), 0x00, 0x03 }; moretoreceive = false; break; default: //Console.WriteLine(method.ToString() + " " + received.ToString()); responsePacket = new byte[3] { (byte)ObexStatusCode.NotImplemented, 0x00, 0x03 }; break; } return(responsePacket); }
private bool CheckResponse(ref ObexStatusCode status, bool isConnectResponse) { if (isConnectResponse) { throw new ArgumentException("CheckResponse does not know how to parse the connect response"); } byte[] receiveBuffer = new byte[3]; StreamReadBlockMust(ns, receiveBuffer, 0, receiveBuffer.Length); status = (ObexStatusCode)receiveBuffer[0]; switch (status) { case ObexStatusCode.Final | ObexStatusCode.OK: case ObexStatusCode.OK: case ObexStatusCode.Final | ObexStatusCode.Continue: case ObexStatusCode.Continue: //get length short len = (short)(IPAddress.NetworkToHostOrder(BitConverter.ToInt16(receiveBuffer, 1)) - 3); Debug.Assert(len >= 0, "not got len!"); if (len > 0) { byte[] receivePacket2 = new byte[len]; StreamReadBlockMust(ns, receivePacket2, 0, len); if (len == 5 && ((ObexHeader)receivePacket2[0] == ObexHeader.ConnectionID)) { // ignore ConnectionId } else { Debug.Fail("unused headers..."); } } return(true); default: return(false); } //handle errors please! }
private void DisconnectIgnoreIOErrors() { if (ns != null) { ObexStatusCode status = 0; short disconnectPacketSize = 3; byte[] disconnectPacket = new byte[8]; disconnectPacket[0] = (byte)ObexMethod.Disconnect; //add connectionid header if (connectionId != 0) { disconnectPacket[3] = (byte)ObexHeader.ConnectionID; BitConverter.GetBytes(IPAddress.HostToNetworkOrder(connectionId)).CopyTo(disconnectPacket, 4); disconnectPacketSize += 5; } //set packet size BitConverter.GetBytes(IPAddress.HostToNetworkOrder(disconnectPacketSize)).CopyTo(disconnectPacket, 1); ns.Write(disconnectPacket, 0, disconnectPacketSize); try { CheckResponse(ref status, false); } catch (EndOfStreamException) { // If the server has closed the connection already we // normally see EoF on reading for the (first byte of the) // response to our Disconnect request. } catch (IOException) { // Other exceptions are possible, // e.g. on _writing_ the Disconnect request. } ns.Close(); } }
private bool CheckResponse(ref ObexStatusCode status, bool isConnectResponse) { if (isConnectResponse) { throw new ArgumentException("CheckResponse does not know how to parse the connect response"); } byte[] receiveBuffer = new byte[3]; StreamReadBlockMust(ns, receiveBuffer, 0, receiveBuffer.Length); status = (ObexStatusCode)receiveBuffer[0]; switch (status) { case ObexStatusCode.Final | ObexStatusCode.OK: case ObexStatusCode.OK: case ObexStatusCode.Final | ObexStatusCode.Continue: case ObexStatusCode.Continue: //get length short len = (short)(IPAddress.NetworkToHostOrder(BitConverter.ToInt16(receiveBuffer, 1)) - 3); Debug.Assert(len >= 0, "not got len!"); if (len > 0) { byte[] receivePacket2 = new byte[len]; StreamReadBlockMust(ns, receivePacket2, 0, len); bool validObexHeader = false; if ((ObexHeader)receivePacket2[0] == ObexHeader.ConnectionID) { if (len == 5) { // ignore ConnectionId validObexHeader = true; } else if (len >= 8 && ((ObexHeader)receivePacket2[5] == ObexHeader.EndOfBody)) { // End of Body Header short eobLen = (short)(IPAddress.NetworkToHostOrder(BitConverter.ToInt16(receivePacket2, 6))); Debug.Assert(len >= 3, "not got eobLen!"); // ex. len = 8, eobLen = 3 if (eobLen != (len - 5)) { // receive packet length and End-of-Body length are mismatch Debug.Fail("invalid packet length..."); // return false; } // ignore ConnectionId and EndOfBody validObexHeader = true; } } if (!validObexHeader) { Debug.Fail("unused headers..."); } } return(true); default: return(false); } //handle errors please! }
private ObexStatusCode DoPut() { ObexStatusCode status = 0; byte[] buffer = new byte[remoteMaxPacket]; string filename = uri.PathAndQuery; if (!uri.UserEscaped) { #if !(WinCE && V1) // This is a NOP if there's no %xx encodings present. filename = Uri.UnescapeDataString(filename); #else // HACK ObexWebRequest -- No Unescape method on NETCFv1!! #endif } filename = filename.TrimStart(new char[] { '/' }); int filenameLength = (filename.Length + 1) * 2; int packetLength = 3; buffer[0] = (byte)ObexMethod.Put; if (connectionId != 0) { buffer[packetLength] = (byte)ObexHeader.ConnectionID; byte[] raw = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(connectionId)); raw.CopyTo(buffer, packetLength + 1); packetLength += 5; } buffer[packetLength] = (byte)ObexHeader.Name; BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)(filenameLength + 3))).CopyTo(buffer, packetLength + 1); System.Text.Encoding.BigEndianUnicode.GetBytes(filename).CopyTo(buffer, packetLength + 3); packetLength += (3 + filenameLength); string contentType = headers["TYPE"]; if (contentType != null && contentType != "") { int contentTypeLength = (contentType.Length + 1);// *2; buffer[packetLength] = (byte)ObexHeader.Type; BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)(contentTypeLength + 3))).CopyTo(buffer, packetLength + 1); System.Text.Encoding.ASCII.GetBytes(contentType).CopyTo(buffer, packetLength + 3); packetLength += (3 + contentTypeLength); } if (this.ContentLength != 0) { buffer[packetLength] = (byte)ObexHeader.Length; BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Convert.ToInt32(this.ContentLength))).CopyTo(buffer, packetLength + 1); packetLength += 5; } //write the final packet size BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)packetLength)).CopyTo(buffer, 1); //send packet with name header ns.Write(buffer, 0, packetLength); if (CheckResponse(ref status, false)) { int totalBytes = 0; byte[] requestBuffer = requestStream.GetBuffer(); //we really want the content length, but if not available send the whole buffer if (this.ContentLength > 0) { totalBytes = (int)this.ContentLength; } else { totalBytes = requestBuffer.Length; } MemoryStream readBuffer = new MemoryStream(requestBuffer); while (totalBytes > 0) { int packetLen = AddBody(buffer, 3, ref totalBytes, readBuffer); ns.Write(buffer, 0, packetLen); if (!CheckResponse(ref status, false)) { return(status); } } } return(status); }
private ObexStatusCode DoPut() { ObexStatusCode status = 0; byte[] buffer = new byte[remoteMaxPacket]; string filename = uri.PathAndQuery; if (!uri.UserEscaped) { // This is a NOP if there's no %xx encodings present. filename = Uri.UnescapeDataString(filename); } //name filename = filename.TrimStart('/'); string[] segments = filename.Split('/'); if (segments.Length > 1) { SetPath("/"); for (int i = 0; i < segments.Length - 1; i++) { SetPath(segments[i]); } filename = segments[segments.Length - 1]; } int filenameLength = (filename.Length + 1) * 2; int packetLength = 3; buffer[0] = (byte)ObexMethod.Put; if (connectionId != INVALID_CONNECTION_ID) { buffer[packetLength] = (byte)ObexHeader.ConnectionID; byte[] raw = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(connectionId)); raw.CopyTo(buffer, packetLength + 1); packetLength += 5; } buffer[packetLength] = (byte)ObexHeader.Name; BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)(filenameLength + 3))).CopyTo(buffer, packetLength + 1); System.Text.Encoding.BigEndianUnicode.GetBytes(filename).CopyTo(buffer, packetLength + 3); packetLength += (3 + filenameLength); string contentType = ContentType; if (!string.IsNullOrEmpty(contentType)) { int contentTypeLength = (contentType.Length + 1);// *2; buffer[packetLength] = (byte)ObexHeader.Type; BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)(contentTypeLength + 3))).CopyTo(buffer, packetLength + 1); System.Text.Encoding.ASCII.GetBytes(contentType).CopyTo(buffer, packetLength + 3); packetLength += (3 + contentTypeLength); } if (ContentLength != 0) { buffer[packetLength] = (byte)ObexHeader.Length; BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Convert.ToInt32(ContentLength))).CopyTo(buffer, packetLength + 1); packetLength += 5; } // write the final packet size BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)packetLength)).CopyTo(buffer, 1); // send packet with name header ns.Write(buffer, 0, packetLength); if (CheckResponse(ref status, false)) { int totalBytes = 0; byte[] requestBuffer = requestStream.GetBuffer(); // we really want the content length, but if not available send the whole buffer if (this.ContentLength > 0) { totalBytes = (int)ContentLength; } else { totalBytes = requestBuffer.Length; } MemoryStream readBuffer = new MemoryStream(requestBuffer); while (totalBytes > 0) { int packetLen = AddBody(buffer, 3, ref totalBytes, readBuffer); ns.Write(buffer, 0, packetLen); if (!CheckResponse(ref status, false)) { return(status); } } } return(status); }
private void Send_Button(object sender, RoutedEventArgs e) { BluetoothDeviceInfo dev = (BluetoothDeviceInfo)chosenDevice; ObexStatusCode response_status = SendFile(dev.DeviceAddress, filePath); }
internal ObexWebResponse(MemoryStream stream, WebHeaderCollection headers, ObexStatusCode code) { this.responseStream = stream; this.responseHeaders = headers; this.statusCode = code; }
private bool CheckResponse(ref ObexStatusCode status) { byte[] receiveBuffer = new byte[3]; ns.Read(receiveBuffer, 0, receiveBuffer.Length); status = (ObexStatusCode)receiveBuffer[0]; switch(status) { case ObexStatusCode.Final | ObexStatusCode.OK: case ObexStatusCode.OK: case ObexStatusCode.Final | ObexStatusCode.Continue: case ObexStatusCode.Continue: //get length short len = (short)(IPAddress.NetworkToHostOrder(BitConverter.ToInt16(receiveBuffer, 1)) - 3); if (len > 0) { byte[] receivePacket2 = new byte[len]; ns.Read(receivePacket2, 0, len); remoteMaxPacket = unchecked((ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(receivePacket2, 2))); } return true; default: return false; } //handle errors please! }