public Win32NetworkStream(Win32Socket socket, bool ownsSocket) { if (socket is null) { throw new ArgumentNullException(nameof(socket)); } _socket = socket; _ownsSocket = ownsSocket; }
private bool disposedValue = false; // To detect redundant calls void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { _socket?.Close(); _socket?.Dispose(); _socket = null; } disposedValue = true; } }
void DoStart() { if (handle != IntPtr.Zero) { throw new InvalidOperationException(); } endPoint = new BluetoothEndPoint(0, serviceUuid); socket = new Win32Socket(); socket.Bind(endPoint); socket.Listen(1); WSAQUERYSET qs = new WSAQUERYSET(); //qs.dwSize = Marshal.SizeOf(qs); qs.dwNameSpace = NativeMethods.NS_BTH; qs.lpServiceClassId = serviceUuid; qs.lpszServiceInstanceName = ServiceName; qs.dwNumberOfCsAddrs = 1; qs.lpcsaBuffer = new CSADDR_INFO { LocalAddr = socket.LocalEndPointRaw, iSocketType = SocketType.Stream, iProtocol = NativeMethods.PROTOCOL_RFCOMM }; int sb = Marshal.SizeOf(qs.lpcsaBuffer); qs.lpBlob = new BLOB(); qs.lpBlob.blobData = new BTH_SET_SERVICE(); qs.lpBlob.size = Marshal.SizeOf(qs.lpBlob.blobData); qs.lpBlob.blobData.pSdpVersion = 1; qs.lpBlob.blobData.fCodService = ServiceClass; int result = NativeMethods.WSASetService(ref qs, WSAESETSERVICEOP.RNRSERVICE_DELETE, 0); }
internal BluetoothClient(Win32Socket s) { _socket = s; }
void DoConnect(BluetoothAddress address, Guid service) { _socket = new Win32Socket(); _socket.Connect(new BluetoothEndPoint(address, service)); }
void DoStart() { if (handle != IntPtr.Zero) { throw new InvalidOperationException(); } endPoint = new BluetoothEndPoint(0, serviceUuid); socket = new Win32Socket(); socket.Bind(endPoint); System.Diagnostics.Debug.WriteLine(socket.IsBound); socket.Listen(1); WSAQUERYSET qs = new WSAQUERYSET(); qs.dwSize = Marshal.SizeOf(qs); qs.dwNameSpace = NativeMethods.NS_BTH; //var uuidh = GCHandle.Alloc(serviceUuid, GCHandleType.Pinned); //qs.lpServiceClassId = uuidh.AddrOfPinnedObject(); //qs.lpszServiceInstanceName = ServiceName; qs.dwNumberOfCsAddrs = 1; var ep = socket.LocalEndPoint as BluetoothEndPoint; var csa = new CSADDR_INFO { lpLocalSockaddr = Marshal.AllocHGlobal(30), iLocalSockaddrLength = 30, iSocketType = SocketType.Stream, iProtocol = NativeMethods.PROTOCOL_RFCOMM }; Marshal.Copy(socket.LocalEndPointRaw, 0, csa.lpLocalSockaddr, 30); IntPtr pcsa = Marshal.AllocHGlobal(24); Marshal.StructureToPtr(csa, pcsa, false); qs.lpcsaBuffer = pcsa; var blob = new BLOB(); var blobData = new BTH_SET_SERVICE(); int sdpVer = 1; blobData.pSdpVersion = Marshal.AllocHGlobal(4); Marshal.WriteInt32(blobData.pSdpVersion, sdpVer); blobData.pRecordHandle = Marshal.AllocHGlobal(4); Marshal.WriteInt32(blobData.pRecordHandle, 0); blobData.fCodService = ServiceClass; if (ServiceRecord == null) { ServiceRecordBuilder builder = new ServiceRecordBuilder(); builder.AddServiceClass(serviceUuid); builder.ProtocolType = BluetoothProtocolDescriptorType.Rfcomm; if (!string.IsNullOrEmpty(ServiceName)) { builder.ServiceName = ServiceName; } ServiceRecord = builder.ServiceRecord; } ServiceRecordHelper.SetRfcommChannelNumber(ServiceRecord, socket.LocalEndPointRaw[26]); byte[] rawBytes = ServiceRecord.ToByteArray(); blobData.ulRecordLength = (uint)rawBytes.Length; int structSize = (2 * IntPtr.Size) + (7 * 4); blob.size = structSize + rawBytes.Length; blob.blobData = Marshal.AllocHGlobal(blob.size); Marshal.StructureToPtr(blobData, blob.blobData, false); Marshal.Copy(rawBytes, 0, IntPtr.Add(blob.blobData, structSize), rawBytes.Length); var blobh = GCHandle.Alloc(blob, GCHandleType.Pinned); qs.lpBlob = blobh.AddrOfPinnedObject(); try { int result = NativeMethods.WSASetService(ref qs, WSAESETSERVICEOP.RNRSERVICE_REGISTER, 0); var newstruct = Marshal.PtrToStructure <BTH_SET_SERVICE>(blob.blobData); handle = Marshal.ReadIntPtr(newstruct.pRecordHandle); if (result == -1) { int werr = Marshal.GetLastWin32Error(); throw new SocketException(werr); } } finally { Marshal.FreeHGlobal(csa.lpLocalSockaddr); Marshal.FreeHGlobal(qs.lpcsaBuffer); Marshal.FreeHGlobal(blobData.pSdpVersion); Marshal.FreeHGlobal(blobData.pRecordHandle); Marshal.FreeHGlobal(blob.blobData); blobh.Free(); } }