Ejemplo n.º 1
0
        private void ThreadMain()
        {
            UInt32         channel = 0, packetSize = 0;
            ChannelHandler handler = null;

            bool readingHeader = true;

            try
            {
                byte[] buffer = new byte[tcpClient.ReceiveBufferSize],
                decodeBuffer = new byte[tcpClient.ReceiveBufferSize];
                DataQueue queue = new DataQueue(tcpClient.ReceiveBufferSize);

                while (tcpClient != null)
                {
                    int read = ioStream.Read(buffer, 0, tcpClient.ReceiveBufferSize);
                    if (read <= 0)
                    {
                        break;
                    }
                    queue.Append(buffer, read);

                    do
                    {
                        if (readingHeader)
                        {
                            if (queue.Length >= 8)
                            {
                                channel       = queue.PopUInt32();
                                packetSize    = queue.PopUInt32();
                                readingHeader = false;
                                if (packetSize > SafePacketSize)
                                {
                                    throw new Exception("Received unsafe packet size (" + packetSize + ") on channel " + channel);
                                }
                                handler = ChannelMap.LookUp(channel);
                                if (handler == null)
                                {
                                    throw new Exception("Received packet on unexpected channel " + channel);
                                }
                                if (handler.LoggedInOnly && !IsAuthenticated)
                                {
                                    throw new Exception("Received packet on channel " + channel + ", which is reserved for authenticated clients");
                                }
                            }
                        }

                        if (!readingHeader)
                        {
                            if (queue.Length >= packetSize)
                            {
                                if (decodeBuffer.Length < packetSize)
                                {
                                    decodeBuffer = new byte[packetSize];
                                }
                                queue.PopData(decodeBuffer, (int)packetSize);
                                handler.Handle(this, decodeBuffer, (int)packetSize);
                                readingHeader = true;
                                handler       = null;
                            }
                        }
                    }while (readingHeader && queue.Length >= 8);
                }
            }
            catch (ObjectDisposedException)     //socket has been closed
            { }
            catch (SocketException)             //probably safe, tpp
            { }
            catch (BlockException exception)
            {
                OnAbnormalException(exception);
            }
            catch (IOException)             //this is fired if the ssl connection is terminated
            { }
            catch (Exception ex)            //should not happen
            {
                OnAbnormalException(ex);
            }
            Close();
            OnReadThreadExit();
        }
Ejemplo n.º 2
0
        public static void Test()
        {
            DataQueue queue = new DataQueue(0);

            byte[] field = new byte[256];
            for (int i = 0; i < 256; i++)
                field[i] = (byte)i;

            byte[] exported = new byte[256];


            for (int i = 1; i < 257; i++)
            {
                Console.Out.Write(i);
                queue.Append(field, i);

                if (i > 1)
                {
                    queue.PopData(exported, i - 1);
                    for (int j = 0; j < i - 1; j++)
                    {
                        Debug.Assert(exported[j] == (byte)j);
                        Console.Out.Write(" ");
                        Console.Out.Write(exported[j]);
                    }
                }
                Console.Out.WriteLine();
            }

            Console.Out.WriteLine(queue.Length);
            Debug.Assert(queue.Length == 256);
        }
Ejemplo n.º 3
0
		private void ThreadMain()
		{
			UInt32 channel = 0, packetSize = 0;
			ChannelHandler handler = null;

			bool readingHeader = true;

			try
			{
				byte[] buffer = new byte[tcpClient.ReceiveBufferSize],
						decodeBuffer = new byte[tcpClient.ReceiveBufferSize];
				DataQueue queue = new DataQueue(tcpClient.ReceiveBufferSize);

				while (tcpClient != null)
				{
					int read = ioStream.Read(buffer, 0, tcpClient.ReceiveBufferSize);
					if (read <= 0)
						break;
					queue.Append(buffer, read);

					do
					{
						if (readingHeader)
						{
							if (queue.Length >= 8)
							{
								channel = queue.PopUInt32();
								packetSize = queue.PopUInt32();
								readingHeader = false;
								if (packetSize > SafePacketSize)
								{
									throw new Exception("Received unsafe packet size (" + packetSize + ") on channel " + channel);
								}
								handler = ChannelMap.LookUp(channel);
								if (handler == null)
								{
									throw new Exception("Received packet on unexpected channel " + channel);
								}
								if (handler.LoggedInOnly && !IsAuthenticated)
								{
									throw new Exception("Received packet on channel " + channel + ", which is reserved for authenticated clients");
								}
							}
						}

						if (!readingHeader)
							if (queue.Length >= packetSize)
							{
								if (decodeBuffer.Length < packetSize)
									decodeBuffer = new byte[packetSize];
								queue.PopData(decodeBuffer, (int)packetSize);
								handler.Handle(this, decodeBuffer, (int)packetSize);
								readingHeader = true;
								handler = null;
							}
					}
					while (readingHeader && queue.Length >= 8);
                }
			}
			catch (ObjectDisposedException) //socket has been closed
			{ }
			catch (SocketException) //probably safe, tpp
			{ }
			catch (BlockException exception)
			{
				OnAbnormalException(exception);
			}
			catch (IOException) //this is fired if the ssl connection is terminated
			{ }
			catch (Exception ex)    //should not happen
			{
				OnAbnormalException(ex);
			}
			Close();
			OnReadThreadExit();

		}