FileDocCategorySizeDatePackage
SocketConnection.javaAPI DocJ2ME CLDC 1.14736Wed Feb 05 15:56:04 GMT 2003kdp

SocketConnection

public class SocketConnection extends Object implements Runnable

Fields Summary
Socket
socket
DataOutputStream
out
DataInputStream
in
ProxyListener
proxy
Constructors Summary
SocketConnection(ProxyListener proxy, Socket socket)

    this.proxy = proxy;
        this.socket = socket;
        socket.setTcpNoDelay(true);
        in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
        out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
    
Methods Summary
public voidclose()

        try {
            out.flush();
            out.close();
            in.close();
            socket.close();
        } catch (Exception e) {
            ;
        }
    
public bytereceiveByte()

            int b = in.read();
            return (byte)b;
    
public PacketreceivePacket()


            Packet p = new Packet();
            int b1,b2,b3,b4;
    
            // length
            b1 = in.read();
            b2 = in.read();
            b3 = in.read();
            b4 = in.read();
    
            if (b1<0 || b2<0 || b3<0 || b4<0)
                throw new EOFException();
    
            int length = ((b1 << 24) + (b2 << 16) + (b3 << 8) + (b4 << 0));
    
            // id
            b1 = in.read();
            b2 = in.read();
            b3 = in.read();
            b4 = in.read();
    
            if (b1<0 || b2<0 || b3<0 || b4<0)
                throw new EOFException();
    
            p.id = ((b1 << 24) + (b2 << 16) + (b3 << 8) + (b4 << 0));
    
            p.flags = (short)in.read();
            if (p.flags < 0) {
                throw new EOFException();
            }
            if ((p.flags & Packet.Reply) == 0) {
                p.cmdSet = (short)in.read();
                p.cmd = (short)in.read();
                if (p.cmdSet < 0 || p.cmd < 0){
                    throw new EOFException();
                }
            } else {
                b1 = in.read();
                b2 = in.read();
                if (b1 < 0 || b2 < 0){
                    throw new EOFException();
                }
                p.errorCode = (short)((b1 << 8) + (b2 << 0));
            }
    
            length -= 11; // subtract the header
    
            if (length < 0) {
                // WHoa! this shouldn't be happening!
                System.err.println("length is " + length);
                System.err.println("Read is " + in.read());
            }
            p.data = new byte[length];
    
            int n = 0;
            while (n < p.data.length) {
                int count = in.read(p.data, n, p.data.length - n);
                if (count < 0) {
                    throw new EOFException();
                }
                n += count;
            }
    
            return p;
    
public voidrun()

    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    try {
        while (true) {
        Packet p = receivePacket();
        if ((p.flags & Packet.Reply) == 0 || p.id >= 0) {
            proxy.newPacket(p);
        } else {
            proxy.replyReceived(p);
        }
        }
    } catch (Exception e) {
        try {
            Log.LOGN(2, "Socket exception in " + proxy + e + " ...exiting");
//                  e.printStackTrace();
        proxy.newPacket(null);
        proxy.replyReceived(null);
                    return;
        } catch (Exception ignore) {}
    }
    
public voidsend(Packet p)


            int length = p.data.length + 11;
    
            // Length
            out.write((length >>> 24) & 0xFF);
            out.write((length >>> 16) & 0xFF);
            out.write((length >>>  8) & 0xFF);
            out.write((length >>>  0) & 0xFF);
    
            // id
            out.write((p.id >>> 24) & 0xFF);
            out.write((p.id >>> 16) & 0xFF);
            out.write((p.id >>>  8) & 0xFF);
            out.write((p.id >>>  0) & 0xFF);
    
            out.write(p.flags);
    
            if ((p.flags & Packet.Reply) == 0) {
                out.write(p.cmdSet);
                out.write(p.cmd);
            } else {
                out.write((p.errorCode >>>  8) & 0xFF);
                out.write((p.errorCode >>>  0) & 0xFF);
            }
            out.write(p.data);
    
        out.flush();
    
public voidsendByte(byte b)

            out.write(b);
            out.flush();