Example #1
0
        public Client(int sessionId, IrcServer server, Socket socket)
        {
            SessionId = sessionId;
            Server = server;
            _socket = socket;

            ClientInfo = new ClientInfo();

            _currentBuffer = new ByteQueue();
            _processedBuffer = new ByteQueue();
            FragPackets = new ByteQueue();

            _nextActivityCheck = DateTime.Now + TimeSpan.FromSeconds(30);
        }
Example #2
0
        internal void SendStart()
        {
            if (!Running || !_socket.Connected)
            {
                DisposeSendSystem();
                return;
            }

            Paket packet = null;
            try
            {
                var byteQueue = new ByteQueue();
                int length = 0;
                while (!PacketsToBeSent.IsEmpty && length <= 1024)
                {
                    if (!PacketsToBeSent.TryDequeue(out packet))
                    {
                        Interlocked.Exchange(ref _timesEnqueuedForSend, 0);
                        return;
                    }

                    packet.Write();

                    byte[] packetBuffer = packet.Stream.GetBuffer();
                    length += packetBuffer.Length;

                    byteQueue.Enqueue(packetBuffer, 0, packetBuffer.Length);

                }

                if (byteQueue.Length > 0)
                {
                    var data = new byte[length];
                    byteQueue.Dequeue(data, 0, data.Length);
                    SendAsync(data);
                }
                else
                {
                    Interlocked.Exchange(ref _timesEnqueuedForSend, 0);

                    if (!PacketsToBeSent.IsEmpty)
                    {
                        int newValue = Interlocked.Increment(ref _timesEnqueuedForSend);

                        if (newValue == 1)
                        {
                            IrcServer.SendClientQueue.Enqueue(this);
                            Server.NetworkSignal.Set();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                DisposeSendSystem();
                if (packet != null)
                    Logger.Log(Logger.LogLevel.Error, "Sending packet: {0}", packet.ToString());
                Logger.Log(Logger.LogLevel.Error, e.ToString());

                // TODO: log something?
            }
        }