1. Victor Kotseruba
  2. FlSock

Commits

Victor Kotseruba  committed 1284670

dirty room support

  • Participants
  • Parent commits 773f107
  • Branches default

Comments (0)

Files changed (2)

File FlSock.as

View file
         protected var port:int;
         protected var term:String = String.fromCharCode(1);
         protected var messageStart:String = 'm';
+        protected var roomStart:String = 'r';
         protected var sock:Socket;
         protected var data:String = '';
-        
+
         public function FlSock() {
             super();
             sock = new Socket();
         
         protected function connectHandle(e:Event):void {
             ExternalInterface.addCallback('sendMessage', sendMessage);
+            ExternalInterface.addCallback('joinRoom', joinRoom);
             ExternalInterface.call('connectHandle');
         }
         
         protected function sendMessage(message:String):void {
             this.sock.writeUTFBytes(messageStart + message + term);
         }
+
+        protected function joinRoom(room:String):void {
+            this.sock.writeUTFBytes(roomStart + room + term);
+        }
     }
 }

File FlSock.py

View file
 parser.add_option('-d', dest='domains', default='*',
                   help='allowed domains comma separated, default *')
 
+
+rooms = {}
+
+
 class FlSockChannel(asynchat.async_chat):
-
+    
     ac_in_buffer_size = 1024
     ac_out_buffer_size = 1024
-
+    
     def __init__(self, server, sock, opts):
         asynchat.async_chat.__init__(self, sock)
         self.data = ''
-        self.state = 1
+        self.state = 'mode'
         self.opts = opts
         self.server = server
-        self.flash_ready = True
+        self.flash_ready = False
+        self.room = None
         self.set_terminator(1)
     
     def collect_incoming_data(self, data):
         self.data = self.data + data
         if len(self.data) > self.opts.max_length:
-            self.close()
+            self.handle_close()
     
     def found_terminator(self):
-        if self.state == 1:
+        if self.state == 'mode':
             if self.data == '<':
                 self.push('<?xml version="1.0"?>')
                 self.push('<!DOCTYPE cross-domain-policy')
                     self.push('<allow-access-from domain="%s" to-ports="%i" />' % (domain, self.opts.port))
                 self.push('</cross-domain-policy>')
                 self.close_when_done()
+            elif self.data == 'r':
+                self.flash_ready = True
+                self.state = 'room'
+                self.set_terminator(chr(1))
             elif self.data == 'm':
                 self.flash_ready = True
-                self.data = ''
-                self.state = 2
+                self.state = 'message'
                 self.set_terminator(chr(1))
-        elif self.state == 2:
+        elif self.state == 'message':
             self.state = 1
-            for channel in self.server._map.values():
-                if isinstance(channel, FlSockChannel) and channel.flash_ready:
+            if self.room:
+                for channel in rooms[self.room]:
                     if channel is not self:
                         channel.push(self.data + chr(1))
             self.data = ''
-            self.set_terminator(2)
+            self.set_terminator(1)
+        elif self.state == 'room':
+            self.leave_room()
+            self.room = self.data
+            self.join_room()
+            self.set_terminator(1)
+        self.data = ''
 
+    def join_room(self):
+        if not rooms[self.room]:
+            rooms[self.room] = []
+        rooms[self.room].append(self)
     
+    def leave_room(self):
+        if self.room:
+            rooms[self.room].remove(self)
+            if not rooms[self.room]:
+                del room[self.room]
+    
+    def handle_close(self):
+        self.leave_room()
+        self.close()
+
+
 class FlSockDispatcher(asyncore.dispatcher):
     
     def __init__(self, opts):