A server instance is a connected client on the server side
예제 #1
0
        public void NetworkLog(ServerInstance instance, string message)
        {
            if(!_clients.ContainsKey (instance)){
                Debug.Log (_debugChannel, "Client " + instance.IP + " has no channel");
                return;
            }

            Debug.Log (_clients[instance], message);
        }
예제 #2
0
        public void NetworkRegisterLogger(ServerInstance instance, string name)
        {
            if(_clients.ContainsKey (instance)){
                Debug.Log (_debugChannel, "Client " + instance.IP + " already owns a channel");
                return;
            }

            _clients.Add (instance, Debug.AddChannel ("Network | " + name));
        }
예제 #3
0
        public void NetworkUnregisterLogger(ServerInstance instance)
        {
            if(!_clients.ContainsKey (instance)){
                Debug.Log (_debugChannel, "Client " + instance.IP + " has no channel");
                return;
            }

            Debug.DeleteChannel (_clients[instance]);
            _clients.Remove (instance);
        }
예제 #4
0
        /// <summary>
        /// Registers a client
        /// </summary>
        /// <param name="instance">The client</param>
        /// <param name="function">The clients function to call</param>
        public static void RegisterClient(ServerInstance instance, string function)
        {
            _clientSemaphore.Enqueue();

            Debug.Log(_debugChannel, "Registering client " + instance.IP);

            if (_clients.ContainsKey(instance))
                _clients[instance] = function;
            else
                _clients.Add(instance, function);

            _clientSemaphore.Dequeue();
        }
예제 #5
0
        /// <summary>
        /// Unregisters a client
        /// </summary>
        /// <param name="instance">The client</param>
        public static void Unregister(ServerInstance instance)
        {
            if (!_clients.ContainsKey(instance))
                return;

            _semaphore.Enqueue();

            _clients.Remove(instance);

            Debug.Log(_debugChannel, "Unregistered client " + instance.IP);

            _semaphore.Dequeue();
        }
예제 #6
0
        /// <summary>
        /// Registers a client
        /// </summary>
        /// <param name="instance">The client</param>
        /// <param name="function">The clients function to call</param>
        public static void RegisterClient(ServerInstance instance, string startedF, string stoppedF, string updatedF, string calledF)
        {
            _clientSemaphore.Enqueue();

            Debug.Log(_debugChannel, "Registering client " + instance.IP);

            if (_clients.ContainsKey (instance))
                _clients.Remove (instance);

            _clients.Add(instance, new Dictionary<Function, string>());
            _clients [instance].Add (Function.STARTED, startedF);
            _clients [instance].Add (Function.STOPPED, stoppedF);
            _clients [instance].Add (Function.UPDATED, updatedF);
            _clients [instance].Add (Function.CALLED, calledF);

            _clientSemaphore.Dequeue();
        }
예제 #7
0
 private static void ServerInstance_InstanceClosed(ServerInstance obj)
 {
     UnregisterClient(obj);
 }
예제 #8
0
        private static void Server_MessageReceived(ServerInstance arg1, byte[] arg2)
        {
            Debug.Log(_debugChannel, "Client '" + arg1.IP + "' sent " + arg2.Length + " bytes");

            //Check for multiple messages
            String message = Encoding.UTF8.GetString(arg2);

            int xmlCount = Regex.Matches(message, Regex.Escape("<?xml version=")).Count;

            if(xmlCount == 1) {
                HandleMessage(arg1, message);
                return;
            }

            List<int> indices = new List<int>();
            foreach (int index in message.IndexesOf("<?xml version="))
                indices.Add(index);

            for(int i = 0; i < indices.Count; i++) {
                string substring = null;

                if (i == indices.Count - 1) {
                    substring = message.Substring(indices[i]);
                } else {
                    substring = message.Substring(indices[i], indices[i + 1] - indices[i]);
                }

                HandleMessage(arg1, substring);
            }
        }
예제 #9
0
 private static void Server_ClientConnected(ServerInstance obj)
 {
     Debug.Log(_debugChannel, "Client '" + obj.IP + "' connected");
 }
예제 #10
0
 public void NetworkRegisterToTimeService(ServerInstance instance, string functionName)
 {
     TimeService.Register(instance, functionName);
 }
