|
| 1 | +/** |
| 2 | + * Created on Sep 19, 2004 |
| 3 | + */ |
| 4 | + |
| 5 | +package BNLSProtocol; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStream; |
| 9 | +import java.io.InterruptedIOException; |
| 10 | +import java.io.OutputStream; |
| 11 | +import java.net.Socket; |
| 12 | + |
| 13 | +import util.Constants; |
| 14 | +import util.Controller; |
| 15 | +import util.Out; |
| 16 | +import BNLSProtocol.BNLSConnectionThread; |
| 17 | + |
| 18 | +/** |
| 19 | + * |
| 20 | + * Individual thread to accept data on a BNLS Connection Seperates out the |
| 21 | + * packets and contains its own Parser class for interpretting them |
| 22 | + */ |
| 23 | +public class BNLSConnectionThread extends Thread |
| 24 | +{ |
| 25 | + /** Total Connection Count * */ |
| 26 | + public static int connectionCount = 0; |
| 27 | + |
| 28 | + /** Next item in the linked list */ |
| 29 | + private BNLSConnectionThread bNextList = null; |
| 30 | + private BNLSConnectionThread bPrevList = null; |
| 31 | + |
| 32 | + /** Thread's Socket */ |
| 33 | + private Socket socket = null; |
| 34 | + |
| 35 | + private OutputStream out = null; |
| 36 | + public String IP = null; |
| 37 | + |
| 38 | + /* |
| 39 | + * Note this is only an InputStream. I spent hours trying to figure out what |
| 40 | + * was wrong with the code when all along it was that "InputStreamReader" |
| 41 | + * was changing some values. |
| 42 | + */ |
| 43 | + private InputStream in = null; |
| 44 | + |
| 45 | + /** Current Thread Count */ |
| 46 | + private static int threadCount = 0; |
| 47 | + |
| 48 | + /** Thread ID of this instance */ |
| 49 | + public int threadID; |
| 50 | + |
| 51 | + |
| 52 | + /** Set the next item in the linked list */ |
| 53 | + public void setNext(BNLSConnectionThread bNext){ |
| 54 | + bNextList = bNext; |
| 55 | + } |
| 56 | + /** Get the next item in the linked list */ |
| 57 | + public BNLSConnectionThread getNext(){ |
| 58 | + return bNextList; |
| 59 | + } |
| 60 | + /** Set the last item in the linked list */ |
| 61 | + public void setPrev(BNLSConnectionThread bPrev){ |
| 62 | + bPrevList = bPrev; |
| 63 | + } |
| 64 | + /** Get the Last item in the linked list */ |
| 65 | + public BNLSConnectionThread getPrev(){ |
| 66 | + return bPrevList; |
| 67 | + } |
| 68 | + |
| 69 | + /** Destry removed this thread from the Linked List. */ |
| 70 | + public void Destroy() { |
| 71 | + if (bPrevList == null) { |
| 72 | + if (bNextList == null ) |
| 73 | + Controller.lLinkedHead = null; |
| 74 | + else |
| 75 | + Controller.lLinkedHead = bNextList; |
| 76 | + } else { |
| 77 | + if (bNextList == null) { |
| 78 | + bPrevList.setNext(null); |
| 79 | + } else { |
| 80 | + bPrevList.setNext(bNextList); |
| 81 | + bNextList.setPrev(bPrevList); |
| 82 | + } |
| 83 | + } |
| 84 | + try { |
| 85 | + out.close(); |
| 86 | + in.close(); |
| 87 | + socket.close(); |
| 88 | + }catch (IOException e){ |
| 89 | + e.printStackTrace(); |
| 90 | + Out.error("Thread " + threadID, "IO Error:" + e.toString()); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + /** Creates the Interpreter Thread with a given socket */ |
| 97 | + public BNLSConnectionThread(Socket cSocket) |
| 98 | + { |
| 99 | + super("BNLSConnectionThread"); |
| 100 | + threadID = threadCount++; |
| 101 | + connectionCount++; |
| 102 | + socket = cSocket; |
| 103 | + setDaemon(true);// make this Thread Not Hold up the Program |
| 104 | + } |
| 105 | + /** Runs the Connection thread(blocks until connection done) */ |
| 106 | + public void run() |
| 107 | + { // Run the connection thread |
| 108 | + |
| 109 | + // Check for too many thread instances(dont want to overload server) |
| 110 | + if (threadCount > Constants.maxThreads) |
| 111 | + { |
| 112 | + Out.error("JBLS", "Max Threads Exceeded. Current count: " + threadCount + ". Max count: " + Constants.maxThreads + ". Connection terminated."); |
| 113 | + threadCount--; |
| 114 | + Destroy(); |
| 115 | + return; |
| 116 | + } |
| 117 | + this.IP = socket.getInetAddress().getHostAddress(); |
| 118 | + |
| 119 | + // Check for IPStatistics for this guy |
| 120 | + if (!IpAuth.checkAuth(IP)) |
| 121 | + { |
| 122 | + Out.error("Thread " + threadID, "IP Not Authorized. Thread Terminated."); |
| 123 | + threadCount--; |
| 124 | + Destroy(); |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + int[] ipHash = Hashing.BrokenSHA1.calcHashBuffer(this.IP.getBytes()); |
| 129 | + this.IP = util.PadString.padHex(ipHash[0], 8) + |
| 130 | + util.PadString.padHex(ipHash[1], 8) + |
| 131 | + util.PadString.padHex(ipHash[2], 8) + |
| 132 | + util.PadString.padHex(ipHash[3], 8) + |
| 133 | + util.PadString.padHex(ipHash[4], 8); |
| 134 | + if(Controller.stats != null) Controller.stats.onConnection(this.IP); |
| 135 | + |
| 136 | + OutPacketBuffer outputLine; |
| 137 | + BNLSParse myParse = new BNLSParse(this);// create BNLS Parsing Class |
| 138 | + boolean parsing = true; |
| 139 | + byte errCount = 0; |
| 140 | + |
| 141 | + try |
| 142 | + { |
| 143 | + // Retrieve Input and output Streams |
| 144 | + out = socket.getOutputStream(); |
| 145 | + in = socket.getInputStream(); |
| 146 | + socket.setSoTimeout(60000);// 60 second timeout |
| 147 | + socket.setKeepAlive(true);// keep connection alive |
| 148 | + |
| 149 | + Out.debug("Thread " + threadID, "Streams created."); |
| 150 | + |
| 151 | + while (parsing){ |
| 152 | + outputLine = null; |
| 153 | + try |
| 154 | + { |
| 155 | + int i;// input integer(read from input string) |
| 156 | + short pLength;// packet Length |
| 157 | + byte packetID; |
| 158 | + i = in.read();// read in first byte of Packet Length |
| 159 | + if (i == -1) throw new IOException("Connection terminated."); |
| 160 | + pLength = (short) ((i << 0) & 0x000000FF); |
| 161 | + i = in.read();// Second Packet Length Byte |
| 162 | + if (i == -1) throw new IOException("Connection terminated."); |
| 163 | + pLength |= (short) ((i << 8) & 0x0000FF00); |
| 164 | + packetID = (byte) in.read();// Read in PacketID |
| 165 | + |
| 166 | + InPacketBuffer inPacket = new InPacketBuffer(packetID, pLength); |
| 167 | + int bytesRead = 0; |
| 168 | + while (bytesRead < pLength - 3) |
| 169 | + {// read in each byte |
| 170 | + i = in.read(); |
| 171 | + if (i == -1) |
| 172 | + throw new IOException("Connection terminated."); |
| 173 | + inPacket.add((char)i);// add to packet |
| 174 | + bytesRead++; |
| 175 | + } |
| 176 | + if (Constants.displayPacketInfo || Constants.displayParseInfo) |
| 177 | + Out.info("Thread " + threadID, "Input Received. Packet ID: 0x" + ((packetID & 0xF0) >> 4) + "" + Integer.toString((packetID & 0x0F) >> 0, 16) + " Length: " + pLength + "."); |
| 178 | + if (Constants.debugInfo && packetID == 0) |
| 179 | + Out.debug("Thread " + threadID, "0x00 received from " + IP); |
| 180 | + |
| 181 | + outputLine = myParse.parseInput(inPacket); |
| 182 | + if (outputLine != null) |
| 183 | + { |
| 184 | + if (Constants.displayPacketInfo) |
| 185 | + Out.info("Thread " + threadID, "Sending response."); |
| 186 | + out.write(outputLine.getBuffer()); |
| 187 | + outputLine = null; |
| 188 | + }else{// outputline=null, no response |
| 189 | + if (Constants.displayPacketInfo) |
| 190 | + Out.info("Thread " + threadID, "No response.");// +outputLine.toString()); |
| 191 | + } |
| 192 | + }catch (InvalidPacketException e){ |
| 193 | + Out.error("Thread " + threadID, "Invalid Packet: " + e.toString()); |
| 194 | + errCount++; |
| 195 | + if (errCount > 2) break; |
| 196 | + }catch (InterruptedIOException e){ |
| 197 | + Out.error("Thread " + threadID, "Connection Timeout"); |
| 198 | + parsing = false; |
| 199 | + break; |
| 200 | + }catch (IOException e){ |
| 201 | + Out.info("Thread " + threadID, "Disconnected (" + e.getMessage() + ")"); |
| 202 | + parsing = false; |
| 203 | + break; |
| 204 | + }catch (BNLSException e){ // Fatal BNLS Error(Not authorized, etc.) |
| 205 | + Out.error("Thread " + threadID, "BNLS Exception: " + e.toString()); |
| 206 | + parsing = false; |
| 207 | + break; |
| 208 | + }// end inner Try-Catch |
| 209 | + }// end input while loop |
| 210 | + |
| 211 | + // Take Care of Streams/Sockets |
| 212 | + out.close(); |
| 213 | + in.close(); |
| 214 | + socket.close(); |
| 215 | + |
| 216 | + }catch (IOException e){ |
| 217 | + e.printStackTrace(); |
| 218 | + Out.error("Thread " + threadID, "IO Error:" + e.toString()); |
| 219 | + } |
| 220 | + |
| 221 | + Out.debug("Thread " + threadID, "Closed"); |
| 222 | + threadCount--; |
| 223 | + Destroy(); |
| 224 | + |
| 225 | + }// end of run method |
| 226 | + public boolean send(OutPacketBuffer data){ |
| 227 | + try{ |
| 228 | + out.write(data.getBuffer()); |
| 229 | + }catch(Exception e){ return false;} |
| 230 | + return true; |
| 231 | + } |
| 232 | +} |
0 commit comments