Пример #1
0
        private static void SendMessage(EndPoint endpoint, ENetID type, string message = "")
        {
            SendMessage(endpoint as IPEndPoint, type, message);

            if (type == ENetID.ID_ConnectionClosed)
            {
                CloseConnection(endpoint as IPEndPoint);
            }
        }
Пример #2
0
        private static void SendMessage(IPEndPoint endpoint, ENetID type, string message = "")
        {
            if (IPAddress.IsLoopback(endpoint.Address) || endpoint.Address.Address == 0)
            {
                return;
            }

            byte[]      msgarr   = Encoding.ASCII.GetBytes(message);
            List <byte> dataList = new List <byte>(msgarr.Length + 1);

            dataList.Add(Convert.ToByte((int)type));
            dataList.AddRange(msgarr);

            byte[] broadcastmsg = dataList.ToArray();

            Logging.Log($"[SENT] {endpoint.Address}:{endpoint.Port} {type} {msgarr.Length} bytes | {message}");
            serverSocket.SendTo(broadcastmsg, broadcastmsg.Length, SocketFlags.None, clientEndPoint);
        }
Пример #3
0
        private static void OnMessageReceived(byte[] res, int length)
        {
            IPEndPoint receviedEndPoint = clientEndPoint as IPEndPoint;

            if (settings.Whitelist.Count > 0 && !settings.Whitelist.Contains(receviedEndPoint.Address.ToString()))
            {
                Logging.Log($"[WHITELIST] {receviedEndPoint.Address}:{receviedEndPoint.Port} sent a request but is not whitelisted");
                return;
            }

            ENetID cmd     = (ENetID)res[0];
            string encoded = Encoding.ASCII.GetString(res, 1, length - 1);

            Logging.Log($"[RECEIVED] {receviedEndPoint.Address}:{receviedEndPoint.Port} {cmd} {length} bytes | {encoded.Replace(Environment.NewLine, "")}");

            if (cmd != ENetID.ID_HeresPassword && cmd != ENetID.ID_Open && cmd != ENetID.ID_KeepAlive)
            {
                if (!IsRegistered(receviedEndPoint))
                {
                    return;
                }
            }

            try
            {
                if (messageReceivers.ContainsKey(cmd))
                {
                    messageReceivers[cmd]?.Invoke(null, (new object[] {
                        receviedEndPoint,
                        encoded
                    }));
                }
            }
            catch (Exception e)
            {
                Logging.Log(e.Message, true);
            }
        }
Пример #4
0
 public ENetCommandType(ENetID cmd)
 {
     cmdInt = cmd;
 }