Beispiel #1
0
        public void Send_Start(Packet packet = null)
        {
            if(!Running)
            {
                DisposeSendSystem();
                return;
            }

            try
            {
                byte[] data;
                if (packet == null)
                {
                    if (packetsToBeSent.Count > 0)
                    {
                        if (!packetsToBeSent.TryDequeue(out packet))
                        {
                            Interlocked.Exchange(ref _TimesEnqueuedForSend, 0);
                            return;
                        }

                        packet.Write();
                        data = packet.GetBuffer();

                        if (packet.Async)
                            Send_Async(data);
                        else
                        {
                            Send_Sync(data);
                            while (Running && packetsToBeSent.Count > 0)
                            {
                                if (!packetsToBeSent.TryDequeue(out packet))
                                {
                                    Interlocked.Exchange(ref _TimesEnqueuedForSend, 0);
                                    return;
                                }

                                if (packet.Async)
                                    break;

                                packet.Write();
                                data = packet.GetBuffer();

                                Send_Sync(data);
                                packet = null;
                            }

                            if (packet != null)
                                Send_Start(packet);
                            else
                                Interlocked.Exchange(ref _TimesEnqueuedForSend, 0);
                        }
                    }
                    else
                        Interlocked.Exchange(ref _TimesEnqueuedForSend, 0);
                }
                else
                {
                    packet.Write();
                    data = packet.GetBuffer();
                    Send_Async(data);
                }
            }
            catch (Exception e)
            {
                MarkToDispose();
                DisposeSendSystem();
                Logger.Log(Logger.LogLevel.Error, e.Message);
                // TODO: log something?
            }
        }
Beispiel #2
0
 public void Send_Sync_Packet(Packet packet)
 {
     packet.Write();
     Send_Sync(packet.GetBuffer());
     packet.Release();
 }