示例#1
0
        static void DumpThrift(string url, CookieCollection cookies)
        {
            List <webcrap.web.Cookie> list = new List <webcrap.web.Cookie>();

            foreach (System.Net.Cookie cookie in cookies)
            {
                webcrap.web.Cookie c = new webcrap.web.Cookie
                {
                    Name       = cookie.Name,
                    Value      = cookie.Value,
                    Version    = cookie.Version,
                    Path       = cookie.Path,
                    Domain     = cookie.Domain,
                    Port       = cookie.Port,
                    Comment    = cookie.Comment,
                    CommentUri = cookie.CommentUri?.AbsolutePath,
                    Expired    = cookie.Expired,
                    Expires    = cookie.Expires.ToUniversalTime().Ticks,
                    HttpOnly   = cookie.HttpOnly,
                    Secure     = cookie.Secure,
                    Timestamp  = cookie.TimeStamp.ToUniversalTime().Ticks
                };
                list.Add(c);
            }
            var dict = new Dictionary <string, List <webcrap.web.Cookie> >();

            dict.Add(url, list);

            Cookies cc = new Cookies
            {
                Cookies_ = dict
            };

            TByteBuffer trans = new TByteBuffer(102400);
            TProtocol   prot  = new TSimpleJSONProtocol(trans);

            cc.Write(prot);
            string json = Encoding.UTF8.GetString(trans.GetBuffer(), 0, trans.Length);

            Console.WriteLine(json);
            Console.WriteLine();


            TMemoryBuffer trans1 = new TMemoryBuffer(Encoding.UTF8.GetBytes(json));
            TProtocol     prot1  = new TSimpleJSONProtocol(trans1);

            Cookies cs = new Cookies();

            cs.Read(prot1);

            TByteBuffer trans2 = new TByteBuffer(102400);
            TProtocol   prot2  = new TSimpleJSONProtocol(trans2);

            cs.Write(prot2);
            string json2 = Encoding.UTF8.GetString(trans2.GetBuffer(), 0, trans2.Length);

            Console.WriteLine(json2);
            Console.WriteLine();
        }
示例#2
0
        // manually reading commands when not using a reader thread
        public void ReadCommandsNonThreaded(int aTimeOut)
        {
            TCommands Command = TCommands.icEndSession;

            byte[]      FixedCommandPart = new byte[MagicBytes.Length + sizeof(Int32) + sizeof(Int32)]; // magic + command + payloadsize
            TByteBuffer Payload          = new TByteBuffer();

            byte[] PayloadCheck = new byte[sizeof(Int32)];
            FNetStream.ReadTimeout = aTimeOut;
            do
            {
                if (ReadCommand(ref Command, FixedCommandPart, Payload, PayloadCheck))
                {
                    HandleCommand(Command, Payload);
                }
            } while ((Command != TCommands.icEndSession) && Connected);
        }
示例#3
0
 protected int ReadBytesFromNetStream(TByteBuffer aBuffer)
 {
     try
     {
         int Count        = 0;
         int NumBytesRead = -1;
         while (aBuffer.WriteAvailable > 0 && NumBytesRead != 0)
         {
             NumBytesRead = FNetStream.Read(aBuffer.Buffer, aBuffer.WriteCursor, aBuffer.WriteAvailable);
             aBuffer.Written(NumBytesRead);
             Count += NumBytesRead;
         }
         return(Count);
     }
     catch (IOException)
     {
         return(0); // signal connection error
     }
 }
示例#4
0
        // manually reading commands when not using a reader thread
        public void ReadCommandsNonBlocking()
        {
            TCommands Command = TCommands.icEndSession;

            byte[]      FixedCommandPart = new byte[MagicBytes.Length + sizeof(Int32) + sizeof(Int32)]; // magic + command + payloadsize
            TByteBuffer Payload          = new TByteBuffer();

            byte[] PayloadCheck = new byte[sizeof(Int32)];
            if (FNetStream.DataAvailable)
            {
                do
                {
                    if (ReadCommand(ref Command, FixedCommandPart, Payload, PayloadCheck))
                    {
                        HandleCommand(Command, Payload);
                    }
                } while (Command != TCommands.icEndSession && Connected);
            }
        }