예제 #11
0
 private void ServerInstance_MessageReceived(ServerInstance arg1, byte[] arg2)
 {
     MessageReceived?.Invoke(arg1, arg2);
 }
예제 #12
0
 private void ServerInstance_InstanceClosed(ServerInstance obj)
 {
     _clients.Remove(obj);
     ClientDisconnected?.Invoke(obj);
 }
예제 #13
0
 /// <summary>
 /// Closes the connection to one client
 /// </summary>
 /// <param name="instance">The connection to close</param>
 public void Close(ServerInstance instance)
 {
     instance.Close();
 }
예제 #14
0
        private void Work()
        {
            try {
                _listener.Start();

                while (_running) {
                    while (!_listener.Pending()) {
                        if (!_running)
                            break;
                        Thread.Sleep(_sleepTime);
                    }

                    if (!_running)
                        break;

                    Socket newSocket = _listener.AcceptSocket();

                    if (newSocket == null)
                        continue;

                    ServerInstance newConnection = new ServerInstance(newSocket);
                    _clients.Add(newConnection);

                    ClientConnected?.Invoke(newConnection);
                }

                foreach (ServerInstance instance in _clients)
                    instance.Close();

                _listener.Stop();
                _thread.Abort();
            } catch (Exception e) {

            }
        }
예제 #15
0
 public void NetworkUnregisterFromTimeService(ServerInstance instance)
 {
     TimeService.Unregister(instance);
 }
예제 #16
0
 void ServerInstance_InstanceClosed(ServerInstance obj)
 {
     if(_clients.ContainsKey (obj))
         NetworkUnregisterLogger (obj);
 }
예제 #17
0
        private static void HandleMessage(ServerInstance arg1, string message)
        {
            IXPFile ixp = new IXPFile(message);

            FunctionInfo nfunc = _networkFunctions.FirstOrDefault(nf => nf.Name.Equals(ixp.NetworkFunction));

            if (nfunc != null)
                Debug.Log(_debugChannel, "Function: " + ixp.NetworkFunction);
            else {
                Debug.Log(_debugChannel, "Function: Unknown");
                return;
            }

            Dictionary<string, object> dicParams = new Dictionary<string, object>();
            foreach (string param in nfunc.Parameters) {
                if (ixp.GetInfoValue(param) == null && nfunc.GetParameterType(param) != typeof(ServerInstance)) {
                    Debug.Log(_debugChannel, "Missing parameter: " + param);
                    return;
                }

                object paramVal = null;
                Type t = nfunc.GetParameterType(param);
                if (t == typeof(ServerInstance)) {
                    paramVal = arg1;
                } else {
                    string strVal = ixp.GetInfoValue(param);

                    if (t == typeof(string)) {
                        paramVal = strVal;
                    } else if (t == typeof(int)) {
                        int i = -1;
                        bool s = int.TryParse(strVal, out i);
                        if (s)
                            paramVal = i;
                    } else if(t == typeof(byte)) {
                        byte b = 0;
                        bool s = byte.TryParse(strVal, out b);
                        if (s)
                            paramVal = b;
                    } else if(t == typeof(ushort)){
                        ushort us = 0;
                        bool s = ushort.TryParse(strVal, out us);
                        if (s)
                            paramVal = us;
                    } else if (t == typeof(bool)) {
                        if (strVal.Equals("TRUE"))
                            paramVal = true;
                        else if (strVal.Equals("FALSE"))
                            paramVal = false;
                    }

                    if (paramVal == null) {
                        Debug.Log(_debugChannel, "Invalid type for param " + param + ". Expected " + t.ToString() + ". Got Value: " + strVal);
                        return;
                    }
                }

                dicParams.Add(param, paramVal);
            }

            if (nfunc.ReturnType == typeof(void))
                nfunc.Invoke(dicParams);
            else if (nfunc.ReturnType == typeof(IXPFile)) {
                IXPFile file = (IXPFile)nfunc.Invoke(dicParams);
                arg1.Send(Encoding.UTF8.GetBytes(file.XML));
            } else {
                object response = nfunc.Invoke(dicParams);
                IXPFile iresponse = new IXPFile();
                iresponse.NetworkFunction = ixp.NetworkFunction;
                iresponse.PutInfo("Response", response.ToString());
                arg1.Send(Encoding.UTF8.GetBytes(iresponse.XML));
            }
        }