Brunet.Transport.TcpEdge.WriteToBuffer C# (CSharp) Метод

WriteToBuffer() публичный Метод

public WriteToBuffer ( Brunet p ) : void
p Brunet
Результат void
    public void WriteToBuffer(Brunet.Util.ICopyable p) {
      int plength = p.CopyTo(_buffer, _written + 2);
      if( plength > Int16.MaxValue ) {
        throw new EdgeException(true,
                    String.Format("Packet too long: {0}",plength));
      }
      // 
      //The length at the beginning:
      Brunet.Util.NumberSerializer.WriteShort((short)plength, _buffer, _written);
      //Also write the length at the end so we can detect a bad packet
      //as we have seen on planetlab:
      Brunet.Util.NumberSerializer.WriteShort((short)plength, _buffer, _written + 2 + plength);
      //If the buffer did not fill up, we have not yet thrown an exception:
      _written += (plength + 4);
    }

Usage Example

Пример #1
0
        public void HandleEdgeSend(Edge from, ICopyable p)
        {
            TcpEdge sender = (TcpEdge)from;

            try {
                bool flushed = true;
                lock ( sender ) {
                    //Try to fill up the buffer:
                    sender.WriteToBuffer(p);
                    //Okay, we loaded the whole packet into the TcpEdge's buffer
                    //now it is time to try to flush the buffer:
                    flushed = sender.Flush();
                }
                if (!flushed)
                {
                    /*
                     * We should remember to try again when the socket is
                     * writable
                     */
                    ActionQueue.Enqueue(new SendWaitAction(sender.Socket));
                }
            }
            catch (EdgeException ex) {
                if (false == ex.IsTransient)
                {
                    //Go ahead and forget about this socket.
                    RequestClose(from);
                    ActionQueue.Enqueue(new CloseAction(sender, null));
                }
                //Rethrow the exception
                throw;
            }
            catch (Exception x) {
                //Assume any other error is transient:
                throw new EdgeException(true, String.Format("Could not send on: {0}", from), x);
            }
        }