📄 clientcontroller.java
字号:
if ( invokeId.equals( "measure" ) ) onMeasure ( arguments , packetX ); else if ( invokeId.equals( "_error" ) ) onResult ( arguments , packetX ); else if ( invokeId.equals( "_result" ) ) onResult ( arguments , packetX ); else if ( invokeId.equals( "onStatus" ) ) onStatus ( arguments , packetX ); else if ( invokeId.equals( "connect" ) ) onConnect ( arguments ); else onInvoke ( arguments ); } catch ( IOException exception ) { // decode exception happened, trace it System.out.println( Library.CODEEX ); exception.printStackTrace( ); // create status info WrapperMap status = new WrapperMap( Library.STATUSKEYS , Library.AMFERRORARR ); status.put( "description" , exception.getMessage( ) ); call( "onStatus" , new Wrapper( status ) ); } } /** * Connection object from client, connection initialization * @param argumentsX Conneciton arguments **/ public void onConnect ( WrapperList argumentsX ) { // System.out.println( System.currentTimeMillis( ) + " " + id + " ClientController.onConnect " + argumentsX.size( ) ); // get info object, which is the third part after invoke id and channel WrapperMap info = argumentsX.getMap( 2 ); String customId = info.get( "app" ).stringValue; // get agent info ip = socketController.socket.socket( ).getInetAddress( ).getHostName( ); agent = info.get( "flashVer" ).stringValue; referrer = info.get( "swfUrl" ).stringValue; application = Server.getApplication( customId ); if ( application != null ) { // setting read notify stepping sendReadStepServer( readStepServer ); sendReadStepClient( readStepClient ); // first ping event //sendPing( 8 , 0 , -1 , -1 ); sendPing( 0 , 0 , -1 , -1 ); // remove unnecessary arguments argumentsX.remove( 0 ); argumentsX.remove( 0 ); argumentsX.remove( 0 ); // enter client, sync is needed because multiple client threads can enter an application synchronized ( application ) { application.onEnter( client , argumentsX ); } } else reject( new Wrapper( Library.NOINST + customId ) ); } /** * Normal invoke from client * @param argumentsX arguments **/ public void onInvoke ( WrapperList argumentsX ) { if ( invokeListener != null ) { String invokeId = argumentsX.getString( 0 ); double invokeChannel = argumentsX.getDouble( 1 ); // System.out.println( System.currentTimeMillis( ) + " " + id + " ClientController.onInvoke " + invokeId ); // remove id, channel and null argumentsX.remove( 0 ); argumentsX.remove( 0 ); argumentsX.remove( 0 ); // store invoke id for further response, dispatch event if ( invokeChannel != 0 ) outgoingInvokes.put( invokeChannel , invokeId ); invokeListener.onEvent( new InvokeEvent( invokeId , invokeChannel , client , argumentsX ) ); } } /** * Invoke result from client. * @param argumentsX arguments **/ public void onResult ( WrapperList argumentsX , RtmpPacket packetX ) { // get channel and id double invokeChannel = argumentsX.getDouble( 1 ); String invokeId = incomingInvokes.remove( invokeChannel ); // System.out.println( System.currentTimeMillis( ) + " " + id + " ClientController.onResult " + invokeChannel + " " + invokeId ); if ( invokeId.equals( "connect" ) ) { // get result info WrapperMap info = argumentsX.getMap( 3 ); String code = info.getString( "code" ); // following invoke channels must be over 1 incomingInvokes.put( ( double ) 1 , invokeId ); // if success, set accepted true, else close if ( code.equals( StatusEvent.SUCCESS ) ) { accepted = true; // dispatch success if ( statusListener != null ) statusListener.onEvent( new StatusEvent( info , client ) ); } else { // first dispatch close if ( statusListener != null ) statusListener.onEvent( new StatusEvent( info , client ) ); // then close close( ); } } else if ( invokeId.equals( "createStream" ) ) { // invoke result for createStream streamController.onCreateStream( argumentsX.getDouble( 3 ) ); } } /** * Status message from client * @param argumentsX arguments info * @param packetX rtmp packet */ public void onStatus ( WrapperList argumentsX , RtmpPacket packetX ) { // System.out.println( System.currentTimeMillis( ) + " " + id + " ClientController.onStatus " + argumentsX ); //for ( Wrapper wrapper : argumentsX ) System.out.println( wrapper ); if ( packetX.bodyType == 0x12 ) { WrapperMap info = argumentsX.getMap( 1 ); for ( String key : info.keySet() ) System.out.println( key + " " + info.get( key ) ); return; } WrapperMap info = argumentsX.getMap( 3 ); if ( statusListener != null ) statusListener.onEvent( new StatusEvent( info , client ) ); } /** * Bandwidth meause request from client, storing read time and create string if needed * @param argumentsX arguments * @param packetX packet **/ public void onMeasure ( WrapperList argumentsX , RtmpPacket packetX ) { // System.out.println( System.currentTimeMillis( ) + " " + id + " ClientController.onMeasure " ); // store read time immediately // extract chanel double read = System.currentTimeMillis( ); double size = argumentsX.getMap( 3 ).getDouble( "size" ); double channel = argumentsX.getDouble( 1 ); String value; String oldvalue = argumentsX.getMap( 3 ).getString( "value" ); // create string with desired length - desired bytes //System.out.println( "size: " + size + " " + oldvalue.length( ) ); char [ ] chars = new char [ ( int ) size ]; value = new String( chars ); // create message WrapperMap message = new WrapperMap( ); message.put( "read" , read ); message.put( "send" , System.currentTimeMillis( ) ); message.put( "value" , value ); //System.out.println( "read: " + (long)message.getDouble( "read" ) ); //System.out.println( "send: " + (long)message.getDouble( "send" ) ); //System.out.println( "value: " + message.getString( "value" ).length( ) ); // send back message with values invoke( "_result" , channel , 0 , new WrapperList( new Wrapper( message ) ) ); } /** * Invoke calls to client * @param idX Invoke Id * @param argumentsX Invoke arguments */ public void call ( String callIdX ) { invoke( callIdX , 0 , 0 , new WrapperList( ) ); } public void call ( String callIdX , Wrapper argumentX ) { invoke( callIdX , 0 , 0 , new WrapperList( argumentX ) ); } public void call ( String callIdX , Wrapper argumentX , int flvChannelX ) { invoke( callIdX , 0 , flvChannelX , new WrapperList( argumentX ) ); } public void call ( String callIdX , WrapperList argumentsX ) { invoke( callIdX , 0 , 0 , argumentsX ); } public void call ( String callIdX , WrapperList argumentsX , int flvChannelX ) { invoke( callIdX , 0 , flvChannelX , argumentsX ); } public void callResult ( double channelX , Wrapper argumentX ) { invoke( "_result" , channelX , 0 , new WrapperList( argumentX ) ); } public void callResult ( double channelX , WrapperList argumentsX ) { invoke( "_result" , channelX , 0 , argumentsX ); } public void callResponse ( String callIdX , WrapperList argumentsX , int flvChannelX ) { double invokeChannel = incomingInvokes.size( ); incomingInvokes.put( invokeChannel , callIdX ); invoke( callIdX , invokeChannel , flvChannelX , argumentsX ); } /** * Invoke to client * @param channelX invoke channel * @param argumentsX arguments **/ public void invoke ( String callIdX , double invokeChannelX , int flvChannelX , WrapperList argumentsX ) { // System.out.println( System.currentTimeMillis() + " ClientController.invoke " + callIdX + " " + invokeChannelX ); // create invoke packet RtmpPacket call = new RtmpPacket( ); WrapperList arguments = new WrapperList( ); // create invoke object arguments.add( 0 , new Wrapper( callIdX ) ); arguments.add( 1 , new Wrapper( invokeChannelX ) ); arguments.add( 2 , new Wrapper( ) ); arguments.addAll( argumentsX ); // fill up packet call.flvChannel = flvChannelX; call.bodyType = 0x14; call.body = Encoder.encode( arguments ); // send if ( !closed ) addOutgoingPacket( call ); } /** * Accepts the client * @param wrapperX - an information object passed to the client **/ public void accept ( Wrapper wrapperX ) { // System.out.println( System.currentTimeMillis( ) + " " + id + " ClientController.accept" + closed + " " + mode ); if ( !closed && mode.equals( "passive" ) ) { // create acception status WrapperMap info = new WrapperMap( Library.STATUSKEYS , Library.SUCCESSARR ); info.put( "application" , wrapperX ); RtmpPacket call = new RtmpPacket( ); WrapperList message = new WrapperList( ); // create invoke object message.add( new Wrapper( "_result" ) ); message.add( new Wrapper( 1 ) ); message.add( new Wrapper( ) ); message.add( new Wrapper( info ) ); // fill up packet call.flvChannel = 0; call.bodyType = 0x14; call.body = Encoder.encode( message ); // send back as result, register app addOutgoingPacket( call ); Server.registerClient( application , this ); accepted = true; } } /** * Rejects the client, called from a custom applications. Sends a standard rejection object as onStatus * @param wrapperX - an information object passed to the client */ public void reject ( Wrapper wrapperX ) { // System.out.println( System.currentTimeMillis( ) + " " + id + " ClientController.reject closed: " + closed + " mode: " + mode ); if ( !closed && mode.equals( "passive" )) { // create rejection status WrapperMap info = new WrapperMap( Library.STATUSKEYS , Library.REJECTIONARR ); RtmpPacket call = new RtmpPacket( ); WrapperList message = new WrapperList( ); info.put( "application" , wrapperX ); // send back as result // create invoke object message.add( new Wrapper( "_result" ) ); message.add( new Wrapper( 1 ) ); message.add( new Wrapper( ) ); message.add( new Wrapper( info ) ); // fill up packet call.flvChannel = 0; call.bodyType = 0x14; call.body = Encoder.encode( message ); // send back as result, register app addOutgoingPacket( call ); socketController.takePackets( outgoingList ); socketController.closeInited = true; } } /** * Other methods */ public void detach ( ) { socketController.close( "detach" ); } public void addStreamEventListener ( EventListener streamListenerX ) { streamListener = streamListenerX; } public void addInvokeEventListener ( EventListener invokeListenerX ) { invokeListener = invokeListenerX; } public void addStatusEventListener ( EventListener statusListenerX ) { statusListener = statusListenerX; } public HashMap < Integer , Stream > getPlayers ( ) { if ( !closed ) return streamController.getPlayers( ); else return null; } public HashMap < Integer , Stream > getRouters ( ) { if ( !closed ) return streamController.getRouters( ); else return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -