当前位置: 首页>>代码示例>>C#>>正文


C# Frame.ToBinary方法代码示例

本文整理汇总了C#中Frame.ToBinary方法的典型用法代码示例。如果您正苦于以下问题:C# Frame.ToBinary方法的具体用法?C# Frame.ToBinary怎么用?C# Frame.ToBinary使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Frame的用法示例。


在下文中一共展示了Frame.ToBinary方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Transmit

    protected void Transmit(Frame token)
    {
        // Send until THT reached
        // first send the token ack
        Frame sendFrame;
        Frame recieveFrame;
        byte[] bFrame;
        String[] splitInput;
        int dataSize;
        this.THT = 1040;
        Frame newToken;

        // send ack for token
        sendFrame = new Frame(0, token.SA, this.nodeNum, null);
        sendFrame.FS = 2;
        bFrame = sendFrame.ToBinary();
        sendStream.Write(bFrame, 0, bFrame.Length);

        // read from file, and build frame from it
        while (true)
        {
            // tokenize the input line
            splitInput = this.inputFile[index].Split(new char[] { ',' });
            dataSize = Convert.ToByte(splitInput[1]); // the size of data is always in the second array element
            if (dataSize > 254)
            {
                Console.WriteLine("LOL BAD STUFF");
            }

            if (this.THT < dataSize)
            {
                // not enough THT to send the frame, send a token
                newToken = new Token(0, 0, this.nodeNum);
                bFrame = newToken.ToBinary();

                sendStream.Write(bFrame, 0, bFrame.Length);
                recieveFrame = Frame.MakeFrame(Receive());
                Debug.Assert(recieveFrame.DA == this.nodeNum);
                Debug.Assert(recieveFrame.FS == 2 || recieveFrame.FS == 3);

                return;
            }

            // otherwise, build and send the frame
            sendFrame = new Frame(0, Convert.ToByte(splitInput[0]), this.nodeNum, new System.Text.UTF8Encoding().GetBytes(splitInput[2]));
            bFrame = sendFrame.ToBinary();
            sendStream.Write(bFrame, 0, bFrame.Length);
            THT -= sendFrame.size;

            // recieve ack
            while (true)
            {
                recieveFrame = Frame.MakeFrame(Receive());
                Debug.Assert(recieveFrame.SA == this.nodeNum);
                Debug.Assert(recieveFrame.DA == sendFrame.DA);
                Debug.Assert(recieveFrame.FS == 2 || recieveFrame.FS == 3);

                // frame wasn't accepted, resend
                if (recieveFrame.FS == 3)
                {
                    // is there enough THT to resend?
                    if (this.THT > sendFrame.size)
                    {
                        // send it!
                        sendStream.Write(bFrame, 0, bFrame.Length);
                        THT -= sendFrame.size;
                        continue;
                    }
                    else
                    {
                        // send the token along
                        newToken = new Token(0, 0, this.nodeNum);
                        bFrame = newToken.ToBinary();

                        sendStream.Write(bFrame, 0, bFrame.Length);
                        recieveFrame = Frame.MakeFrame(Receive());
                        Debug.Assert(recieveFrame.DA == this.nodeNum);
                        Debug.Assert(recieveFrame.FS == 2 || recieveFrame.FS == 3);
                        return;
                    }
                }
                else
                {
                    // frame was successfully recieved, move onto the next one
                    this.index++;
                    if (this.index >= this.inputFile.Length)
                    {
                        // nothing more to send
                        newToken = new Token(0, 0, this.nodeNum);
                        bFrame = newToken.ToBinary();

                        sendStream.Write(bFrame, 0, bFrame.Length);
                        recieveFrame = Frame.MakeFrame(Receive());
                        Debug.Assert(recieveFrame.DA == this.nodeNum);
                        Debug.Assert(recieveFrame.FS == 2 || recieveFrame.FS == 3);
                        return;
                    }
                    break;
                }
            }
//.........这里部分代码省略.........
开发者ID:Kanazuchi,项目名称:Token-Ring-With-Bridge,代码行数:101,代码来源:TokenRing2.cs

示例2: Transmit

    protected void Transmit(Frame token)
    {
        // Send until THT reached
        // first send the token ack
        Frame sendFrame;
        Frame recieveFrame;
        byte[] bFrame;
        int dataSize;
        this.THT = 1040;
        Frame newToken;
        Queue<Frame> sendQueue;
        Object locker;

        // send ack for token
        sendFrame = new Frame(0, token.SA, this.nodeNum, null);
        sendFrame.FS = 2;
        bFrame = sendFrame.ToBinary();
        sendStream.Write(bFrame, 0, bFrame.Length);

        // setup which buffer to send from
        if (bridgeNumber == 1)
        {
            lock (buf1Locker)
            {
                sendQueue = buffer1;
                locker = buf1Locker;
            }
        }
        else
        {
            lock (buf2Locker)
            {
                sendQueue = buffer2;
                locker = buf1Locker;
            }
        }

        // read from buffer and send it
        while (true)
        {
            // get the next frame to send and its size
            lock (locker)
            {
                sendFrame = sendQueue.Peek();
            }
            dataSize = sendFrame.size;

            if (this.THT < dataSize)
            {
                // not enough THT to send the frame, send a token
                newToken = new Token(0, 0, this.nodeNum);
                bFrame = newToken.ToBinary();

                sendStream.Write(bFrame, 0, bFrame.Length);
                recieveFrame = Frame.MakeFrame(Receive());
                //Debug.Assert(recieveFrame.DA == this.nodeNum);
                Debug.Assert(recieveFrame.FS == 2 || recieveFrame.FS == 3);

                return;
            }

            // otherwise, send the frame
            bFrame = sendFrame.ToBinary();
            sendStream.Write(bFrame, 0, bFrame.Length);
            THT -= sendFrame.size;

            // recieve ack
            while (true)
            {
                recieveFrame = Frame.MakeFrame(Receive());
                //Debug.Assert(recieveFrame.SA == this.nodeNum);
                //Debug.Assert(recieveFrame.DA == sendFrame.DA);
                //Debug.Assert(recieveFrame.FS == 2 || recieveFrame.FS == 3);

                // if it needs to be setup into the routing table
                if (recieveFrame.AC == 4)
                {
                    // if the frame recieved doesn't have the FS changed, nothing processed it
                    if (recieveFrame.FS == 0)
                    {
                        lock (locker)
                        {
                            sendQueue.Dequeue();
                            if (sendQueue.Count == 0)
                            {
                                // nothing more to send
                                newToken = new Token(0, 0, this.nodeNum);
                                bFrame = newToken.ToBinary();

                                sendStream.Write(bFrame, 0, bFrame.Length);
                                recieveFrame = Frame.MakeFrame(Receive());
                                //Debug.Assert(recieveFrame.DA == this.nodeNum);
                                Debug.Assert(recieveFrame.FS == 2 || recieveFrame.FS == 3);
                                return;
                            }
                        }
                        break;
                    }
                    // otherwise, add it to the routing table
                    else
//.........这里部分代码省略.........
开发者ID:Kanazuchi,项目名称:Token-Ring-With-Bridge,代码行数:101,代码来源:Bridge.cs

示例3: Run

    // monitors run, since it will only accept a token
    public override void Run()
    {
        // connect with neighboring nodes
        this.Connect();

        // Accept incoming connection and wait for
        // right neighbor to be ready.

        // create the first token
        Frame sendFrame = new Token(0, 0, 0);
        byte[] bFrame = sendFrame.ToBinary();
        Frame recieveFrame;
        Frame newToken;
        Frame frame;

        // send it!
        sendStream.Write(bFrame, 0, bFrame.Length);
        recieveFrame = Frame.MakeFrame(Receive());
        Debug.Assert(recieveFrame.DA == this.nodeNum);
        Debug.Assert(recieveFrame.FS == 2 || recieveFrame.FS == 3);

        try
        {
            while (true)
            {
                // listen state
                // recieve frame, check if token

                frame = Frame.MakeFrame(Receive());

                if (frame is Token)
                {
                    // if it was sent by the monitor, everyone is done sending
                    if (frame.SA == this.nodeNum)
                    {
                        // send a frame with the finished bit enabled
                        sendFrame = new Frame(2, 255, 0, null);
                        bFrame = sendFrame.ToBinary();
                        sendStream.Write(bFrame, 0, bFrame.Length);
                    }
                    // if the token wasn't sent by the monitor, must send ack
                    else
                    {
                        sendFrame = new Frame(0, frame.SA, this.nodeNum, null);
                        sendFrame.FS = 2;
                        bFrame = sendFrame.ToBinary();
                        sendStream.Write(bFrame, 0, bFrame.Length);
                    }

                    // otherwise send a new token if the SA is not the monitor
                    newToken = new Token(0, this.nodeNum, this.nodeNum);

                    bFrame = newToken.ToBinary();
                    sendStream.Write(bFrame, 0, bFrame.Length);
                    frame = Frame.MakeFrame(Receive());

                    if (frame.SA == this.nodeNum)
                    {
                        // send a frame with the kill bit enabled
                        sendFrame = new Frame(2, lastNode, 0, null);
                        bFrame = sendFrame.ToBinary();
                        sendStream.Write(bFrame, 0, bFrame.Length);
                    }
                    else
                    {
                        Debug.Assert(frame.DA == this.nodeNum);
                        Debug.Assert(frame.FS == 2 || frame.FS == 3);
                    }
                }
                // otherwise, check if the bridge is telling it to shutdown
                else if (frame.AC == 1)
                {
                    Console.WriteLine("Exterminate list");
                    // send a frame with the kill bit enabled
                    sendFrame = new Frame(1, lastNode, 0, null);
                    bFrame = sendFrame.ToBinary();
                    sendStream.Write(bFrame, 0, bFrame.Length);
                    break;
                }
                else
                {
                    bFrame = frame.ToBinary();
                    sendStream.Write(bFrame, 0, bFrame.Length);
                }
            }
        }
        finally
        {
            // Close all open resources (i.e. sockets!)
            Console.WriteLine("Monitor node EX-TERM-INATED!");
            this.recieveStream.Close();
            this.recieveClient.Close();
        }
    }
开发者ID:Kanazuchi,项目名称:Token-Ring-With-Bridge,代码行数:95,代码来源:TokenRing2.cs

示例4: Run

    // what runs on startup of node
    public virtual void Run()
    {
        int destRing = -1;
        bool shouldTransmit = false;
        bool shutdown;

        // connect with neighboring nodes
        this.Connect();
        Console.WriteLine(this.nodeNum + "Connected");
        lock (logLocker)
        {
            logFile.WriteLine(DateTime.Now.ToString() + " Connected with token ring " + this.bridgeNumber);
        }

        Random rand = new Random();

        byte[] bFrame;

        // Accept incoming connection and wait for
        // right neighbor to be ready.
        try
        {
            while (true)
            {
                // listen state
                // recieve frame, check if token
                Frame frame = Frame.MakeFrame(Receive());

                if (frame is Token)
                {
                    // check to see if the thread's buffer has something to transmit
                    if (bridgeNumber == 1)
                    {
                        lock (buf1Locker)
                        {
                            if (buffer1.Count != 0)
                                shouldTransmit = true;
                            else
                                shouldTransmit = false;
                        }
                    }
                    else
                    {
                        lock (buf2Locker)
                        {
                            if (buffer2.Count != 0)
                                shouldTransmit = true;
                            else
                                shouldTransmit = false;
                        }
                    }

                    // if it does, enter transmit state
                    if (shouldTransmit)
                    {
                        Transmit(frame);
                    }

                    else
                    {
                        // otherwise, send the token along
                        bFrame = frame.ToBinary();
                        this.sendStream.Write(bFrame, 0, bFrame.Length);
                    }
                }

                // shutdown signal.
                else if (frame.AC == 2 || frame.DA == 255)
                {
                    Console.WriteLine(this.bridgeNumber + " is ready to shutdown");
                    lock (logLocker)
                    {
                        logFile.WriteLine(DateTime.Now.ToString() + " Token ring " + this.bridgeNumber + " is ready to shutdown");
                    }
                    // figure out if the other side is ready to shutdown
                    lock (shutdownLocker)
                    {
                        shutdown = shouldShutdown;
                        if (!shouldShutdown)
                            shouldShutdown = true;
                    }

                    // if not ready to shutdown, go into infinite send state until a shutdown signal comes through the buffer
                    if (!shutdown)
                    {
                        while (true)
                        {
                            // check to see if the thread's buffer has something to transmit
                            if (bridgeNumber == 1)
                            {
                                lock (buf1Locker)
                                {
                                    if (buffer1.Count != 0)
                                        shouldTransmit = true;
                                    else
                                        shouldTransmit = false;
                                }
                            }
                            else
//.........这里部分代码省略.........
开发者ID:Kanazuchi,项目名称:Token-Ring-With-Bridge,代码行数:101,代码来源:Bridge.cs


注:本文中的Frame.ToBinary方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。