コード例 #1
0
        static NeuronSource FindCommandSource(IntPtr commandSocketReference)
        {
            NeuronSource source = null;

            commandSocketReferenceIndex.TryGetValue(commandSocketReference, out source);
            return(source);
        }
コード例 #2
0
        static void DestroyConnection(NeuronSource source)
        {
            if (source == null)
            {
                return;
            }

            // We have to make a lock before removing the source from the maps.
            lock (_sourcesLock) _sources.Remove(source);
            // Now we can delete the source safely!

            if (source.SocketType == SocketType.TCP)
            {
                NeuronDataReader.BRCloseSocket(source.Socket);
                Debug.Log("[Neuron] Disconnected " + source.Address + ":" + source.Port);
            }
            else
            {
                NeuronDataReader.BRCloseSocket(source.Socket);
                Debug.Log("[Neuron] Stopped listening " + source.Port);
            }

            // Unregister the reader callback if this was the last connection.
            if (_sources.Count == 0)
            {
                NeuronDataReader.BRRegisterFrameDataCallback(IntPtr.Zero, null);
            }
        }
コード例 #3
0
        static NeuronSource FindSource(IntPtr socketReference)
        {
            NeuronSource source = null;

            socketReferencesIndex.TryGetValue(socketReference, out source);
            return(source);
        }
コード例 #4
0
 protected void Disconnect()
 {
     NeuronConnection.Disconnect(boundActor.owner);
     UnregisterCallbacks();
     boundActor = null;
     source     = null;
 }
コード例 #5
0
        static void OnSocketStatusChanged(IntPtr customObject, IntPtr socketReference, SocketStatus status, string msg)
        {
            NeuronSource source = FindSource(socketReference);

            if (source != null)
            {
                source.OnSocketStatusChanged(status, msg);
            }
        }
コード例 #6
0
        static void OnFrameDataReceived(IntPtr customObject, IntPtr socketReference, IntPtr header, IntPtr data)
        {
            NeuronSource source = FindSource(socketReference);

            if (source != null)
            {
                source.OnFrameDataReceived(header, data);
            }
        }
コード例 #7
0
        public NeuronActor(NeuronSource owner, int actorID)
        {
            this.owner   = owner;
            this.actorID = actorID;

            if (owner != null)
            {
                owner.RegisterResumeActorCallback(OnResumeFrameData);
                owner.RegisterSuspendActorCallback(OnNoFrameData);
            }
        }
コード例 #8
0
        protected bool Connect()
        {
            source = NeuronConnection.Connect(address, port, commandServerPort, socketType);
            if (source != null)
            {
                boundActor = source.AcquireActor(actorID);
                RegisterCallbacks();
            }

            return(source != null);
        }
コード例 #9
0
 public static void Disconnect(NeuronSource source)
 {
     if (source != null)
     {
         source.Release();
         if (source.referenceCounter == 0)
         {
             DestroyConnection(source);
         }
     }
 }
コード例 #10
0
        public static void Disconnect(NeuronSource source)
        {
            if (source == null)
            {
                return;
            }

            if (source.DecrementReferenceCount() <= 0)
            {
                DestroyConnection(source);
            }
        }
コード例 #11
0
        static void DestroyConnection(NeuronSource source)
        {
            if (source != null)
            {
                if (source.commandSocketReference != IntPtr.Zero)
                {
                    commandSocketReferenceIndex.Remove(source.commandSocketReference);
                }

                source.OnDestroy();

                Guid       guid                   = source.guid;
                string     address                = source.address;
                int        port                   = source.port;
                int        commandServerPort      = source.commandServerPort;
                SocketType socketType             = source.socketType;
                IntPtr     socketReference        = source.socketReference;
                IntPtr     commandSocketReference = source.commandSocketReference;

                connections.Remove(guid);
                socketReferencesIndex.Remove(socketReference);

                if (commandSocketReference != IntPtr.Zero)
                {
                    NeuronDataReader.BRCloseSocket(commandSocketReference);
                    Debug.Log(string.Format("[NeuronConnection] Disconnected from command server {0}:{1}.", address,
                                            commandServerPort));
                }

                if (socketType == SocketType.TCP)
                {
                    NeuronDataReader.BRCloseSocket(socketReference);
                    Debug.Log(string.Format("[NeuronConnection] Disconnected from {0}:{1}.", address, port));
                }
                else
                {
                    NeuronDataReader.BRCloseSocket(socketReference);
                    Debug.Log(string.Format("[NeuronConnection] Stop listening at {0}. {1}", port,
                                            source.guid.ToString("N")));
                }
            }

            if (connections.Count == 0)
            {
                UnregisterReaderCallbacks();
            }
        }
