Ejemplo n.º 1
0
        public TNET_Socket(String host, ushort port, tnet_socket_type_t type, Boolean nonblocking, Boolean bindsocket)
        {
            mId = ++sUniqueId;
            mType = type;
            mHost = host;
            mPort = port;
            mSocketEvent = new ManualResetEvent(false);

            mSendSocketEventArgs = new SocketAsyncEventArgs();
            mSendSocketEventArgs.Completed += delegate(object sender, SocketAsyncEventArgs e)
            {

                if (e.SocketError != SocketError.Success)
                {
                    mSocketEvent.Set();
                    return;
                }

                // On WP7 Socket.ReceiveFromAsync() will only works after at least ONE SendTo()
                if (e.LastOperation == SocketAsyncOperation.SendTo)
                {
                    if (mRecvSocketEventArgs == null && RecvSocketCompleted != null)
                    {
                        mRecvSocketEventArgs = new SocketAsyncEventArgs();

                        // setting the remote endpoint will not filter on the remote host:port
                        // => will continue to work even if the remote port is different than 5060
                        // IPEndPoint is not serializable => create new one
                        mRecvSocketEventArgs.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 5060); // e.RemoteEndPoint
                        mRecvSocketEventArgs.Completed += this.RecvSocketCompleted;

                        mRecvSocketEventArgs.SetBuffer(buffers, 0, buffers.Length);
                        mRecvSocketEventArgs.UserToken = e.UserToken;
                        (mRecvSocketEventArgs.UserToken as TNET_Socket).Socket.ReceiveFromAsync(mRecvSocketEventArgs);
                    }
                }

                mSocketEvent.Set();
            };

            AddressFamily addressFamily = TNET_Socket.IsIPv6Type(type) ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork;
            SocketType socketType = TNET_Socket.IsStreamType(type) ? SocketType.Stream : SocketType.Dgram;
            #if WINDOWS_PHONE
            ProtocolType protocolType = TNET_Socket.IsStreamType(type) ? ProtocolType.Tcp : ProtocolType.Udp;
            #else
            ProtocolType protocolType = TNET_Socket.IsIPv6Type(type) ? ProtocolType.IP : ProtocolType.IP; // Leave it like this
            #endif
            mSocket = new Socket(addressFamily, socketType, protocolType);
            #if !WINDOWS_PHONE
            mSocket.Blocking = !nonblocking;
            mSocket.UseOnlyOverlappedIO = true;
            #endif

            mSendSocketEventArgs.UserToken = this;

            if (bindsocket)
            {
                IPAddress ipAddress = TNET_Utils.GetBestLocalIPAddress(host, type);
                TSK_Debug.Info("Local address={0}", ipAddress);
                mHost = ipAddress.ToString();
                if (bindsocket)
                {
                    ushort localPort = (port == TNET_SOCKET_PORT_ANY) ? (ushort)0 : port;
                    IPEndPoint localIEP = new IPEndPoint(ipAddress, localPort);
            #if !WINDOWS_PHONE
                    mSocket.Bind(localIEP);
            #endif
                    if (TNET_Socket.IsDatagramType(type))
                    {
             #if !WINDOWS_PHONE
                        mPort = (ushort)(mSocket.LocalEndPoint as IPEndPoint).Port;
            #endif
                    }
                }
            }
        }
Ejemplo n.º 2
0
 public static Boolean IsTLSType(tnet_socket_type_t type)
 {
     return (((int)type & TNET_SOCKET_TYPE_TLS) == TNET_SOCKET_TYPE_TLS);
 }
Ejemplo n.º 3
0
 private static Boolean IsIPv4Type(tnet_socket_type_t type)
 {
     return (((int)type & TNET_SOCKET_TYPE_IPV4) == TNET_SOCKET_TYPE_IPV4);
 }
Ejemplo n.º 4
0
 public static Boolean IsIPv6Type(tnet_socket_type_t type)
 {
     return (((int)type & TNET_SOCKET_TYPE_IPV6) == TNET_SOCKET_TYPE_IPV6);
 }
Ejemplo n.º 5
0
 public static Boolean IsStreamType(tnet_socket_type_t type)
 {
     return (((int)type & TNET_SOCKET_TYPE_UDP) != TNET_SOCKET_TYPE_UDP);
 }
Ejemplo n.º 6
0
 public TNET_Socket(String host, ushort port, tnet_socket_type_t type)
     : this(host, port, type, true, true)
 {
 }
Ejemplo n.º 7
0
 public TNET_Socket(tnet_socket_type_t type)
     : this(TNET_SOCKET_HOST_ANY, TNET_SOCKET_PORT_ANY, type, true, true)
 {
 }
