Example #1
0
        public static bool Read(out Identity user, byte[] data, out ulong length, int channel, Dictionary <int, List <QueuedData> > queue)
        {
            user   = null;
            length = 0;
            if (!queue.ContainsKey(channel) || queue[channel].Count == 0)
            {
                return(false);
            }

            QueuedData queuedData = queue[channel].ElementAt(0);

            for (int i = 0; i < data.Length; i++)
            {
                if (queuedData.Data.Count == 0)
                {
                    break;
                }
                data[i] = queuedData.Data.ElementAt(0);
                queuedData.Data.RemoveAt(0);
                length++;
            }

            user = queuedData.Ident;

            if (queuedData.Data.Count == 0)
            {
                queue[channel].Remove(queuedData);
            }

            return(true);
        }
Example #2
0
		protected Task SendBytesAsync (byte[] bytes)
		{
			var data = new QueuedData (bytes);
			lock (_syncRoot)
			{
				_sendQueue.Enqueue (data);
				SendNextBuffer ();
			}
			return data.Tcs.Task;
		}
Example #3
0
        protected Task SendBytesAsync(byte[] bytes)
        {
            var data = new QueuedData(bytes);

            lock (_syncRoot)
            {
                _sendQueue.Enqueue(data);
                SendNextBuffer();
            }
            return(data.Tcs.Task);
        }
Example #4
0
        private static void FlushCallback(bool transactional)
        {
            int total = ExportQueue.Values.Sum(d => d.Count);

            if (!_Updating || ExportQueue.Count == 0 || total == 0)
            {
                if (transactional)
                {
                    Connection.EndTransaction();
                    _Sync.Set();
                }

                OnAbort();
                return;
            }

            CMOptions.ToConsole("{0:#,0} objects in {1:#,0} tables pending update...", total, ExportQueue.Count);

            int updated = 0;

            ConcurrentStack <QueuedData> stack;

            QueuedData[] pop;
            QueuedData   peek;

            MySQLData[][] batch;
            int           count;

            var watch = new Stopwatch();

            watch.Start();

            foreach (string table in ExportQueue.Keys.TakeWhile(table => _Updating && updated < CMOptions.ExportCapacity))
            {
                stack = ExportQueue[table];

                if (stack == null || stack.IsEmpty)
                {
                    ExportQueue.TryRemove(table, out stack);
                    continue;
                }

                count = Math.Max(0, Math.Min(CMOptions.ExportCapacity - updated, stack.Count));

                if (count == 0)
                {
                    continue;
                }

                pop = new QueuedData[count];

                if (stack.TryPopRange(pop) > 0)
                {
                    peek = pop.FirstOrDefault(p => p != null && p.RawData != null && p.RawData.Count > 0);

                    if (peek == null || !Connection.CreateTable(table, peek.RawData))
                    {
                        ExportQueue.TryRemove(table, out stack);
                        continue;
                    }

                    batch = pop.AsParallel().Select(qd => qd.SqlData).ToMultiArray();

                    if (batch.Length > 0)
                    {
                        if (CMOptions.ModuleDebug)
                        {
                            CMOptions.ToConsole("Update {0:#,0} objects in '{1}'...", batch.Length, table);
                        }

                        Connection.InsertMany(table, batch);

                        updated += batch.Length;
                    }
                }

                if (stack.IsEmpty)
                {
                    ExportQueue.TryRemove(table, out stack);
                }
            }

            if (transactional)
            {
                VitaNexCore.TryCatch(Connection.EndTransaction, CMOptions.ToConsole);
            }

            watch.Stop();

            CMOptions.ToConsole("Updated {0:#,0} objects in {1:F2} seconds.", updated, watch.Elapsed.TotalSeconds);

            _UpdateCount += updated;

            //GC.Collect();

            _Sync.Set();

            if (!ExportQueue.IsEmpty)
            {
                Flush();
            }
        }
Example #5
0
        public static void Listen(NetPeer host, Connection connection, Dictionary <int, List <QueuedData> > queue, Dictionary <ulong, NetConnection> peers, out List <NetIncomingMessage> skippedMsgs)
        {
            skippedMsgs = new List <NetIncomingMessage>();
            NetIncomingMessage msg;

            while ((msg = host.ReadMessage()) != null)
            {
                switch (msg.MessageType)
                {
                case NetIncomingMessageType.StatusChanged:
                    NetConnectionStatus status = (NetConnectionStatus)msg.PeekByte();
                    LogUtils.LogNetwork("Status: " + status);
                    if (status == NetConnectionStatus.Disconnected)
                    {
                        var sConIdent = GetIdentFromConnection(msg.SenderConnection, peers);
                        if (Connection.CurrentConnection is ServerConnection)
                        {
                            ((ServerConnection)Connection.CurrentConnection).DisconnectClient(sConIdent, false);
                        }
                        else
                        {
                            Connection.CurrentConnection.Disconnect("Connection lost");
                        }
                        host.Recycle(msg);
                        continue;
                    }
                    break;

                case NetIncomingMessageType.VerboseDebugMessage:
                case NetIncomingMessageType.DebugMessage:
                    LogUtils.Debug(msg.ReadString());
                    continue;

                case NetIncomingMessageType.WarningMessage:
                    LogUtils.LogWarning(msg.ReadString());
                    continue;

                case NetIncomingMessageType.ErrorMessage:
                    LogUtils.LogError(msg.ReadString());
                    continue;
                }


                if (msg.MessageType != NetIncomingMessageType.Data)
                {
                    skippedMsgs.Add(msg);
                    continue;
                }

                byte[] chData = new byte[sizeof(int)];
                for (int i = 0; i < chData.Length; i++)
                {
                    chData[i] = msg.ReadByte();
                }

                var channel = BitConverter.ToInt32(chData, 0);

                LogUtils.LogNetwork("NetworkEvent: " + msg.MessageType + ": channel: " + channel);

                if (!queue.ContainsKey(channel))
                {
                    queue.Add(channel, new List <QueuedData>());
                }

                var ident = GetIdentFromConnection(msg.SenderConnection, peers);

                QueuedData qData = new QueuedData
                {
                    Ident = ident,
                    Data  = msg.ReadBytes(msg.LengthBytes - chData.Length).ToList()
                };

                queue[channel].Add(qData);
                host.Recycle(msg);
            }
        }