示例#5
0
        protected void ReadCommands()
        {
            // todo: more like Delphi code
            TCommands Command = TCommands.icEndSession;

            // define once
            byte[]      FixedCommandPart = new byte[MagicBytes.Length + sizeof(Int32) + sizeof(Int32)]; // magic + command + payloadsize
            TByteBuffer Payload          = new TByteBuffer();

            byte[] PayloadCheck = new byte[sizeof(Int32)];
            do
            {
                try
                {
                    try
                    {
                        if (ReadCommand(ref Command, FixedCommandPart, Payload, PayloadCheck))
                        {
                            HandleCommand(Command, Payload);
                        }
                        else
                        if (Connected)
                        {
                            Debug.Print("## Dropped command (" + (int)Command + ") in ReadCommands loop");
                        }
                    }
                    catch (ThreadAbortException)
                    {
                        Thread.ResetAbort();
                        Command = TCommands.icEndSession;
                    }
                }
                catch (Exception e)
                {
                    if (Connected)
                    {
                        Debug.Print("## Exception in ReadCommands loop: " + e.Message);
                    }
                }
            } while (Command != TCommands.icEndSession && Connected);
        }
示例#6
0
文件: thriftnet.cs 项目: xgray/dotnet
        public override void Setup()
        {
            simpleInput            = new Simple();
            simpleInput.Value      = simpleValue;
            simpleInput.ShortValue = 16;
            simpleInput.IntValue   = 17;
            simpleInput.LongValue  = 18;
            simpleOutput           = new Simple();

            complexInput             = new Complex();
            complexOutput            = new Complex();
            complexInput.SimpleValue = simpleInput;
            complexInput.ListValue   = new List <Simple> {
                simpleInput, simpleInput
            };

            if (full)
            {
                complexInput.StringArray = Enumerable
                                           .Range(0, this.simpleValue.Length)
                                           .Select(i => this.simpleValue.Substring(i))
                                           .ToArray();

                complexInput.SetValue = complexInput.ListValue.ToHashSet();
                complexInput.MapValue = new Dictionary <string, Simple>
                {
                    { "a", simpleInput },
                    { "b", simpleInput }
                };
                complexInput.ArrayValue = complexInput.ListValue.ToArray();
            }

            trans = new TByteBuffer(1024);
            //  prot = new TBinaryProtocol(trans);
            prot = new TCompactProtocol(trans);

            trans.Clear();
            Proto <Simple> .Write(prot, simpleInput);
        }
示例#7
0
        public override void Setup()
        {
            simpleInput            = new Simple();
            simpleInput.Value      = simpleValue;
            simpleInput.ShortValue = 16;
            simpleInput.IntValue   = 17;
            simpleInput.LongValue  = 18;
            simpleOutput           = new Simple();

            complexInput             = new Complex();
            complexOutput            = new Complex();
            complexInput.SimpleValue = simpleInput;
            complexInput.ListValue   = new List <Simple> {
                simpleInput, simpleInput
            };

            if (full)
            {
                complexInput.SetValue = new THashSet <Simple>();
                complexInput.SetValue.Add(simpleInput);
                complexInput.SetValue.Add(simpleInput);

                complexInput.MapValue = new Dictionary <string, Simple>
                {
                    { "a", simpleInput },
                    { "b", simpleInput }
                };
            }

            trans = new TByteBuffer(1024);
            //  prot = new TBinaryProtocol(trans);
            prot = new TCompactProtocol(trans);

            trans.Clear();
            simpleInput.Write(prot);
        }
