Exemplo n.º 1
0
        void Update()
        {
            while (server.Server.IsBound && server.Pending())
            {
                SocketStateObject newConn = new SocketStateObject();
                newConn.socket = server.AcceptSocket();
                connections.Add(newConn);
            }

            List <SocketStateObject> cleanup = new List <SocketStateObject>();

            byte[] sendData;
            lock (this)
            {
                sendData  = Encoding.UTF8.GetBytes(sendCache.ToString());
                sendCache = new StringBuilder();
            }

            LuaRuntime.GlobalEnvironment = luaEnv;
            foreach (SocketStateObject client in connections)
            {
                if (!client.socket.Connected)
                {
                    cleanup.Add(client);
                    continue;
                }

                if (client.socket.Available > 0)
                {
                    int br = client.socket.Receive(client.buffer, SocketStateObject.BufferSize, SocketFlags.Partial);
                    client.sb.Append(Encoding.UTF8.GetString(client.buffer, 0, br));
                    string tmp = client.sb.ToString();
                    if (tmp.Contains("\n"))
                    {
                        string[] pcs = tmp.Replace("\r", "").Split('\n');
                        foreach (string line in pcs.Take(pcs.Length - 1))
                        {
                            try
                            {
                                LuaRuntime.Run(line, luaEnv);
                            }
                            catch (Exception e)
                            {
                                A8Console.WriteLine(e.GetType().Name + ": " + e.Message);
                                luaEnv.SetNameValue("lastError", ObjectToLua.ToLuaValue(e));
                            }
                        }
                        client.sb = new StringBuilder(pcs.Last());
                    }
                }

                if (sendData.Length > 0)
                {
                    client.socket.Send(sendData);
                }
            }

            connections.RemoveAll(i => cleanup.Contains(i));
        }
Exemplo n.º 2
0
        void Update()
        {
            while (server.Pending())
            {
                SocketStateObject newConn = new SocketStateObject();
                newConn.socket = server.AcceptSocket();
                connections.Add(newConn);
            }

            List<SocketStateObject> cleanup = new List<SocketStateObject>();

            byte[] sendData;
            lock (this)
            {
                sendData = Encoding.UTF8.GetBytes(sendCache.ToString());
                sendCache = new StringBuilder();
            }

            LuaRuntime.GlobalEnvironment = luaEnv;
            foreach (SocketStateObject client in connections)
            {
                if (!client.socket.Connected)
                {
                    cleanup.Add(client);
                    continue;
                }

                if (client.socket.Available > 0)
                {
                    int br = client.socket.Receive(client.buffer, SocketStateObject.BufferSize, SocketFlags.Partial);
                    client.sb.Append(Encoding.UTF8.GetString(client.buffer, 0, br));
                    string tmp = client.sb.ToString();
                    if (tmp.Contains("\n"))
                    {
                        string[] pcs = tmp.Replace("\r", "").Split(char.Parse("\n"));
                        foreach (string line in pcs.Take(pcs.Length - 1))
                        {
                            try
                            {
                                LuaRuntime.Run(line, luaEnv);
                            }
                            catch (Exception e)
                            {
                                A8Console.WriteLine(e.GetType().Name + ": " + e.Message);
                                luaEnv.SetNameValue("lastError", ObjectToLua.ToLuaValue(e));
                            }
                        }
                        client.sb = new StringBuilder(pcs.Last());
                    }
                }

                if (sendData.Length > 0)
                {
                    client.socket.Send(sendData);
                }
            }

            connections.RemoveAll(i => cleanup.Contains(i));
        }