Ejemplo n.º 8
0
 public static Boolean IsDatagramType(tnet_socket_type_t type)
 {
     return(((int)type & TNET_SOCKET_TYPE_UDP) == TNET_SOCKET_TYPE_UDP);
 }
Ejemplo n.º 9
0
 public static Boolean IsTLSType(tnet_socket_type_t type)
 {
     return(((int)type & TNET_SOCKET_TYPE_TLS) == TNET_SOCKET_TYPE_TLS);
 }
Ejemplo n.º 10
0
 public static Boolean IsIPv6Type(tnet_socket_type_t type)
 {
     return(((int)type & TNET_SOCKET_TYPE_IPV6) == TNET_SOCKET_TYPE_IPV6);
 }
Ejemplo n.º 11
0
 private static Boolean IsIPv4Type(tnet_socket_type_t type)
 {
     return(((int)type & TNET_SOCKET_TYPE_IPV4) == TNET_SOCKET_TYPE_IPV4);
 }
Ejemplo n.º 12
0
 public TNET_Socket(tnet_socket_type_t type)
     : this(TNET_SOCKET_HOST_ANY, TNET_SOCKET_PORT_ANY, type, true, true)
 {
 }
Ejemplo n.º 13
0
 public TNET_Socket(String host, ushort port, tnet_socket_type_t type)
     : this(host, port, type, true, true)
 {
 }
Ejemplo n.º 14
0
        public TNET_Socket(String host, ushort port, tnet_socket_type_t type, Boolean nonblocking, Boolean bindsocket)
        {
            mId          = ++sUniqueId;
            mType        = type;
            mHost        = host;
            mPort        = port;
            mSocketEvent = new ManualResetEvent(false);

            mSendSocketEventArgs            = new SocketAsyncEventArgs();
            mSendSocketEventArgs.Completed += delegate(object sender, SocketAsyncEventArgs e)
            {
                if (e.SocketError != SocketError.Success)
                {
                    mSocketEvent.Set();
                    return;
                }

                // On WP7 Socket.ReceiveFromAsync() will only works after at least ONE SendTo()
                if (e.LastOperation == SocketAsyncOperation.SendTo)
                {
                    if (mRecvSocketEventArgs == null && RecvSocketCompleted != null)
                    {
                        mRecvSocketEventArgs = new SocketAsyncEventArgs();

                        // setting the remote endpoint will not filter on the remote host:port
                        // => will continue to work even if the remote port is different than 5060
                        // IPEndPoint is not serializable => create new one
                        mRecvSocketEventArgs.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 5060); // e.RemoteEndPoint
                        mRecvSocketEventArgs.Completed     += this.RecvSocketCompleted;

                        mRecvSocketEventArgs.SetBuffer(buffers, 0, buffers.Length);
                        mRecvSocketEventArgs.UserToken = e.UserToken;
                        (mRecvSocketEventArgs.UserToken as TNET_Socket).Socket.ReceiveFromAsync(mRecvSocketEventArgs);
                    }
                }

                mSocketEvent.Set();
            };

            AddressFamily addressFamily = TNET_Socket.IsIPv6Type(type) ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork;
            SocketType    socketType    = TNET_Socket.IsStreamType(type) ? SocketType.Stream : SocketType.Dgram;

#if WINDOWS_PHONE
            ProtocolType protocolType = TNET_Socket.IsStreamType(type) ? ProtocolType.Tcp : ProtocolType.Udp;
#else
            ProtocolType protocolType = TNET_Socket.IsIPv6Type(type) ? ProtocolType.IP : ProtocolType.IP; // Leave it like this
#endif
            mSocket = new Socket(addressFamily, socketType, protocolType);
#if !WINDOWS_PHONE
            mSocket.Blocking            = !nonblocking;
            mSocket.UseOnlyOverlappedIO = true;
#endif

            mSendSocketEventArgs.UserToken = this;

            if (bindsocket)
            {
                IPAddress ipAddress = TNET_Utils.GetBestLocalIPAddress(host, type);
                TSK_Debug.Info("Local address={0}", ipAddress);
                mHost = ipAddress.ToString();
                if (bindsocket)
                {
                    ushort     localPort = (port == TNET_SOCKET_PORT_ANY) ? (ushort)0 : port;
                    IPEndPoint localIEP  = new IPEndPoint(ipAddress, localPort);
#if !WINDOWS_PHONE
                    mSocket.Bind(localIEP);
#endif
                    if (TNET_Socket.IsDatagramType(type))
                    {
 #if !WINDOWS_PHONE
                        mPort = (ushort)(mSocket.LocalEndPoint as IPEndPoint).Port;
#endif
                    }
                }
            }
        }