Ejemplo n.º 1
0
        /// <summary>
        /// Send query
        /// </summary>
        /// <param name="requestType">Request type</param>
        /// <returns>Success</returns>
        private bool SendQuery(ERequestResponseType requestType)
        {
            bool ret = false;

            requestsRequired.SetLastRequestTime(requestType);
            try
            {
                EndPoint endpoint = new IPEndPoint(IPAddress, Port);
                using (MemoryStream stream = new MemoryStream())
                {
                    using (BinaryWriter writer = new BinaryWriter(stream))
                    {
                        char op_code = RequestsRequired.GetOpCode(requestType);
                        writer.Write("SAMP".ToCharArray());
                        writer.Write(ipv4AddressUInt);
                        writer.Write(port);
                        writer.Write(op_code);
                        if (op_code == 'p')
                        {
                            Random r = new Random();
                            r.NextBytes(randomNumbers);
                            writer.Write(randomNumbers);
                        }
                        timestamp[0] = DateTime.Now;
                    }
                    if (socket.SendTo(stream.ToArray(), endpoint) > 0)
                    {
                        ret = true;
                        Receive();
                    }
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);
            }
            return(ret);
        }