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 } } } }
public static Boolean IsTLSType(tnet_socket_type_t type) { return (((int)type & TNET_SOCKET_TYPE_TLS) == TNET_SOCKET_TYPE_TLS); }
private static Boolean IsIPv4Type(tnet_socket_type_t type) { return (((int)type & TNET_SOCKET_TYPE_IPV4) == TNET_SOCKET_TYPE_IPV4); }
public static Boolean IsIPv6Type(tnet_socket_type_t type) { return (((int)type & TNET_SOCKET_TYPE_IPV6) == TNET_SOCKET_TYPE_IPV6); }
public static Boolean IsStreamType(tnet_socket_type_t type) { return (((int)type & TNET_SOCKET_TYPE_UDP) != TNET_SOCKET_TYPE_UDP); }
public TNET_Socket(String host, ushort port, tnet_socket_type_t type) : this(host, port, type, true, true) { }
public TNET_Socket(tnet_socket_type_t type) : this(TNET_SOCKET_HOST_ANY, TNET_SOCKET_PORT_ANY, type, true, true) { }
public static Boolean IsDatagramType(tnet_socket_type_t type) { return(((int)type & TNET_SOCKET_TYPE_UDP) == TNET_SOCKET_TYPE_UDP); }
public static Boolean IsTLSType(tnet_socket_type_t type) { return(((int)type & TNET_SOCKET_TYPE_TLS) == TNET_SOCKET_TYPE_TLS); }
public static Boolean IsIPv6Type(tnet_socket_type_t type) { return(((int)type & TNET_SOCKET_TYPE_IPV6) == TNET_SOCKET_TYPE_IPV6); }
private static Boolean IsIPv4Type(tnet_socket_type_t type) { return(((int)type & TNET_SOCKET_TYPE_IPV4) == TNET_SOCKET_TYPE_IPV4); }