示例#8
0
        // Debug: public Int64 ReadCommandsCount = 0;

        // function returns payload of command, fills found command and returns problems during read in aResult
        // commandmagic + command + payloadsize [ + payload + payloadmagic]
        protected bool ReadCommand(ref TCommands aCommand, byte[] aFixedCommandPart, TByteBuffer aPayload, byte[] aPayloadCheck)
        {
            // Debug: ReadCommandsCount++;
            int NumBytesRead = FNetStream.Read(aFixedCommandPart, 0, aFixedCommandPart.Length);

            if (NumBytesRead > 0)
            {
                while (NumBytesRead < aFixedCommandPart.Length)
                {
                    NumBytesRead += FNetStream.Read(aFixedCommandPart, NumBytesRead, aFixedCommandPart.Length - NumBytesRead);
                }
                while (BitConverter.ToInt64(aFixedCommandPart, 0) != MagicBytesInt64)
                {
                    Array.Copy(aFixedCommandPart, 1, aFixedCommandPart, 0, aFixedCommandPart.Length - 1);
                    int rbr = FNetStream.ReadByte();
                    if (rbr != -1)
                    {
                        aFixedCommandPart[aFixedCommandPart.Length - 1] = (byte)rbr; // skipped bytes because of invalid magic in read command
                    }
                    else
                    {
                        return(false); // error, no valid connection
                    }
                }
                // we found the magic in the stream
                aCommand = (TCommands)BitConverter.ToInt32(aFixedCommandPart, MagicBytes.Length);
                Int32 PayloadSize = BitConverter.ToInt32(aFixedCommandPart, MagicBytes.Length + sizeof(Int32));
                if (PayloadSize <= MaxPayloadSize)
                {
                    aPayload.Clear(PayloadSize);
                    if (PayloadSize > 0)
                    {
                        int Len = ReadBytesFromNetStream(aPayload);
                        if (Len == aPayload.Length)
                        {
                            NumBytesRead = 0;
                            while (NumBytesRead < aPayloadCheck.Length)
                            {
                                NumBytesRead += FNetStream.Read(aPayloadCheck, NumBytesRead, aPayloadCheck.Length - NumBytesRead);
                            }
                            return((NumBytesRead == aPayloadCheck.Length) && (BitConverter.ToInt32(aPayloadCheck, 0) == CheckStringMagic));
                        }
                        else
                        {
                            return(false); // error, payload size mismatch
                        }
                    }
                    else
                    {
                        return(true); // ok, no payload
                    }
                }
                else
                {
                    return(false);  // error, payload is over max size
                }
            }
            else
            {
                return(false); //  error, no valid connection
            }
        }
示例#9
0
        public static string LocateServerURI(AddressFamily aAddressFamily = AddressFamily.InterNetwork, int aPort = 4000, int aTimeout = 1000)
        {
            string server = "";
            Socket socket = new Socket(aAddressFamily, SocketType.Dgram, ProtocolType.Udp);

            try
            {
                socket.ReceiveTimeout = aTimeout; // ms
                EndPoint receiveEP     = null;
                byte[]   receiveBuffer = new byte[MaxUDPCommandBufferSize];
                int      receivedBytes = 0;
                // build buffer with locator command
                TByteBuffer buffer = new TByteBuffer();
                buffer.Prepare(TConnection.MagicBytes);
                buffer.Prepare((Int32)TConnection.TCommands.icHUBLocate);
                buffer.Prepare((Int32)0);
                buffer.PrepareApply();
                buffer.QWrite(TConnection.MagicBytes);
                buffer.QWrite((Int32)TConnection.TCommands.icHUBLocate);
                buffer.QWrite((Int32)0);
                // do ipv4 and ipv6 stuff
                switch (aAddressFamily)
                {
                case AddressFamily.InterNetworkV6:
                    // bind to socket to enable receiving response
                    socket.Bind(new IPEndPoint(IPAddress.IPv6Any, 0));
                    socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.MulticastInterface, 0);
                    socket.SendTo(buffer.Buffer, new IPEndPoint(IPAddress.Parse("FF02::1"), aPort));
                    receiveEP = new IPEndPoint(IPAddress.IPv6Any, 0);
                    break;

                default:     // ipv4
                    // bind to socket to enable receiving response
                    socket.Bind(new IPEndPoint(IPAddress.Any, 0));
                    socket.EnableBroadcast = true;
                    socket.SendTo(buffer.Buffer, new IPEndPoint(IPAddress.Broadcast, aPort));
                    receiveEP = new IPEndPoint(IPAddress.Any, 0);
                    break;
                }
                try
                {
                    receivedBytes = socket.ReceiveFrom(receiveBuffer, ref receiveEP);
                    if (receivedBytes > 0)
                    {
                        // decode locator message
                        TByteBuffer receivedBuffer = new TByteBuffer(receiveBuffer, receivedBytes);
                        receivedBuffer.SkipReading(TConnection.MagicBytes.Length);
                        Int32 command;
                        receivedBuffer.Read(out command);
                        if (command == (Int32)TConnection.TCommands.icHUBFound)
                        {
                            string payload;
                            receivedBuffer.Read(out payload);
                            if (payload != "")
                            {
                                Int32 checkSum;
                                receivedBuffer.Read(out checkSum);
                                if (checkSum == TConnection.CheckStringMagic)
                                {
                                    server = payload;
                                }
                            }
                        }
                    }
                }
                catch
                {
                }
            }
            finally
            {
                socket.Close();
            }
            return(server);
        }