1 Reply Latest reply on Jul 5, 2007 6:12 AM by ion gion

    using localconnection to pass a NetConnection object


      According to the docs, LocalConnection can be used to pass objects between two swf files.

      Is it possible to send a NetConnection or NetStream object from one swf file to another to redirect a connection / stream without creating a second one?

      Also, if I create a second NetConnection object from a different swf file, is that going to eat up another connection on the flash media server, or will it all be multiplexed through one?

        • 1. Re: using localconnection to pass a NetConnection object
          ion gion

          Well I have discovered that you can not do that. Why ?
          Well, LocalConnection is very Operating System dependent, it uses Mutexes and SharedMemory.
          For this reason, when simple objects are transfered like Numbers, Arrays, Strings, Bools, there is no problem on sending them between flash because what is guess is that flash sends copies to SharedMemory.
          But when complex objects are sent like NetStream, NetConnection, MovieClip, the problems appear. Why ? Because to pass these objects you have to pass addresses of pointers to memory of a different process and this highly unreliable. This is basic C programming from what i remember from highschool.

          Lets assume there are 2 swfs in the same page communicating with LocalConnection, and lets name them SWF_ONE and SWF_TWO, here is what happens:

          // in SWF_ONE
          char *buf = malloc(SIZE_NEEDED_MEMORY_OF_SWF_ONE);
          // above OS allocates from address 1024 to 1024+SIZE_NEEDED_MEMORY_OF_SWF_ONE-1

          // in SWF_TWO
          char *buf = malloc(SIZE_NEEDED_MEMORY_OF_SWF_TWO);
          // above OS allocates from address 4096 to 4096+SIZE_NEEDED_MEMORY_OF_SWF_ONE-1

          But your computer has 1 memory, there is no concept of many memories(even though physically you might have 3+ pieces), and that memory is referenced with numbers from 0 to MEMORY_SIZE.

          Now comes the hard part:

          You may assume that when SWF_ONE needs some memory for storing for example _root element, it reserves some addresses from 0 to SIZE_NEEDED_FOR_ROOT_OF_SWF_ONE, but this 0 is relative to the location 1024 where os did the allocation for SWF_ONE in first place.

          The same might happen with SWF_TWO, so as you can see, you cannot just pass addresses of those pointers because address 0 for SWF_ONE might mean something totally different for SWF_TWO.

          Thats why hared memory is used, it creates a zone in memory where pointers can have absolute positions, memory addresses, where everyone sees the same thing.

          The things above are not exactly what happens but you can get the idea why complex data cannot be sent over LocalConnection.

          It would have been nice if from the start every SWF would use only shared memory. Then that problem would disappear and we could sent even movieclips and sound through LocalConnection.