tcphandler.java

来自「J2ME MIDP_Example_Applications」· Java 代码 · 共 307 行

JAVA
307
字号
// Copyright 2003 Nokia Corporation.
//
// THIS SOURCE CODE IS PROVIDED 'AS IS', WITH NO WARRANTIES WHATSOEVER,
// EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY, FITNESS
// FOR ANY PARTICULAR PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE
// OR TRADE PRACTICE, RELATING TO THE SOURCE CODE OR ANY WARRANTY OTHERWISE
// ARISING OUT OF ANY PROPOSAL, SPECIFICATION, OR SAMPLE AND WITH NO
// OBLIGATION OF NOKIA TO PROVIDE THE LICENSEE WITH ANY MAINTENANCE OR
// SUPPORT. FURTHERMORE, NOKIA MAKES NO WARRANTY THAT EXERCISE OF THE
// RIGHTS GRANTED HEREUNDER DOES NOT INFRINGE OR MAY NOT CAUSE INFRINGEMENT
// OF ANY PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OWNED OR CONTROLLED
// BY THIRD PARTIES
//
// Furthermore, information provided in this source code is preliminary,
// and may be changed substantially prior to final release. Nokia Corporation
// retains the right to make changes to this source code at
// any time, without notice. This source code is provided for informational
// purposes only.
//
// Nokia and Nokia Connecting People are registered trademarks of Nokia
// Corporation.
// Java and all Java-based marks are trademarks or registered trademarks of
// Sun Microsystems, Inc.
// Other product and company names mentioned herein may be trademarks or
// trade names of their respective owners.
//
// A non-exclusive, non-transferable, worldwide, limited license is hereby
// granted to the Licensee to download, print, reproduce and modify the
// source code. The licensee has the right to market, sell, distribute and
// make available the source code in original or modified form only when
// incorporated into the programs developed by the Licensee. No other
// license, express or implied, by estoppel or otherwise, to any other
// intellectual property rights is granted herein.


// unnamed package

import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;


class TCPHandler
    implements Runnable
{
    private static final byte ZERO = (byte)'0';
    private static final int LENGTH_MAX_DIGITS = 5;
    private static final int MAX_MESSAGE_LENGTH = 65536 - LENGTH_MAX_DIGITS;

    private final Vector writeQueue = new Vector();
    private final String url;

    private SocketConnection conn = null;
    private OutputStream out = null;
    private InputStream in = null;
    private TCPHandlerListener listener = null;
    private boolean aborting = false;


    TCPHandler(String url)
    {
        if ((url == null) || !url.startsWith("socket://"))
        {
            throw new IllegalArgumentException("Bad address");
        }
        else
        {
            this.url = url;
            new Thread(this).start();
        }
    }


    void setTCPHandlerListener(TCPHandlerListener listener)
    {
        this.listener = listener;
    }


    void close()
    {
        if (!aborting)
        {
            synchronized(this)
            {
                aborting = true;
            }

            synchronized(writeQueue)
            {
                writeQueue.notify();
            }

            if (out != null)
            {
                try
                {
                    out.close();
                    synchronized(this)
                    {
                        out = null;
                    }
                }
                catch (Exception e)
                {
                    // there is nothing we can do: ignore
                }
            }

            if (in != null)
            {
                try
                {
                    in.close();
                    synchronized(this)
                    {
                        in = null;
                    }
                }
                catch (Exception e)
                {
                    // there is nothing we can do: ignore
                }
            }

            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (Exception e)
                {
                    // there is nothing we can do: ignore
                }
            }
        }
    }


    public void run()
    {
        try
        {
            conn = (SocketConnection) (Connector.open(url));
            // Disable Nagle algorithm:
            conn.setSocketOption(SocketConnection.DELAY, 0);
            out = conn.openOutputStream();
            in = conn.openInputStream();

            startWriteQueue();
        }
        catch (IOException e)
        {
            listener.handleError("Error opening socket connection to: '" +
                                 url + "'");
            return;
        }

        while (!aborting)
        {
            try
            {
                byte[] lengthBuf = new byte[LENGTH_MAX_DIGITS];
                readFully(in, lengthBuf);
                int length = readLength(lengthBuf);
                byte[] temp = new byte[length];
                readFully(in, temp);
                if (listener != null)
                {
                    listener.handleMessage(temp);
                }
            }
            catch (IOException ex)
            {
                close();

                if (listener != null)
                {
                    listener.handleError("I/O Exception: " + ex.getMessage());
                }
            }
        }
    }


    void queueMessageForSending(byte[] data)
    {
        if (data.length > MAX_MESSAGE_LENGTH)
        {
            throw new IllegalArgumentException(
                           "Message too long: limit is " +
                           MAX_MESSAGE_LENGTH + " bytes");
        }

        synchronized(writeQueue)
        {
            writeQueue.addElement(data);
            writeQueue.notify();
        }
    }


    private void sendMessage(byte[] data)
        throws IOException
    {
        byte[] buf = new byte[LENGTH_MAX_DIGITS + data.length];
        writeLength(data.length, buf);
        System.arraycopy(data, 0, buf, LENGTH_MAX_DIGITS, data.length);
        out.write(buf);
        out.flush();
    }


    private void startWriteQueue()
    {
        Runnable writer = new Runnable()
        {
            public void run()
            {
                while (!aborting)
                {
                    if (!writeQueue.isEmpty())
                    {
                        byte[] sendData = (byte[]) writeQueue.elementAt(0);
                        try
                        {
                            sendMessage(sendData);
                            writeQueue.removeElement(sendData);

                            // One might want to make a callback
                            // to the listener to inform it that
                            // the message has been actually sent.
                        }
                        catch (IOException e)
                        {
                            close();

                            // the reader calls parent.handleClose()
                        }
                    }

                    synchronized(writeQueue)
                    {
                        if (writeQueue.size() == 0)
                        {
                            try
                            {
                                writeQueue.wait();
                            }
                            catch (InterruptedException e)
                            {
                                // can't happen in MIDP
                            }
                        }
                    }
                }
            }
        };

        Thread writeThread = new Thread(writer);
        writeThread.start();
    }


    private static void readFully(InputStream in, byte[] buffer)
        throws IOException
    {
        int bytesRead = 0;
        while (bytesRead < buffer.length)
        {
            int count = in.read(buffer, bytesRead, buffer.length - bytesRead);
            if (count == -1)
            {
                throw new IOException("Input stream closed");
            }
            bytesRead += count;
        }
    }


    private static void writeLength(int value, byte[] buffer)
    {
        for (int i = LENGTH_MAX_DIGITS - 1; i >= 0; --i)
        {
            buffer[i] = (byte)(ZERO + value % 10);
            value = value / 10;
        }
    }


    private static int readLength(byte[] buffer)
    {
        int value = 0;
        for (int i = 0; i < LENGTH_MAX_DIGITS; ++i)
        {
            value *= 10;
            value += buffer[i] - ZERO;
        }
        return value;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?