1) Although dbg will be dropped from being supported or bundled with PHPeclipse,...
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.core / src / net / sourceforge / phpdt / internal / debug / core / PHPDBGPacket.java
index 03d3350..337a556 100644 (file)
@@ -10,47 +10,62 @@ Contributors:
 **********************************************************************/
 package net.sourceforge.phpdt.internal.debug.core;
 
+import java.io.IOException;
 import java.io.OutputStream;
 import java.util.Vector;
-import java.io.IOException;
+
+/**
+ *
+ * TODO: Added a try catch block in sendPacket to avoid an execption in case the PHP script
+ * has finished and DBG isn't ready for receiving any new frame.
+ * (This happens when mouse is hovering over variable or user switches to watch expression)
+ * It is not clear why DBG isn't sending any SCRIPT_END event (or something similar),
+ * or whether there is another way to check whether DBG is listening or not.
+ * For the moment this is only a workaround.
+ *
+ */
 public class PHPDBGPacket {
 
-       private static final int PACKET_HEADER_SIZE= 16;
-       private char[] packetHeader= new char[PACKET_HEADER_SIZE];
-       private int packetSize;
-       private Vector frames= new Vector();
-       
-       public PHPDBGPacket(char[] packetType) {
-               PHPDBGBase.copyChars(packetHeader, PHPDBGBase.DBGSYNC, 4);
-               PHPDBGBase.copyCharsTo(packetHeader, packetType, 4, 4);
+       private static final int        PACKET_HEADER_SIZE      = 16;
+       private char[]                  packetHeader            = new char[PACKET_HEADER_SIZE];
+       private int                             packetSize;
+       private Vector                          frames                          = new Vector ();
+
+       public PHPDBGPacket (char[] packetType) {
+               PHPDBGBase.copyChars (packetHeader, PHPDBGBase.DBGSYNC, 4);
+               PHPDBGBase.copyCharsTo (packetHeader, packetType, 4, 4);
        }
 
-       public void addFrame(PHPDBGFrame frame) {
-               frames.add(frame);
-               packetSize+= frame.getSize();
+       public void addFrame (PHPDBGFrame frame) {
+               frames.add (frame);
+               packetSize += frame.getSize ();
        }
-       
-       public void sendPacket(OutputStream out) throws IOException {
-               int i;
+
+       public void sendPacket (OutputStream out) throws IOException {
+               int             i;
                PHPDBGFrame frame;
+
+//             System.out.println ("PHPDBGPacket: sendPacket");
                
-               PHPDBGBase.copyCharsTo(packetHeader, PHPDBGBase.IntToChar4(packetSize), 4, 12);
-               
-               // Send packet header
-               out.write(PHPDBGBase.CharArrayToByteArray(packetHeader));
-               out.flush();
-
-               // Send Frames
-               for(i=0; i < frames.size(); i++) {
-                       // Header of frame
-                       frame= (PHPDBGFrame)frames.get(i);
-                       out.write(PHPDBGBase.CharArrayToByteArray(frame.getHeader()));
-                       out.flush();
-                       if (frame.getSizeOfData() > 0) {
-                               // Data of frame
-                               out.write(PHPDBGBase.CharArrayToByteArray(frame.getFrameData()));
-                               out.flush();
+               PHPDBGBase.copyCharsTo (packetHeader, PHPDBGBase.IntToChar4 (packetSize), 4, 12);
+
+               try {
+                       out.write (PHPDBGBase.CharArrayToByteArray (packetHeader));                             // Send packet header
+                       out.flush ();                                                               //
+
+                       for (i = 0; i < frames.size (); i++) {                                                  // Send Frames
+                               frame = (PHPDBGFrame) frames.get (i);                                                   // Header of frame
+
+                               out.write (PHPDBGBase.CharArrayToByteArray (frame.getHeader ()));       // Convert the char buffer to a byte buffer and send
+                               out.flush ();
+
+                               if (frame.getSizeOfData () > 0) {                                       // If there is a data frame
+                                       out.write (PHPDBGBase.CharArrayToByteArray (frame.getFrameData ()));// Convert the data char buffer to a byte buffer and send
+                                       out.flush ();
+                               }
                        }
                }
+               catch (Exception e) {
+               }
        }
-}
\ No newline at end of file
+}