Exemple #1
0
 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;
 }
Exemple #2
0
 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);
         writeStream = new CFWriteStream(write);
     }
 }
Exemple #4
0
        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);
                }
            }
        }
Exemple #5
0
        static void OnCallback(IntPtr s, nuint type, IntPtr address, IntPtr data, IntPtr info)
        {
            var socket = GCHandle.FromIntPtr(info).Target as CFSocket;
            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());
            }
        }
Exemple #6
0
        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));
            }
        }
Exemple #7
0
 public static void CreatePairWithPeerSocketSignature(AddressFamily family, SocketType type,
     ProtocolType proto, IPEndPoint endpoint,
     out NSInputStream readStream,
     out NSOutputStream writeStream)
 {
     using (var address = new CFSocketAddress (endpoint)) {
         var sig = new CFSocketSignature (family, type, proto, address);
         IntPtr read, write;
         CFStream.CFStreamCreatePairWithPeerSocketSignature (IntPtr.Zero, ref sig, out read, out write);
         AssignStreams (read, write, out readStream, out writeStream);
     }
 }
Exemple #8
0
		public static CFHost Create (IPEndPoint endpoint)
		{
			// CFSocketAddress will throw the ANE
			using (var data = new CFSocketAddress (endpoint))
				return new CFHost (CFHostCreateWithAddress (IntPtr.Zero, data.Handle));
		}
Exemple #9
0
 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);
     }
 }
Exemple #10
0
 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);
     }
 }
Exemple #11
0
        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);
            }
        }
Exemple #12
0
 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;
 }