コード例 #12
0
 static void OnCommandResponded(IntPtr customObject, IntPtr commandSocketReference, IntPtr respondHeader, IntPtr respondData)
 {
     if (commandSocketReference == IntPtr.Zero)
     {
         foreach (KeyValuePair <IntPtr, NeuronSource> it in commandSocketReferenceIndex)
         {
             it.Value.OnCommandResponded(respondHeader, respondData);
         }
     }
     else
     {
         NeuronSource source = FindCommandSource(commandSocketReference);
         if (source != null)
         {
             source.OnCommandResponded(respondHeader, respondData);
         }
     }
 }
コード例 #13
0
        public static NeuronSource Connect(string address, int port, int commandServerPort, SocketType socketType)
        {
            NeuronSource source = FindConnection(address, port, socketType);

            if (source != null)
            {
                source.Grab();
                return(source);
            }

            source = CreateConnection(address, port, commandServerPort, socketType);
            if (source != null)
            {
                source.Grab();
                return(source);
            }

            return(null);
        }
コード例 #14
0
        static NeuronSource FindConnection(string address, int port, SocketType socketType)
        {
            NeuronSource source = null;

            foreach (KeyValuePair <Guid, NeuronSource> it in connections)
            {
                if (it.Value.socketType == SocketType.UDP && socketType == SocketType.UDP && it.Value.port == port)
                {
                    source = it.Value;
                    break;
                }
                else if (it.Value.socketType == SocketType.TCP && socketType == SocketType.TCP && it.Value.address == address && it.Value.port == port)
                {
                    source = it.Value;
                    break;
                }
            }
            return(source);
        }
コード例 #15
0
        static NeuronSource CreateConnection(string address, int port, SocketType socketType)
        {
            // Try to make connection with using the native plugin.
            var socket = IntPtr.Zero;

            if (socketType == SocketType.TCP)
            {
                socket = NeuronDataReader.BRConnectTo(address, port);

                if (socket == IntPtr.Zero)
                {
                    Debug.LogError("[Neuron] Connection failed " + address + ":" + port);
                    return(null);
                }

                Debug.Log("[Neuron] Connected " + address + ":" + port);
            }
            else
            {
                socket = NeuronDataReader.BRStartUDPServiceAt(port);

                if (socket == IntPtr.Zero)
                {
                    Debug.LogError("[Neuron] Failed listening " + port);
                    return(null);
                }

                Debug.Log("[Neuron] Started listening " + port);
            }

            // If this is the first connection, register the reader callack.
            if (_sources.Count == 0)
            {
                NeuronDataReader.BRRegisterFrameDataCallback(IntPtr.Zero, OnFrameDataReceived);
            }

            // Create a new source.
            var source = new NeuronSource(address, port, socketType, socket);

            lock (_sourcesLock) _sources.Add(source);

            return(source);
        }
コード例 #16
0
        public void SetBoundActor(NeuronActor actor)
        {
            if (boundActor != null)
            {
                UnregisterCallbacks();
            }

            if (actor != null)
            {
                boundActor = actor;
                RegisterCallbacks();
                actorID = actor.actorID;

                NeuronSource source = actor.owner;
                address           = source.address;
                port              = source.port;
                commandServerPort = source.commandServerPort;
                socketType        = source.socketType;

                standalone = false;
            }
        }
	static NeuronSource CreateConnection( string address, int port, int commandServerPort, NeuronConnection.SocketType socketType )
	{	
		NeuronSource source = null;
		IntPtr socketReference = IntPtr.Zero;
		IntPtr commandSocketReference = IntPtr.Zero;
		
		if( socketType == NeuronConnection.SocketType.TCP )
		{
			socketReference = NeuronDataReader.BRConnectTo( address, port );
			if( socketReference != IntPtr.Zero )
			{
				if (bDebugLog) Debug.Log( string.Format( "[NeuronConnection] Connected to {0}:{1}.", address, port ) );
			}
			else
			{
				if (bDebugLog) Debug.LogError( string.Format( "[NeuronConnection] Connecting to {0}:{1} failed.", address, port ) );
			}
		}
		else
		{
			socketReference = NeuronDataReader.BRStartUDPServiceAt( port );
			if( socketReference != IntPtr.Zero )
			{
				if (bDebugLog) Debug.Log( string.Format( "[NeuronConnection] Start listening at {0}.", port ) );
			}
			else
			{
				if (bDebugLog) Debug.LogError( string.Format( "[NeuronConnection] Start listening at {0} failed.", port ) );
			}
		}
		
		if( socketReference != IntPtr.Zero )
		{
			if( commandServerPort > 0 )
			{
				// connect to command server
				commandSocketReference = NeuronDataReader.BRConnectTo( address, commandServerPort );
				if( commandSocketReference != IntPtr.Zero )
				{
					if (bDebugLog) Debug.Log( string.Format( "[NeuronConnection] Connected to command server {0}:{1}.", address, commandServerPort ) );
				}
				else
				{
					if (bDebugLog) Debug.LogError( string.Format( "[NeuronConnection] Connected to command server {0}:{1} failed.", address, commandServerPort ) );
				}
			}

			source = new NeuronSource( address, port, commandServerPort, socketType, socketReference, commandSocketReference );
		}
		
		return source;
	}
