public CFSocketSignature(AddressFamily family, SocketType type, ProtocolType proto, CFSocketAddress address) { this.protocolFamily = AddressFamilyToInt(family); this.socketType = SocketTypeToInt(type); this.protocol = ProtocolToInt(proto); this.address = address.Handle; }
public void Connect(IPEndPoint endpoint, double timeout) { using (var address = new CFSocketAddress(endpoint)) { var error = (CFSocketError)(long)CFSocketConnectToAddress(Handle, address.Handle, timeout); if (error != CFSocketError.Success) { throw new CFSocketException(error); } } }
public static void CreatePairWithPeerSocketSignature(AddressFamily family, SocketType type, ProtocolType proto, IPEndPoint endpoint, out CFReadStream readStream, out CFWriteStream writeStream) { using (var address = new CFSocketAddress(endpoint)) { var sig = new CFSocketSignature(family, type, proto, address); IntPtr read, write; CFStreamCreatePairWithPeerSocketSignature(IntPtr.Zero, ref sig, out read, out write); readStream = new CFReadStream(read, true); writeStream = new CFWriteStream(write, true); } }
static void OnCallback(IntPtr s, nuint type, IntPtr address, IntPtr data, IntPtr info) { var socket = GCHandle.FromIntPtr(info).Target as CFSocket; if (socket is null) { return; } CFSocketCallBackType cbType = (CFSocketCallBackType)(ulong)type; if (cbType == CFSocketCallBackType.AcceptCallBack) { var ep = CFSocketAddress.EndPointFromAddressPtr(address); var handle = new CFSocketNativeHandle(Marshal.ReadInt32(data)); socket.OnAccepted(new CFSocketAcceptEventArgs(handle, ep)); } else if (cbType == CFSocketCallBackType.ConnectCallBack) { CFSocketError result; if (data == IntPtr.Zero) { result = CFSocketError.Success; } else { // Note that we read a 32bit value even if CFSocketError is a nint: // 'or a pointer to an SInt32 error code if the connect failed.' result = (CFSocketError)Marshal.ReadInt32(data); } socket.OnConnect(new CFSocketConnectEventArgs(result)); } else if (cbType == CFSocketCallBackType.DataCallBack) { var ep = CFSocketAddress.EndPointFromAddressPtr(address); using (var cfdata = new CFData(data, false)) socket.OnData(new CFSocketDataEventArgs(ep, cfdata.GetBuffer())); } else if (cbType == CFSocketCallBackType.NoCallBack) { // nothing to do } else if (cbType == CFSocketCallBackType.ReadCallBack) { socket.OnRead(new CFSocketReadEventArgs()); } else if (cbType == CFSocketCallBackType.WriteCallBack) { socket.OnWrite(new CFSocketWriteEventArgs()); } }
public void SetAddress(IPEndPoint endpoint) { EnableCallBacks(CFSocketCallBackType.AcceptCallBack); var flags = GetSocketFlags(); flags |= CFSocketFlags.AutomaticallyReenableAcceptCallBack; SetSocketFlags(flags); using (var address = new CFSocketAddress(endpoint)) { var error = (CFSocketError)(long)CFSocketSetAddress(Handle, address.Handle); if (error != CFSocketError.Success) { throw new CFSocketException(error); } } }
public static CFSocket CreateConnectedToSocketSignature(AddressFamily family, SocketType type, ProtocolType proto, IPEndPoint endpoint, double timeout) { var cbTypes = CFSocketCallBackType.ConnectCallBack | CFSocketCallBackType.DataCallBack; using (var address = new CFSocketAddress(endpoint)) { var sig = new CFSocketSignature(family, type, proto, address); var handle = CFSocketCreateConnectedToSocketSignature( IntPtr.Zero, ref sig, (nuint)(ulong)cbTypes, OnCallback, IntPtr.Zero, timeout); if (handle == IntPtr.Zero) { throw new CFSocketException(CFSocketError.Error); } return(new CFSocket(handle, true)); } }