Пример #1
0
        static void OnCallback(IntPtr s, int type, IntPtr address, IntPtr data, IntPtr info)
        {
            var socket = GCHandle.FromIntPtr(info).Target as CFSocket;
            CFSocketCallBackType cbType = (CFSocketCallBackType)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
                {
                    result = (CFSocketError)Marshal.ReadInt32(data);
                }
                socket.OnConnect(new CFSocketConnectEventArgs(result));
            }
        }
Пример #2
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;
 }
Пример #3
0
 public void Connect(IPEndPoint endpoint, double timeout)
 {
     using (var address = new CFSocketAddress(endpoint)) {
         var error = CFSocketConnectToAddress(Handle, address.Handle, timeout);
         if (error != CFSocketError.Success)
         {
             throw new CFSocketException(error);
         }
     }
 }
Пример #4
0
 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);
     }
 }
Пример #5
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 = CFSocketSetAddress(Handle, address.Handle);
                if (error != CFSocketError.Success)
                {
                    throw new CFSocketException(error);
                }
            }
        }
Пример #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, cbTypes, OnCallback, IntPtr.Zero, timeout);
                if (handle == IntPtr.Zero)
                {
                    throw new CFSocketException(CFSocketError.Error);
                }

                return(new CFSocket(handle));
            }
        }
Пример #7
0
        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);
            }
        }
Пример #8
0
 public static CFHost CreateWithAddress(IPAddress address)
 {
     using (var data = new CFSocketAddress (new IPEndPoint (address, 0))) {
         return new CFHost (CFHostCreateWithAddress (IntPtr.Zero, data.Handle));
     }
 }
Пример #9
0
		public static CFHost Create (IPEndPoint endpoint)
		{
			using (var data = new CFSocketAddress (endpoint))
				return new CFHost (CFHostCreateWithAddress (IntPtr.Zero, data.Handle));
		}
Пример #10
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;
		}
Пример #11
0
		public void Connect (IPEndPoint endpoint, double timeout)
		{
			using (var address = new CFSocketAddress (endpoint)) {
				var error = CFSocketConnectToAddress (handle, address.Handle, timeout);
				if (error != CFSocketError.Success)
					throw new CFSocketException (error);
			}
		}
Пример #12
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 = CFSocketSetAddress (handle, address.Handle);
				if (error != CFSocketError.Success)
					throw new CFSocketException (error);
			}
		}
Пример #13
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, cbTypes, OnCallback, IntPtr.Zero, timeout);
				if (handle == IntPtr.Zero)
					throw new CFSocketException (CFSocketError.Error);

				return new CFSocket (handle);
			}
		}