mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-30 08:34:16 +01:00
new class to create socket for talking to proxy
This commit is contained in:
parent
39827ec894
commit
89d20578b8
2 changed files with 68 additions and 16 deletions
|
@ -0,0 +1,60 @@
|
||||||
|
/* -*- compile-command: "cd ../../../../../; ant install"; -*- */
|
||||||
|
/*
|
||||||
|
* Copyright 2009-2010 by Eric House (xwords@eehouse.org). All
|
||||||
|
* rights reserved.
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License as
|
||||||
|
* published by the Free Software Foundation; either version 2 of the
|
||||||
|
* License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.eehouse.android.xw4;
|
||||||
|
|
||||||
|
import javax.net.SocketFactory;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.Socket;
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import org.eehouse.android.xw4.jni.CommonPrefs;
|
||||||
|
|
||||||
|
public class NetUtils {
|
||||||
|
|
||||||
|
public static final byte PROTOCOL_VERSION = 0;
|
||||||
|
// from xwrelay.h
|
||||||
|
public static byte PRX_PUB_ROOMS = 1;
|
||||||
|
public static byte PRX_HAS_MSGS = 2;
|
||||||
|
|
||||||
|
public static Socket MakeProxySocket( Context context,
|
||||||
|
int timeoutMillis )
|
||||||
|
{
|
||||||
|
Socket socket = null;
|
||||||
|
try {
|
||||||
|
int port = CommonPrefs.getDefaultProxyPort( context );
|
||||||
|
String host = CommonPrefs.getDefaultRelayHost( context );
|
||||||
|
|
||||||
|
SocketFactory factory = SocketFactory.getDefault();
|
||||||
|
InetAddress addr = InetAddress.getByName( host );
|
||||||
|
socket = factory.createSocket( addr, port );
|
||||||
|
socket.setSoTimeout( timeoutMillis );
|
||||||
|
|
||||||
|
} catch ( java.net.UnknownHostException uhe ) {
|
||||||
|
Utils.logf( uhe.toString() );
|
||||||
|
} catch( java.io.IOException ioe ) {
|
||||||
|
Utils.logf( ioe.toString() );
|
||||||
|
}
|
||||||
|
Utils.logf( "MakeProxySocket=>%s", null != socket
|
||||||
|
? socket.toString():"null" );
|
||||||
|
return socket;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -40,14 +40,6 @@ import org.eehouse.android.xw4.jni.GameSummary;
|
||||||
|
|
||||||
public class RelayService extends Service {
|
public class RelayService extends Service {
|
||||||
|
|
||||||
private static final String proxy_addr = "10.0.2.2";
|
|
||||||
private static final int proxy_port = 10998;
|
|
||||||
private static final byte PROTOCOL_VERSION = 0;
|
|
||||||
|
|
||||||
// from xwrelay.h
|
|
||||||
private static byte PRX_PUB_ROOMS = 1;
|
|
||||||
private static byte PRX_HAS_MSGS = 2;
|
|
||||||
|
|
||||||
private NotificationManager m_nm;
|
private NotificationManager m_nm;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -63,11 +55,9 @@ public class RelayService extends Service {
|
||||||
ArrayList<byte[]>ids = collectIDs( nBytes );
|
ArrayList<byte[]>ids = collectIDs( nBytes );
|
||||||
if ( null != ids && 0 < ids.size() ) {
|
if ( null != ids && 0 < ids.size() ) {
|
||||||
try {
|
try {
|
||||||
SocketFactory factory = SocketFactory.getDefault();
|
Socket socket =
|
||||||
InetAddress addr = InetAddress.getByName( proxy_addr );
|
NetUtils.MakeProxySocket( RelayService.this,
|
||||||
Socket socket = factory.createSocket( addr,
|
3000 );
|
||||||
proxy_port );
|
|
||||||
socket.setSoTimeout( 3000 );
|
|
||||||
DataOutputStream outStream =
|
DataOutputStream outStream =
|
||||||
new DataOutputStream( socket.getOutputStream() );
|
new DataOutputStream( socket.getOutputStream() );
|
||||||
|
|
||||||
|
@ -76,8 +66,8 @@ public class RelayService extends Service {
|
||||||
Utils.logf( "total packet size: %d",
|
Utils.logf( "total packet size: %d",
|
||||||
2 + nBytes[0] + ids.size() );
|
2 + nBytes[0] + ids.size() );
|
||||||
|
|
||||||
outStream.writeByte( PROTOCOL_VERSION );
|
outStream.writeByte( NetUtils.PROTOCOL_VERSION );
|
||||||
outStream.writeByte( PRX_HAS_MSGS );
|
outStream.writeByte( NetUtils.PRX_HAS_MSGS );
|
||||||
|
|
||||||
// number of ids
|
// number of ids
|
||||||
outStream.writeShort( ids.size() );
|
outStream.writeShort( ids.size() );
|
||||||
|
@ -117,7 +107,7 @@ public class RelayService extends Service {
|
||||||
ids.get(ii).toString() );
|
ids.get(ii).toString() );
|
||||||
// Toast.makeText( RelayService.this, msg,
|
// Toast.makeText( RelayService.this, msg,
|
||||||
// Toast.LENGTH_SHORT).
|
// Toast.LENGTH_SHORT).
|
||||||
// show();
|
// show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -126,6 +116,8 @@ public class RelayService extends Service {
|
||||||
Utils.logf( uhe.toString() );
|
Utils.logf( uhe.toString() );
|
||||||
} catch( java.io.IOException ioe ) {
|
} catch( java.io.IOException ioe ) {
|
||||||
Utils.logf( ioe.toString() );
|
Utils.logf( ioe.toString() );
|
||||||
|
} catch( NullPointerException npe ) {
|
||||||
|
Utils.logf( npe.toString() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
RelayService.this.stopSelf();
|
RelayService.this.stopSelf();
|
||||||
|
|
Loading…
Add table
Reference in a new issue