Esempio n. 1
0
        // By this call you specify that Q3DemoStream must poll underlying stream for a new datagram.
        // All unreaded data stored in readBuffer will be erased.
        // It is like Flush () but for reading purposes.
        public int ReadNew(byte [] array, int offset, int count, out PacketKind kind)
        {
            int bytesReceieved = this.ReadNew(array, offset, count);

            kind = this.readPacketKind;

            return(bytesReceieved);
        }
Esempio n. 2
0
        // By this call you specify that Q3NetworkStream must poll underlying stream for a new datagram.
        // All unreaded data stored in readBuffer will be erased.
        // It is like Flush () but for reading purposes.
        public int ReadNew(byte [] array, int offset, int count, out PacketKind kind)
        {
            int bytesReceieved = this.ReadNew(array, offset, count);

            kind = (( Q3NetworkStreamOld )baseStream).ReadPacketKind;                   // FIXIT: make q3-specified abstract stream instead of System.IO.Stream

            return(bytesReceieved);
        }
Esempio n. 3
0
        // By this call you specify that Q3HuffmanStream must poll underlying stream for a new datagram.
        // All unreaded data stored in readBuffer will be erased.
        // It is like Flush () but for reading purposes.
        public int ReadNew(byte [] array, int offset, int count, out PacketKind kind)
        {
            int bytesReceieved = this.ReadNew(array, offset, count);

            kind = baseStream is Q3DatagramStream ? (( Q3DatagramStream )baseStream).ReadPacketKind : PacketKind.Unknown;                       // FIXIT: make q3-specified abstract stream instead of System.IO.Stream

            return(bytesReceieved);
        }
Esempio n. 4
0
        public void Write(byte [] array, int offset, int count, PacketKind kind)
        {
            if (this.writePacketKind != kind && msWriteBuffer.Length != 0)
            {
                this.Flush();
            }

            this.writePacketKind = kind;
            this.Write(array, offset, count);
        }
Esempio n. 5
0
        private bool ReadConnectionlessPacket(out string command, out byte [] data)
        {
            byte []    buffer     = new byte [256];
            PacketKind packetKind = q3NetStream.BeginReadPacket();
            int        bytesRead  = q3NetStream.Read(buffer, 0, buffer.Length);
            bool       res        = false;

            if (packetKind != PacketKind.Connectionless)
            {
                command = null;
                data    = null;

                return(false);
            }

            string data_cmd = Encoding.ASCII.GetString(buffer, 0, 17);

            if (data_cmd.Contains("challengeResponse"))
            {
                command = "challengeResponse";
                res     = true;
            }
            else if (data_cmd.Contains("connectResponse"))
            {
                command = "connectResponse";
                res     = true;
            }
            else
            {
                command = null;
            }

            if (res)
            {
                int data_len = bytesRead - command.Length;

                if (data_len != 0)
                {
                    data = new byte [bytesRead - command.Length];
                    Array.Copy(buffer, command.Length, data, 0, bytesRead - command.Length);
                }
                else
                {
                    data = null;
                }
            }
            else
            {
                data = null;
            }

            return(res);
        }
Esempio n. 6
0
 public void ProcessPacket(NetIncomingMessage im, PacketKind kind)
 {
     switch (kind)
     {
     case PacketKind.NewCharacter:
         var om = server.NetServer.CreateMessage();
         om.Write((byte)PacketKind.NewCharacter);
         om.Write((byte)1);                      //allowed (1 byte as Lidgren packs bools)
         om.Write(1000);                         //Credits
         server.NetServer.SendMessage(om, connection, NetDeliveryMethod.ReliableOrdered);
         break;
     }
     server.NetServer.Recycle(im);
 }
Esempio n. 7
0
        public override void Flush()
        {
            #region Exception checks
            if (baseStream == null)
            {
                throw new ObjectDisposedException("sock");
            }
            #endregion Exception checks

            baseStream.Write(msWriteBuffer.GetBuffer(), 0, ( int )msWriteBuffer.Length);
            msWriteBuffer.SetLength(0);
            justFlushed     = true;
            writePacketKind = PacketKind.ConnectionOriented;
        }
Esempio n. 8
0
        //
        // Summary:
        //     Flush is not necessary for this kind of stream
        //
        // Exceptions:
        //   System.ObjectDisposedException:
        //     The stream is closed.
        public override void Flush()
        {
            #region Exception checks
            if (sock == null)
            {
                throw new ObjectDisposedException("sock");
            }
            #endregion Exception checks

            sock.Send(msWriteBuffer.GetBuffer(), ( int )msWriteBuffer.Length, SocketFlags.None);
            msWriteBuffer.SetLength(0);
            justFlushed     = true;
            writePacketKind = PacketKind.ConnectionOriented;
        }
Esempio n. 9
0
        public virtual void BeginWritePacket(PacketKind packetKind)
        {
            if (msWriteBuffer != null)
            {
                msWriteBuffer.SetLength(0);
            }
            bytesWritten = 0;

            if (underlying is Q3DatagramStream)
            {
                (underlying as Q3DatagramStream).BeginWritePacket(packetKind);
            }
            else
            {
                writePacketKind = packetKind;
            }
        }
