package com.macaroon.piztor; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.EOFException; import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketTimeoutException; import java.net.UnknownHostException; import java.util.Vector; import android.os.Handler; import android.os.Message; public class SocketClient { static Socket client; static final int ByteLength = 1; static final int IntLength = 4; static final int DoubleLength = 8; static final int TokenLength = 32; static final int Login =0; static final int Update =1; static final int UserInfo =2; static final int Subscription =3; static final int Logout =4; static final int StartPush =5; static final int SendMessage =6; static final int SetMarker =7; static final int SetPassword =8; static final int ClosePush =-5; static final int UID =1; static final int Uname =2; static final int Nname =3; static final int Gender =4; static final int GroupID =5; static final int Latitude =6; static final int Longitude =7; static final int Level =8; static final int PasswordFailed =5; static final int ServerFetchFailed =4; static final int LevelFailed =3; static final int StatusFailed = 2; static final int TimeOut = 1; static final int Success = 0; public SocketClient(String site, int port, int retime) throws UnknownHostException, IOException { try { client = new Socket(); client.connect(new InetSocketAddress(site,port), retime); client.setSoTimeout(retime); //client.setTcpNoDelay(true); } catch (UnknownHostException e) { e.printStackTrace(); throw e; } catch (IOException e) { e.printStackTrace(); throw e; } } public int sendMsg(Req req,Handler recall,Handler h) throws IOException,SocketTimeoutException { try { DataOutputStream out = new DataOutputStream( client.getOutputStream()); int tmp = req.type; int len; byte[] b; int pos = 0; switch (tmp) { case Login: ReqLogin rau = (ReqLogin) req; len = IntLength+ByteLength+rau.user.length()+ByteLength+rau.pass.length()+ByteLength; b = new byte[len]; Convert.write(b,Convert.intToBytes(len),pos); pos+=IntLength; b[pos] = (byte) tmp; pos+=ByteLength; Convert.write(b,rau.user.getBytes(),pos); pos+=rau.user.length(); b[pos] = 0; pos+=ByteLength; Convert.write(b, rau.pass.getBytes(),pos); pos+= rau.pass.length(); b[pos] = 0; pos+=ByteLength; out.write(b); break; case Update: ReqUpdate rup = (ReqUpdate) req; len = IntLength+ByteLength+TokenLength+(rup.uname).length()+ByteLength+DoubleLength+DoubleLength; b = new byte[len]; Convert.write(b,Convert.intToBytes(len),pos); pos+=IntLength; b[pos] = (byte) tmp; pos+=ByteLength; Convert.write(b,Convert.hexStringToBytes(rup.token),pos); pos+=TokenLength; Convert.write(b,(rup.uname).getBytes(),pos); pos+=(rup.uname).length(); b[pos] = 0; pos+=ByteLength; Convert.write(b,Convert.doubleToBytes(rup.latitude),pos); pos+=DoubleLength; Convert.write(b,Convert.doubleToBytes(rup.longitude),pos); pos+=DoubleLength; out.write(b); break; case UserInfo: ReqUserInfo rus = (ReqUserInfo) req; len = IntLength+ByteLength+TokenLength+rus.uname.length()+ByteLength+ByteLength+ByteLength; b = new byte[len]; Convert.write(b,Convert.intToBytes(len),pos); pos+=IntLength; b[pos] = (byte) tmp; pos+=ByteLength; Convert.write(b,Convert.hexStringToBytes(rus.token),pos); pos+=TokenLength; Convert.write(b,(rus.uname).getBytes(),pos); pos+=(rus.uname).length(); b[pos] = 0; pos+=ByteLength; b[pos] = (byte) rus.gid.company; pos+=ByteLength; b[pos] = (byte) rus.gid.section; pos+=ByteLength; out.write(b); break; case Subscription: ReqSubscription rsu = (ReqSubscription) req; int number = rsu.n; len = IntLength+ByteLength+TokenLength+rsu.uname.length()+ByteLength+2*ByteLength*number+ByteLength; b = new byte[len]; Convert.write(b,Convert.intToBytes(len),pos); pos+=IntLength; b[pos] = (byte) tmp; pos+=ByteLength; Convert.write(b,Convert.hexStringToBytes(rsu.token),pos); pos+=TokenLength; Convert.write(b,(rsu.uname).getBytes(),pos); pos+=(rsu.uname).length(); b[pos] = 0; pos+=ByteLength; for(int i=0;i vrg = new Vector(); while(outlen > 1) { int com = in.readUnsignedByte(); int sec = in.readUnsignedByte(); vrg.add(new RGroup(com,sec)); outlen-=ByteLength*2; number++; } in.readUnsignedByte(); msg.obj = new ResLogin(tk, r, vrg, number); msg.what = Login; recall.sendMessage(msg); Vector vs = new Vector(); vs.add(uname); vs.add(tk); Message m = new Message(); m.obj = vs; m.what = StartPush; h.sendMessage(m); break; case Update: msg.obj = new ResUpdate(); msg.what = Update; recall.sendMessage(msg); break; case UserInfo: outlen-=(IntLength+ByteLength+ByteLength); int uid1 =0,s1 =0,l1 =0; String uname1 ="",nname1 =""; double lat1 =0.0,lot1 =0.0; RGroup rg1 = null; int k = 0; byte[] bn = new byte[200]; Vector v = new Vector(); int n = 0; while(outlen > 0) { int tmpcnt =0; while(tmpcnt < 8) { int typ = in.readUnsignedByte(); outlen-=ByteLength; switch(typ){ case UID: uid1 = in.readInt(); outlen-=IntLength; break; case Uname: k = 0; while(true) { bn[k] = in.readByte(); if(bn[k] == 0)break; k++; } byte[] wr = new byte[k]; for(int j=0;j