コード例 #18
0
	public void OnDisconnect( NeuronSource source )
	{		
		NeuronActor[] activeActors = source.GetActiveActors();
		
		for( int i = 0; i < activeActors.Length; ++i )
		{
			NeuronActor actor = activeActors[i];
			NeuronAnimatorInstance instance = null;
			instances.TryGetValue( actor, out instance );
			if( instance != null )
			{
				instancesToDestroy.Add( instance );
			}
		}
	}
コード例 #19
0
        static NeuronSource CreateConnection(string address, int port, int commandServerPort, SocketType socketType)
        {
            NeuronSource source                 = null;
            IntPtr       socketReference        = IntPtr.Zero;
            IntPtr       commandSocketReference = IntPtr.Zero;

            if (socketType == SocketType.TCP)
            {
                socketReference = NeuronDataReader.BRConnectTo(address, port);
                if (socketReference != IntPtr.Zero)
                {
                    Debug.Log(string.Format("[NeuronConnection] Connected to {0}:{1}.", address, port));
                }
                else
                {
                    Debug.LogError(string.Format("[NeuronConnection] Connecting to {0}:{1} failed.", address, port));
                }
            }
            else
            {
                socketReference = NeuronDataReader.BRStartUDPServiceAt(port);
                if (socketReference != IntPtr.Zero)
                {
                    Debug.Log(string.Format("[NeuronConnection] Start listening at {0}.", port));
                }
                else
                {
                    Debug.LogError(string.Format("[NeuronConnection] Start listening at {0} failed.", port));
                }
            }

            if (socketReference != IntPtr.Zero)
            {
                if (connections.Count == 0)
                {
                    RegisterReaderCallbacks();
                }

                if (commandServerPort > 0)
                {
                    // connect to command server
                    commandSocketReference = NeuronDataReader.BRConnectTo(address, commandServerPort);
                    if (commandSocketReference != IntPtr.Zero)
                    {
                        Debug.Log(string.Format("[NeuronConnection] Connected to command server {0}:{1}.", address,
                                                commandServerPort));
                    }
                    else
                    {
                        Debug.LogError(string.Format("[NeuronConnection] Connected to command server {0}:{1} failed.",
                                                     address, commandServerPort));
                    }
                }

                source = new NeuronSource(address, port, commandServerPort, socketType, socketReference,
                                          commandSocketReference);
                connections.Add(source.guid, source);
                socketReferencesIndex.Add(socketReference, source);
                if (commandSocketReference != IntPtr.Zero)
                {
                    commandSocketReferenceIndex.Add(commandSocketReference, source);
                }
            }

            return(source);
        }
コード例 #20
0
		public NeuronActor( NeuronSource owner, int actorID )
		{
			this.owner = owner;
			this.actorID = actorID;
			
			if( owner != null )
			{
				owner.RegisterResumeActorCallback( OnResumeFrameData );
				owner.RegisterSuspendActorCallback( OnNoFrameData );
			}
		}
コード例 #21
0
 public static NeuronSource[] GetSources()
 {
     NeuronSource[] sources = new NeuronSource[connections.Count];
     connections.Values.CopyTo(sources, 0);
     return(sources);
 }