Пример #1
0
        public void Run()
        {
            try
            {
                ByteBuilder builder = new ByteBuilder(2048);

                //
                // Get first line
                //
                String line = socketLineReader.ReadLine();
                if (line == null)
                {
                    return;
                }

                if (line.StartsWith("GET "))
                {
                    uint start = BuildHttpResponseFromFirstLine(builder, line);
                    if (builder.contentLength > 0)
                    {
                        socket.Send(builder.bytes, (int)start, (int)(builder.contentLength - start), SocketFlags.None);
                    }

                    socket.ShutdownAndDispose();
                }
                else
                {
                    //
                    // Tcp Mode
                    //
                    do
                    {
                        if (!HandleLine(builder, line))
                        {
                            socket.ShutdownAndDispose();
                            break;
                        }
                        if (builder.contentLength > 0)
                        {
                            socket.Send(builder.bytes, (int)builder.contentLength, SocketFlags.None);
                            builder.Clear();
                        }

                        line = socketLineReader.ReadLine();
                    } while (line != null);

                    socket.ShutdownAndDispose();
                }
            }
            catch (Exception e)
            {
                callback.UnhandledException(clientString, e);
                socket.ShutdownAndDispose();
            }
        }
Пример #2
0
        void HandleNpcLines(ref SelectControl selectControl, Socket socket, Buf safeBuffer, ByteBuilder builder, String line)
        {
            do
            {
                if (!HandleLine(builder, line))
                {
                    selectControl.ShutdownDisposeAndRemoveReceiveSocket(socket);
                    break;
                }
                if (builder.bytes != safeBuffer.array)
                {
                    safeBuffer.array = builder.bytes; // Update the buffer with the bigger buffer
                }

                if (builder.contentLength > 0)
                {
                    socket.Send(builder.bytes, (int)builder.contentLength, SocketFlags.None);
                    builder.Clear();
                }

                line = lineParser.GetLine();
            } while (line != null);
        }
Пример #3
0
        public void TestMethod1()
        {
            ByteBuilder builder = new ByteBuilder(0);

            builder.AppendNumber(0, 10);
            Assert.AreEqual((Byte)'0', builder.bytes[0]);

            builder.Clear();
            builder.AppendNumber(1, 10);
            Assert.AreEqual((Byte)'1', builder.bytes[0]);

            builder.Clear();
            builder.AppendNumber(9, 10);
            Assert.AreEqual((Byte)'9', builder.bytes[0]);

            builder.Clear();
            builder.AppendNumber(10, 10);
            Assert.AreEqual((Byte)'1', builder.bytes[0]);
            Assert.AreEqual((Byte)'0', builder.bytes[1]);

            builder.Clear();
            builder.AppendNumber(48, 10);
            Assert.AreEqual((Byte)'4', builder.bytes[0]);
            Assert.AreEqual((Byte)'8', builder.bytes[1]);

            builder.Clear();
            builder.AppendNumber(975, 10);
            Assert.AreEqual((Byte)'9', builder.bytes[0]);
            Assert.AreEqual((Byte)'7', builder.bytes[1]);
            Assert.AreEqual((Byte)'5', builder.bytes[2]);

            builder.Clear();
            builder.AppendNumber(0xAF12, 16);
            Assert.AreEqual((Byte)'A', builder.bytes[0]);
            Assert.AreEqual((Byte)'F', builder.bytes[1]);
            Assert.AreEqual((Byte)'1', builder.bytes[2]);
            Assert.AreEqual((Byte)'2', builder.bytes[3]);
        }