var forge=require("./forge");require("./util");var net=module.exports=forge.net=forge.net||{};net.socketPools={},net.createSocketPool=function(e){e.msie=e.msie||!1;var o=e.flashId,t=document.getElementById(o);t.init({marshallExceptions:!e.msie});var c={id:o,flashApi:t,sockets:{},policyPort:e.policyPort||0,policyUrl:e.policyUrl||null};net.socketPools[o]=c,!0===e.msie?c.handler=function(e){if(e.id in c.sockets){var o;switch(e.type){case"connect":o="connected";break;case"close":o="closed";break;case"socketData":o="data";break;default:o="error"}setTimeout(function(){c.sockets[e.id][o](e)},0)}}:c.handler=function(e){if(e.id in c.sockets){var o;switch(e.type){case"connect":o="connected";break;case"close":o="closed";break;case"socketData":o="data";break;default:o="error"}c.sockets[e.id][o](e)}};var n="forge.net.socketPools['"+o+"'].handler";return t.subscribe("connect",n),t.subscribe("close",n),t.subscribe("socketData",n),t.subscribe("ioError",n),t.subscribe("securityError",n),c.destroy=function(){for(var o in delete net.socketPools[e.flashId],c.sockets)c.sockets[o].destroy();c.sockets={},t.cleanup()},c.createSocket=function(e){e=e||{};var o=t.create(),n={id:o,connected:e.connected||function(e){},closed:e.closed||function(e){},data:e.data||function(e){},error:e.error||function(e){}};return n.destroy=function(){t.destroy(o),delete c.sockets[o]},n.connect=function(e){var n=e.policyUrl||null,s=0;null===n&&0!==e.policyPort&&(s=e.policyPort||c.policyPort),t.connect(o,e.host,e.port,s,n)},n.close=function(){t.close(o),n.closed({id:n.id,type:"close",bytesAvailable:0})},n.isConnected=function(){return t.isConnected(o)},n.send=function(e){return t.send(o,forge.util.encode64(e))},n.receive=function(e){var c=t.receive(o,e).rval;return null===c?null:forge.util.decode64(c)},n.bytesAvailable=function(){return t.getBytesAvailable(o)},c.sockets[o]=n,n},c},net.destroySocketPool=function(e){e.flashId in net.socketPools&&net.socketPools[e.flashId].destroy()},net.createSocket=function(e){var o=null;return e.flashId in net.socketPools&&(o=net.socketPools[e.flashId].createSocket(e)),o};