[flashproxy/rtmfp] Factor out common socket error–handling code.

dcf at torproject.org dcf at torproject.org
Fri Jun 10 13:59:55 UTC 2011


commit d9a0753a77f367b9eff3bfd3788b517cd822bc52
Author: David Fifield <david at bamsoftware.com>
Date:   Fri Jun 10 03:33:54 2011 -0700

    Factor out common socket error–handling code.
---
 swfcat.as |   58 ++++++++++++++++++++++------------------------------------
 1 files changed, 22 insertions(+), 36 deletions(-)

diff --git a/swfcat.as b/swfcat.as
index 2afde01..4aa430b 100644
--- a/swfcat.as
+++ b/swfcat.as
@@ -277,6 +277,7 @@ import flash.events.EventDispatcher;
 import flash.events.IOErrorEvent;
 import flash.events.ProgressEvent;
 import flash.events.SecurityErrorEvent;
+import flash.events.TextEvent;
 import flash.net.Socket;
 import flash.utils.ByteArray;
 import flash.utils.clearTimeout;
@@ -422,29 +423,29 @@ class ProxyPair extends EventDispatcher
         this.r2c_schedule = [];
     }
 
+    /* Return a function that shows an error message and closes the other half
+       of a communication pair. */
+    private function socket_error(message:String, other:Socket):Function
+    {
+        return function(e:Event):void {
+            if (e is TextEvent)
+                log(message + ": " + (e as TextEvent).text + ".");
+            else
+                log(message + ".");
+            if (other && other.connected)
+                other.close();
+            dispatchEvent(new Event(Event.COMPLETE));
+        };
+    }
+
     public function connect():void
     {
         s_r = new Socket();
 
         s_r.addEventListener(Event.CONNECT, relay_connected);
-        s_r.addEventListener(Event.CLOSE, function (e:Event):void {
-            log("Relay: closed.");
-            if (s_c && s_c.connected)
-                s_c.close();
-            dispatchEvent(new Event(Event.COMPLETE));
-        });
-        s_r.addEventListener(IOErrorEvent.IO_ERROR, function (e:IOErrorEvent):void {
-            log("Relay: I/O error: " + e.text + ".");
-            if (s_c && s_c.connected)
-                s_c.close();
-            dispatchEvent(new Event(Event.COMPLETE));
-        });
-        s_r.addEventListener(SecurityErrorEvent.SECURITY_ERROR, function (e:SecurityErrorEvent):void {
-            log("Relay: security error: " + e.text + ".");
-            if (s_c && s_c.connected)
-                s_c.close();
-            dispatchEvent(new Event(Event.COMPLETE));
-        });
+        s_r.addEventListener(Event.CLOSE, socket_error("Relay: closed", s_c));
+        s_r.addEventListener(IOErrorEvent.IO_ERROR, socket_error("Relay: I/O error", s_c));
+        s_r.addEventListener(SecurityErrorEvent.SECURITY_ERROR, socket_error("Relay: security error", s_c));
         s_r.addEventListener(ProgressEvent.SOCKET_DATA, relay_to_client);
 
         log("Relay: connecting to " + addr_r.host + ":" + addr_r.port + ".");
@@ -458,24 +459,9 @@ class ProxyPair extends EventDispatcher
         s_c = new Socket();
 
         s_c.addEventListener(Event.CONNECT, client_connected);
-        s_c.addEventListener(Event.CLOSE, function (e:Event):void {
-            log("Client: closed.");
-            if (s_r && s_r.connected)
-                s_r.close();
-            dispatchEvent(new Event(Event.COMPLETE));
-        });
-        s_c.addEventListener(IOErrorEvent.IO_ERROR, function (e:IOErrorEvent):void {
-            log("Client: I/O error: " + e.text + ".");
-            if (s_r && s_r.connected)
-                s_r.close();
-            dispatchEvent(new Event(Event.COMPLETE));
-        });
-        s_c.addEventListener(SecurityErrorEvent.SECURITY_ERROR, function (e:SecurityErrorEvent):void {
-            log("Client: security error: " + e.text + ".");
-            if (s_r && s_r.connected)
-                s_r.close();
-            dispatchEvent(new Event(Event.COMPLETE));
-        });
+        s_c.addEventListener(Event.CLOSE, socket_error("Client: closed", s_r));
+        s_c.addEventListener(IOErrorEvent.IO_ERROR, socket_error("Client: I/O error", s_r));
+        s_c.addEventListener(SecurityErrorEvent.SECURITY_ERROR, socket_error("Client: security error", s_r));
         s_c.addEventListener(ProgressEvent.SOCKET_DATA, client_to_relay);
 
         log("Client: connecting to " + addr_c.host + ":" + addr_c.port + ".");





More information about the tor-commits mailing list