Java Mine
ContentsIndexHome
PreviousUpNext
MineTCPSocket.HandleTCPClientComm Method
Syntax
Java
public void HandleTCPClientComm(Socket clientSocket) throws IOException, InterruptedException;

public void HandleTCPClientComm(Socket tcpClient) throws IOException//TcpClient tcpClient) throws Exception , InterruptedException { Extensions.debug("Reached HandleTCPClientComm", 30); OutputStream outputStream = tcpClient.getOutputStream(); InputStream inputStream = tcpClient.getInputStream(); try { _RunningClients++; int ReadTimeout = 30000; //clientStream.ReadTimeout = 30000;//30 seconds 

byte[] inboundBuffer = new byte[4096]; int bytesRead; 

while (true) { Extensions.debug("Reached HandleTCPClientComm Main loop", 50); bytesRead = 0; 

try { bytesRead = inputStream.read(inboundBuffer, 0, 4096); //System.out.println(bytesRead); } catch (Exception e) { //a socket error has occurred break; } 

if (bytesRead < 0) break; //eof if (bytesRead == 0) break;//the client has disconnected from the server if (bytesRead < 16) { Extensions.debug("Reached HandleTCPClientComm bytesRead < 16"); try { String str = Extensions.byteToAsciiString(inboundBuffer, 0, bytesRead); if (str == "TCPTest") { Extensions.debug("TCPTest"); byte[] ResponseBuffer = Extensions.asciiStringToByte("Success"); outputStream.write(ResponseBuffer, 0, ResponseBuffer.length); outputStream.flush(); } else if (str.toLowerCase().startsWith("gwp=")) { Extensions.debug("gwp="); byte[] ResponseBuffer; int gwID = Integer.parseInt((str.toLowerCase().replace("gwp=", "").replace("/0", ""))); Gateway Gateway = ServerInstance.FindGateway(gwID); if (Gateway != null) { if (!Gateway.HasUrgentTraffic) ResponseBuffer = Extensions.asciiStringToByte("OK"); else ResponseBuffer = Extensions.asciiStringToByte("CB"); 

outputStream.write(ResponseBuffer, 0, ResponseBuffer.length); outputStream.flush(); } } else { Extensions.debug("Entered neither if statement.", 50); } } catch (Exception ex) { Extensions.debug(ex.toString()); ServerInstance.OnLogException(ex, "MineTCPSocket.HandleTCPClientComm invalid ping"); } continue; } 

//ELSE IF bytesRead >= 16 byte[] Message = new byte[bytesRead]; System.arraycopy(inboundBuffer, 0, Message, 0, bytesRead); Extensions.debug("Initial Message: " + Extensions.hexStr(Message), 30); 

Gateway SensorGateway = null; 

InboundPacket Packet = new InboundPacket(); Packet.setReceivedDate(new Date(System.currentTimeMillis())); Packet.setVersion(Message[8]); if (Packet.getVersion() == 1) break; 

Packet.setSequence(Message[9]); Packet.setGatewayID(BitConverter.toInt32(Message, 0)); 

if (Packet.getGatewayID() < Integer.MAX_VALUE) SensorGateway = ServerInstance.FindGateway(Packet.getGatewayID()); 

Packet.setSecurity(BitConverter.toUInt32(Message, 4)); 

boolean needsResetEncryption = false; Extensions.debug("Security: "+Packet.getSecurity(), 30); if (Packet.getSecurity() > 0 && SensorGateway != null) { try { Extensions.debug("Not Decrypted Message: " + Extensions.hexStr(Message), 30); Message = ServerInstance.DecryptPacket(SensorGateway, Message); Extensions.debug("Decrypted Message: " + Extensions.hexStr(Message), 30); eGatewayMessageType responseType = eGatewayMessageType.ToMessageType(Message[12] & 0xFF); //throws exception if the response type is not valid. Extensions.debug(""+responseType, 50); if(responseType == null) { //e.printStackTrace(); Extensions.debug("Decryption failed, invalid response type.", 50); needsResetEncryption = true; } } catch (Exception e) { //e.printStackTrace(); Extensions.debug("Decryption threw exception " + e, 50); needsResetEncryption = true; } 

if(needsResetEncryption) { ServerInstance.resetEncryptionAck(SensorGateway, Packet, outputStream, ServerInstance); continue; } } 

if(needsResetEncryption) { Extensions.debug("nnContinue failed to skip packet.nn", 50); } else { Extensions.debug("Does not need reset.", 50); } 

Packet.setPower(BitConverter.ToUInt16(Message, 10)); Packet.setResponse(Message[12] & 0xFF); Packet.setMore(Message[13] != 0); Packet.setMessageCount(BitConverter.ToUInt16(Message, 14)); Packet.setMessage(Message); 

Extensions.debug(Packet.toString()); 

if (Packet.getGatewayID() == Integer.MAX_VALUE && Packet.getSecurity() == 0 && Packet.getResponse() == 7)//Wifi Lookup from MAC SensorGateway = ServerInstance .FindGatewayByExternalIdentifier(BitConverter.toString(Packet .getMessage(), 16, 6).replace("-", "")); 

if (SensorGateway != null) { //This gets saved in Process Packet we just don't have the client to grab it there String LastInboundIPAddress = String.format("%s -> %s", tcpClient.getRemoteSocketAddress(), tcpClient.getLocalSocketAddress()); byte[] responseBuffer = ServerInstance.ProcessPacket(Packet, SensorGateway); 

outputStream = tcpClient.getOutputStream(); outputStream.write(responseBuffer, 0, responseBuffer.length); outputStream.flush(); 

//Extensions.debug(Extensions.hexStr(responseBuffer), 50); } 

} catch (Exception ex) { ServerInstance.OnLogException(ex, "MineTCPSocket.HandleTCPClientComm(object client)"); } finally { _RunningClients--; tcpClient.close(); } }

@2015 all rights reserved
What do you think about this topic? Send feedback!