Esempio n. 10
0
        private void ReadPacket(Q3HuffmanStream stream)
        {
            List <string> cmdLog  = new List <string> ();
            PacketKind    pktKind = stream.BeginRead();

            this.reliableAcknowledge = stream.ReadInt32();
            ServerCommandType cmd;

            while (ServerCommandType.EOF != (cmd = ( ServerCommandType )stream.ReadByte()))
            {
                switch (cmd)
                {
                case ServerCommandType.Nop:
                    cmdLog.Add("Nop");
                    break;

                case ServerCommandType.ServerCommand:
                    this.ParseCommandString(stream);
                    cmdLog.Add("ServerCommand");
                    break;

                case ServerCommandType.GameState:
                    this.ParseGamestate(stream);
                    cmdLog.Add("GameState");
                    break;

                case ServerCommandType.Snapshot:
                    this.ParseSnapshot(stream);
                    cmdLog.Add("Snapshot");
                    break;

                case ServerCommandType.Download:
                    // We never download ;)
                    return;

                default:
                    // Unknown command
                    return;
                }
            }

            stream.EndRead();
        }
Esempio n. 11
0
 public PacketData(ushort size, DateTime sendPoint, PacketKind kind)
 {
     Size = size; SendPoint = sendPoint; Kind = kind;
 }
Esempio n. 12
0
        //
        // Summary:
        //     Reads data from the Q3Network.Q3NetworkStream.
        //
        // Parameters:
        //   array:
        //     The array used to store obtained bytes.
        //
        //   offset:
        //     The byte offset in array at which to begin writing data read from the stream.
        //
        //   count:
        //     The number of bytes to read.
        //
        // Returns:
        //     The number of bytes that were read into the byte array. If the end
        //     of the stream has been reached, zero or the number of bytes read is returned.
        //
        // Exceptions:
        //   System.ArgumentNullException:
        //     array is null.
        //
        //   System.ArgumentOutOfRangeException:
        //     offset or count is less than zero.  -or- array length minus the index starting
        //     point is less than count.
        //
        //   System.ObjectDisposedException:
        //     The stream is closed.
        public override int Read(byte [] array, int offset, int count)
        {
            #region Exception checks
            if (array == null)
            {
                throw new ArgumentNullException("array");
            }

            if (sock == null)
            {
                throw new ObjectDisposedException("baseStream");
            }

            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException("offset");
            }

            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            if (array.Length < offset)
            {
                throw new ArgumentOutOfRangeException("offset");
            }

            if (array.Length < offset + count)
            {
                throw new ArgumentOutOfRangeException("count");
            }
            #endregion Exception checks

            if (msReadBuffer.Position >= this.dGramLen && this.dGramLen != 0)
            {
                throw new InvalidOperationException
                          ("Read position reached the end of the buffer." +
                          "Call ReadNew () instead of Read () in this case." +
                          "By calling ReadNew () you will be also notified about kind of received datagram");
            }

            if (msReadBuffer.Position == 0)
            {
                // Ready to peek new datagram from network
                int bytesRead = sock.Receive(readBuffer, READ_BUFFER_SIZE, SocketFlags.None);
                this.dGramLen = bytesRead;

                if (readBuffer [0] == 0xff && readBuffer [1] == 0xff &&
                    readBuffer [2] == 0xff && readBuffer [3] == 0xff)                           // Connectionless packet
                {
                    this.readPacketKind = PacketKind.Connectionless;
                }
                else
                {
                    connection.IncomingSequence = BitConverter.ToInt32(readBuffer, 0);
                    this.readPacketKind         = PacketKind.ConnectionOriented;

                    if ((connection.IncomingSequence & FRAGMENT_BIT) != 0)
                    {
                        int seq = connection.IncomingSequence & (~FRAGMENT_BIT);

                        if (this.fragmentSequence != seq)
                        {
                            this.fragmentSequence = seq;
                            Array.Copy(BitConverter.GetBytes(seq), fragmentBuffer, 4);
                            this.fDataLen = 4;
                        }

                        int fragmentStart  = BitConverter.ToInt16(readBuffer, 4);                               // Needed only for warning (fragmentStart != this.fDataLen)
                        int fragmentLength = BitConverter.ToInt16(readBuffer, 6);

                        Array.Copy(readBuffer, 8, fragmentBuffer, this.fDataLen, fragmentLength);
                        this.fDataLen += fragmentLength;

                        if (fragmentLength != FRAGMENT_SIZE)
                        {
                            connection.IncomingSequence = seq;
                            Array.Copy(fragmentBuffer, readBuffer, fDataLen);
                            this.dGramLen = fDataLen;

                            return(bytesRead);
                        }
                        else
                        {
                            this.Read(array, offset, count);

                            if (fragmentStart != 0)                                     // We're not on the top of the stack (relative to first Read())
                            {
                                return(bytesRead);
                            }
                        }
                    }
                }

                msReadBuffer.Position = 4;
            }

            // Return the next part of previously obtained packet data
            int bytesToReturn = ( int )(count + msReadBuffer.Position > this.dGramLen ?
                                        this.dGramLen - msReadBuffer.Position : count);
            msReadBuffer.Read(array, 0, bytesToReturn);

            return(bytesToReturn);
        }