BF2Statistics.Gamespy.Net.GamespyUdpPacket.SetBufferContents C# (CSharp) Method

SetBufferContents() public method

Sets the contents of the SocketAsyncEventArgs buffer, so a reply can be sent to the remote host connection
public SetBufferContents ( byte contents ) : int
contents byte The new contents to set the buffer to
return int
        public int SetBufferContents(byte[] contents)
        {
            BufferDataToken token = AsyncEventArgs.UserToken as BufferDataToken;
            if (contents.Length > token.BufferBlockSize)
                throw new ArgumentOutOfRangeException("contents", "Contents are larger then the allocated buffer block size.");

            // Copy contents to buffer, then set buffer position
            Array.Copy(contents, 0, AsyncEventArgs.Buffer, token.BufferOffset, contents.Length);
            AsyncEventArgs.SetBuffer(token.BufferOffset, contents.Length);
            return contents.Length;
        }

Usage Example

        /// <summary>
        /// Called when a connection comes in on the CDKey server
        /// </summary>
        /// known messages
        ///  \ka\ = keep alive from the game server every 20s, we don't care about this
        ///  \auth\ ... = authenticate cd key, this is what we care about
        ///  \disc\ ... = disconnect cd key, because there's checks if the cd key is in use, which we don't care about really, but we could if we wanted to
        /// </remarks>
        protected override void ProcessAccept(GamespyUdpPacket Packet)
        {
            // If we dont reply, we must manually release the EventArgs back to the pool
            bool replied = false;

            try
            {
                // Decrypt message
                IPEndPoint remote = (IPEndPoint)Packet.AsyncEventArgs.RemoteEndPoint;
                string decrypted = Xor(Encoding.UTF8.GetString(Packet.BytesRecieved)).Trim('\\');

                // Ignore keep alive pings
                if (!decrypted.StartsWith("ka"))
                {
                    Dictionary<string, string> recv = ConvertToKeyValue(decrypted.Split('\\'));
                    if (recv.ContainsKey("auth") && recv.ContainsKey("resp") && recv.ContainsKey("skey"))
                    {
                        // Normally you would check the CD key database for the CD key MD5, but we arent Gamespy, we dont care
                        DebugLog.Write("CDKey Check Requested from: {0}:{1}", remote.Address, remote.Port);
                        string reply = String.Format(@"\uok\\cd\{0}\skey\{1}", recv["resp"].Substring(0, 32), recv["skey"]);

                        // Set new packet contents, and send a reply
                        Packet.SetBufferContents(Encoding.UTF8.GetBytes(Xor(reply)));
                        base.ReplyAsync(Packet);
                        replied = true;
                    }
                    else if (recv.ContainsKey("disc"))
                    {
                        // Handle, User disconnected from server
                    }
                    else
                    {
                        DebugLog.Write("Incomplete or Invalid CDKey Packet Received: " + decrypted);
                    }
                }
            }
            catch (Exception E)
            {
                Program.ErrorLog.Write("ERROR: [MasterServer.CDKeySocket_OnDataReceived] " + E.Message);
            }
            finally
            {
                // Release so that we can pool the EventArgs to be used on another connection
                if (!replied)
                    base.Release(Packet.AsyncEventArgs);
            }
        }
All Usage Examples Of BF2Statistics.Gamespy.Net.GamespyUdpPacket::SetBufferContents