16 * You should have received a copy of the GNU General Public License |
16 * You should have received a copy of the GNU General Public License |
17 * along with this source code; if not, write to the Free Software |
17 * along with this source code; if not, write to the Free Software |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 */ |
19 */ |
20 |
20 |
21 package src.org.sipdroid.net; |
21 package org.sipdroid.net; |
22 |
22 |
23 import java.io.IOException; |
23 import java.io.IOException; |
24 import java.net.DatagramPacket; |
24 import java.net.DatagramPacket; |
25 import java.net.DatagramSocket; |
25 import java.net.DatagramSocket; |
26 import java.net.InetAddress; |
26 import java.net.InetAddress; |
31 import org.sipdroid.net.impl.OSNetworkSystem; |
31 import org.sipdroid.net.impl.OSNetworkSystem; |
32 import org.sipdroid.net.impl.PlainDatagramSocketImpl; |
32 import org.sipdroid.net.impl.PlainDatagramSocketImpl; |
33 |
33 |
34 public class SipdroidSocket extends DatagramSocket { |
34 public class SipdroidSocket extends DatagramSocket { |
35 |
35 |
36 PlainDatagramSocketImpl impl; |
36 PlainDatagramSocketImpl impl; |
37 public static boolean loaded = false; |
37 public static boolean loaded = false; |
38 |
38 |
39 public SipdroidSocket(int port) throws SocketException, UnknownHostException { |
39 public SipdroidSocket(int port) throws SocketException, UnknownHostException { |
40 super(!loaded?port:0); |
40 super(!loaded?port:0); |
41 if (loaded) { |
41 if (loaded) { |
42 impl = new PlainDatagramSocketImpl(); |
42 impl = new PlainDatagramSocketImpl(); |
43 impl.create(); |
43 impl.create(); |
44 impl.bind(port,InetAddress.getByName("0")); |
44 impl.bind(port,InetAddress.getByName("0")); |
45 } |
|
46 } |
45 } |
47 |
46 } |
48 public void close() { |
47 |
49 super.close(); |
48 public void close() { |
50 if (loaded) impl.close(); |
49 super.close(); |
|
50 if (loaded) impl.close(); |
|
51 } |
|
52 |
|
53 public void setSoTimeout(int val) throws SocketException { |
|
54 if (loaded) impl.setOption(SocketOptions.SO_TIMEOUT, val); |
|
55 else super.setSoTimeout(val); |
|
56 } |
|
57 |
|
58 public void receive(DatagramPacket pack) throws IOException { |
|
59 if (loaded) impl.receive(pack); |
|
60 else super.receive(pack); |
|
61 } |
|
62 |
|
63 public void send(DatagramPacket pack) throws IOException { |
|
64 if (loaded) impl.send(pack); |
|
65 else super.send(pack); |
|
66 } |
|
67 |
|
68 public boolean isConnected() { |
|
69 if (loaded) return true; |
|
70 else return super.isConnected(); |
|
71 } |
|
72 |
|
73 public void disconnect() { |
|
74 if (!loaded) super.disconnect(); |
|
75 } |
|
76 |
|
77 public void connect(InetAddress addr,int port) { |
|
78 if (!loaded) super.connect(addr,port); |
|
79 } |
|
80 |
|
81 static { |
|
82 try { |
|
83 System.loadLibrary("OSNetworkSystem"); |
|
84 OSNetworkSystem.getOSNetworkSystem().oneTimeInitialization(true); |
|
85 SipdroidSocket.loaded = true; |
|
86 } catch (Throwable e) { |
|
87 e.printStackTrace(); |
51 } |
88 } |
52 |
89 } |
53 public void setSoTimeout(int val) throws SocketException { |
|
54 if (loaded) impl.setOption(SocketOptions.SO_TIMEOUT, val); |
|
55 else super.setSoTimeout(val); |
|
56 } |
|
57 |
|
58 public void receive(DatagramPacket pack) throws IOException { |
|
59 if (loaded) impl.receive(pack); |
|
60 else super.receive(pack); |
|
61 } |
|
62 |
|
63 public void send(DatagramPacket pack) throws IOException { |
|
64 if (loaded) impl.send(pack); |
|
65 else super.send(pack); |
|
66 } |
|
67 |
|
68 public boolean isConnected() { |
|
69 if (loaded) return true; |
|
70 else return super.isConnected(); |
|
71 } |
|
72 |
|
73 public void disconnect() { |
|
74 if (!loaded) super.disconnect(); |
|
75 } |
|
76 |
|
77 public void connect(InetAddress addr,int port) { |
|
78 if (!loaded) super.connect(addr,port); |
|
79 } |
|
80 |
|
81 static { |
|
82 try { |
|
83 System.loadLibrary("OSNetworkSystem"); |
|
84 OSNetworkSystem.getOSNetworkSystem().oneTimeInitialization(true); |
|
85 SipdroidSocket.loaded = true; |
|
86 } catch (Throwable e) { |
|
87 } |
|
88 } |
|
89 } |
90 } |