mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-23 07:27:22 +01:00
catch up with changes in udp proto: use vli for packet num in header.
This commit is contained in:
parent
7e02db78db
commit
47414beeb2
1 changed files with 51 additions and 6 deletions
|
@ -27,6 +27,8 @@ import android.os.AsyncTask;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
|
@ -88,7 +90,10 @@ public class RelayService extends XWService
|
||||||
private long m_lastGamePacketReceived;
|
private long m_lastGamePacketReceived;
|
||||||
|
|
||||||
// These must match the enum XWRelayReg in xwrelay.h
|
// These must match the enum XWRelayReg in xwrelay.h
|
||||||
private static final int XWPDEV_PROTO_VERSION = 0;
|
private static enum XWPDevProto { XWPDEV_PROTO_VERSION_INVALID
|
||||||
|
,XWPDEV_PROTO_VERSION_1
|
||||||
|
};
|
||||||
|
|
||||||
// private static final int XWPDEV_NONE = 0;
|
// private static final int XWPDEV_NONE = 0;
|
||||||
|
|
||||||
// Must be kept in sync with eponymous enum in xwrelay.h
|
// Must be kept in sync with eponymous enum in xwrelay.h
|
||||||
|
@ -624,7 +629,7 @@ public class RelayService extends XWService
|
||||||
try {
|
try {
|
||||||
DataOutputStream out =
|
DataOutputStream out =
|
||||||
addProtoAndCmd( bas, XWRelayReg.XWPDEV_ACK );
|
addProtoAndCmd( bas, XWRelayReg.XWPDEV_ACK );
|
||||||
out.writeInt( header.m_packetID );
|
un2vli( header.m_packetID, out );
|
||||||
postPacket( bas );
|
postPacket( bas );
|
||||||
} catch ( java.io.IOException ioe ) {
|
} catch ( java.io.IOException ioe ) {
|
||||||
DbgUtils.loge( ioe );
|
DbgUtils.loge( ioe );
|
||||||
|
@ -637,8 +642,8 @@ public class RelayService extends XWService
|
||||||
{
|
{
|
||||||
PacketHeader result = null;
|
PacketHeader result = null;
|
||||||
byte proto = dis.readByte();
|
byte proto = dis.readByte();
|
||||||
if ( XWPDEV_PROTO_VERSION == proto ) {
|
if ( XWPDevProto.XWPDEV_PROTO_VERSION_1.ordinal() == proto ) {
|
||||||
int packetID = dis.readInt();
|
int packetID = vli2un( dis );
|
||||||
if ( 0 != packetID ) {
|
if ( 0 != packetID ) {
|
||||||
DbgUtils.logf( "readHeader: got packetID %d", packetID );
|
DbgUtils.logf( "readHeader: got packetID %d", packetID );
|
||||||
}
|
}
|
||||||
|
@ -666,8 +671,8 @@ public class RelayService extends XWService
|
||||||
throws java.io.IOException
|
throws java.io.IOException
|
||||||
{
|
{
|
||||||
DataOutputStream out = new DataOutputStream( bas );
|
DataOutputStream out = new DataOutputStream( bas );
|
||||||
out.writeByte( XWPDEV_PROTO_VERSION );
|
out.writeByte( XWPDevProto.XWPDEV_PROTO_VERSION_1.ordinal() );
|
||||||
out.writeInt( nextPacketID( cmd ) ); // packetID
|
un2vli( nextPacketID( cmd ), out );
|
||||||
out.writeByte( cmd.ordinal() );
|
out.writeByte( cmd.ordinal() );
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
@ -951,6 +956,46 @@ public class RelayService extends XWService
|
||||||
stopUDPThreadsIf();
|
stopUDPThreadsIf();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void un2vli( int nn, OutputStream os )
|
||||||
|
throws java.io.IOException
|
||||||
|
{
|
||||||
|
int indx = 0;
|
||||||
|
boolean done = false;
|
||||||
|
do {
|
||||||
|
byte byt = (byte)(nn & 0x7F);
|
||||||
|
nn >>= 7;
|
||||||
|
done = 0 == nn;
|
||||||
|
if ( done ) {
|
||||||
|
byt |= 0x80;
|
||||||
|
}
|
||||||
|
os.write( byt );
|
||||||
|
} while ( !done );
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int vli2un( InputStream is ) throws java.io.IOException
|
||||||
|
{
|
||||||
|
int result = 0;
|
||||||
|
byte[] buf = new byte[1];
|
||||||
|
int nRead = 0;
|
||||||
|
|
||||||
|
boolean done = false;
|
||||||
|
for ( int count = 0; !done; ++count ) {
|
||||||
|
nRead = is.read( buf );
|
||||||
|
if ( 1 != nRead ) {
|
||||||
|
DbgUtils.logf( "vli2un: unable to read from stream" );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
int byt = buf[0];
|
||||||
|
done = 0 != (byt & 0x80);
|
||||||
|
if ( done ) {
|
||||||
|
byt &= 0x7F;
|
||||||
|
}
|
||||||
|
result |= byt << (7 * count);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/* Timers:
|
/* Timers:
|
||||||
*
|
*
|
||||||
* Two goals: simulate the GCM experience for those who don't have
|
* Two goals: simulate the GCM experience for those who don't have
|
||||||
|
|
Loading…
Add table
Reference in a new issue