private void HandleIncomingData() { while (!isShutdownRequested) { while (client.Available <= 0) { ; } byte[] buffer = new byte[1024]; int received = client.GetStream().Read(buffer, 0, buffer.Length); KNet_Packet packet = KNet.DeserializePacket(buffer, 0, received); if (KNet.debug) { Console.WriteLine("Received Packet {0}", packet.rpcName); } if (packet.rpcName == KNet.INTERNAL_LOGIN_SUCCESS) { //Set account data continue; } KNet.ExecutePacket(packet); } client.Close(); }
private void HandleLogins() { while (!isShutdownRequested) { while (loginQueue.Count == 0) { ; } KNet_User user = loginQueue.Dequeue(); //Check database for user, send user data; if user not found, refuse them. //If login success: KNet_Packet packet = new KNet_Packet { rpcName = KNet.INTERNAL_LOGIN_SUCCESS, rpcArgs = new object[0], //This will be Account Data in the future rpcTarget = user.id }; if (KNet.debug) { Console.WriteLine("{0} has been logged in.", user.username); } activeUsers.Add(user); SendPacket(packet); } }
private void HandleIncomingData() { while (!isShutdownRequested) { byte[] buffer = new byte[1024]; EndPoint endPoint = new IPEndPoint(IPAddress.Any, port); int received = receiveSocket.ReceiveFrom(buffer, ref endPoint); IPEndPoint ipEndPoint = (IPEndPoint)endPoint; KNet_Packet packet = KNet.DeserializePacket(buffer, 0, received); packet.origin = ipEndPoint.Address.GetAddressBytes(); if (packet.isReliable) { KNet_Packet freeReliable = new KNet_Packet() { rpcName = "INTERNAL_FREE-RELIABLE", packetID = packet.packetID, isReliable = false, rpcTarget = packet.origin }; SendPacket(packet); } Console.WriteLine("Received Packet {0} from {1}", packet.rpcName, ipEndPoint.Address); ParsePacket(packet); } receiveSocket.Close(); receiveSocket.Dispose(); sendSocket.Close(); sendSocket.Dispose(); }
private async void LoginUser(Socket socket) { if (KNet.debug) { Console.WriteLine("Received connection from {0}", socket.RemoteEndPoint); } await Task.Run(() => { byte[] buffer = new byte[1024]; int received = socket.Receive(buffer); KNet_Packet packet = KNet.DeserializePacket(buffer, 0, received); if (packet.rpcName == KNet.INTERNAL_LOGIN && (activeUsers.Count < maxPlayers || maxPlayers == 0)) { KNet_User user = new KNet_User(packet.rpcArgs[0] as string, packet.rpcArgs[1] as string, DateTime.UtcNow.Ticks, socket); if (KNet.debug) { Console.WriteLine("{0} is logging in with ID {1}", socket.RemoteEndPoint, user.id); } loginQueue.Enqueue(user); } else { socket.Disconnect(false); } }); }
private void HandleIncomingData() { while (!isShutdownRequested) { byte[] buffer = new byte[1024]; int received = receiveSocket.Receive(buffer); KNet_Packet packet = KNet.DeserializePacket(buffer, 0, received); if (packet.isReliable) { KNet_Packet freeReliable = new KNet_Packet() { isReliable = false, packetID = packet.packetID, rpcName = "INTERNAL_FREE-RELIABLE", }; SendPacket(freeReliable); } Console.WriteLine("Received Packet {0}", packet.rpcName); KNet.ExecutePacket(packet); } sendSocket.Close(); sendSocket.Dispose(); receiveSocket.Close(); receiveSocket.Dispose(); }
public static byte[] SerializePacket(KNet_Packet packet) { BinaryFormatter formatter = new BinaryFormatter(); using (MemoryStream memStream = new MemoryStream()) { formatter.Serialize(memStream, packet); return(memStream.ToArray()); } }
public void SendRPC(string name, long target, params object[] args) { KNet_Packet packet = new KNet_Packet { rpcName = name, rpcTarget = target, rpcArgs = args }; SendPacket(packet); }
public static KNet_Packet DeserializePacket(byte[] buffer, int offset, int count) { using (MemoryStream memStream = new MemoryStream()) { BinaryFormatter formatter = new BinaryFormatter(); memStream.Write(buffer, offset, count); memStream.Seek(0, SeekOrigin.Begin); KNet_Packet packet = (KNet_Packet)formatter.Deserialize(memStream); return(packet); } }
public void SendRPC(string name, byte[] target, bool reliable, params object[] args) { KNet_Packet packet = new KNet_Packet { rpcName = name, rpcTarget = target, rpcArgs = args, isReliable = reliable }; SendPacket(packet); }
public static void FreeReliablePacket(long packetID) { foreach (KNet_ReliablePacket p in packetQueue.ToArray()) { KNet_Packet packet = DeserializePacket(p.packet, 0, p.packet.Length); if (packet.packetID == packetID) { packetQueue.Remove(p); return; } } }
private void SendPacket(KNet_Packet packet) { byte[] buffer = KNet.SerializePacket(packet); if (packet.isReliable) { KNet.SendUDPReliable(buffer, host, sendSocket); } else { KNet.SendUDP(buffer, host, sendSocket); } Console.WriteLine("Sent Packet {0}", packet.rpcName); }
private void ParsePacket(KNet_Packet packet) { if (packet.rpcName.StartsWith("RPC")) { if (IsUserActive(packet.sender)) { SendPacket(packet); } } else if (packet.rpcName.StartsWith("CMD")) { KNet.ExecutePacket(packet); } }
private void HandleIncomingData() { while (!isShutdownRequested) { byte[] buffer = new byte[1024]; if (listener.Pending()) { Socket socket = listener.AcceptSocket(); LoginUser(socket); } foreach (KNet_User u in activeUsers.ToArray()) { if (u.socket.Available > 0) { int received = u.socket.Receive(buffer); KNet_Packet packet = KNet.DeserializePacket(buffer, 0, received); packet.sender = u.id; if (KNet.debug) { Console.WriteLine("Received packet {0} from {1}", packet.rpcName, u.username); } if (packet.rpcName == KNet.INTERNAL_LOGOUT) { activeUsers.Remove(u); u.socket.Disconnect(false); continue; } else if (packet.rpcName == KNet.INTERNAL_PING) { KNet_Packet ping = new KNet_Packet() { rpcName = KNet.INTERNAL_PING }; SendPacket(ping); } ParsePacket(packet); } } } listener.Stop(); }
private void ParsePacket(KNet_Packet packet) { originOfCurrentProcessedPacket = packet.origin; if (packet.rpcName.StartsWith("RPC")) { if (IsUserActive(packet.origin)) { SendPacket(packet); } } else if (packet.rpcName.StartsWith("CMD")) { KNet.ExecutePacket(packet); } else if (packet.rpcName == "INTERNAL_FREE-RELIABLE") { KNet.FreeReliablePacket(packet.packetID); } }
public static bool ExecutePacket(KNet_Packet packet) { if (packet.rpcName.StartsWith("RPC")) { if (client == null) { return(false); } MethodInfo method = client.GetType().GetMethod(packet.rpcName); if (method == null) { return(false); } method.Invoke(client, packet.rpcArgs); return(true); } else if (packet.rpcName.StartsWith("CMD")) { if (server == null) { return(false); } MethodInfo method = server.GetType().GetMethod(packet.rpcName); if (method == null) { return(false); } method.Invoke(server, packet.rpcArgs); return(true); } else if (packet.rpcName == "INTERNAL_FREE-RELIABLE") { FreeReliablePacket(packet.packetID); return(true); } else { return(false); } }
public void CMD_Login(string username, string passwordHash) { if (activeUsers.Count >= maxPlayers && maxPlayers > 0) { KNet_Packet packet = new KNet_Packet { rpcName = "RPC_LoginFailed", rpcArgs = new object[] { "The Server was full." }, //Reason for failure rpcTarget = originOfCurrentProcessedPacket, packetID = DateTime.UtcNow.Ticks, isReliable = true }; SendPacket(packet); } KNet_User user = new KNet_User(username, passwordHash, originOfCurrentProcessedPacket); loginQueue.Enqueue(user); }
private void SendPacket(KNet_Packet packet) { byte[] buffer = KNet.SerializePacket(packet); try { client.GetStream().Write(buffer, 0, buffer.Length); }catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; if (KNet.debug) { Console.WriteLine("{0}\n{1}", e.Message, e.StackTrace); } Console.ResetColor(); isShutdownRequested = true; } if (KNet.debug) { Console.WriteLine("Sent Packet {0}", packet.rpcName); } }
public void SendPacket(KNet_Packet packet) { byte[] buffer = KNet.SerializePacket(packet); if (packet.rpcTarget.Length == 0 || (packet.rpcTarget.Length == 1 && packet.rpcTarget[0] == 0)) { Console.WriteLine("Sending packet {0} to all users.", packet.rpcName); foreach (KNet_User user in activeUsers.ToArray()) { IPAddress address = new IPAddress(user.id); EndPoint endPoint = new IPEndPoint(address, port); if (packet.isReliable) { KNet.SendUDPReliable(buffer, endPoint, sendSocket); } else { KNet.SendUDP(buffer, endPoint, sendSocket); } } } else { IPAddress address = new IPAddress(packet.rpcTarget); Console.WriteLine("Sending packet {0} to {1}", packet.rpcName, address); EndPoint endPoint = new IPEndPoint(address, port); if (packet.isReliable) { KNet.SendUDPReliable(buffer, endPoint, sendSocket); } else { KNet.SendUDP(buffer, endPoint, sendSocket); } } }
public void SendPacket(KNet_Packet packet) { byte[] buffer = KNet.SerializePacket(packet); if (packet.rpcTarget == 0) { if (KNet.debug) { Console.WriteLine("Sending packet {0} to all users.", packet.rpcName); } foreach (KNet_User user in activeUsers.ToArray()) { SendData(buffer, user); } } else { foreach (KNet_User u in activeUsers) { if (u.id == packet.rpcTarget) { if (KNet.debug) { Console.WriteLine("Sending packet {0} to {1}", packet.rpcName, u.username); } SendData(buffer, u); return; } } if (KNet.debug) { Console.WriteLine("User {0} not found.", packet.rpcTarget); } } }
private void HandleLogins() { while (!isShutdownRequested) { while (loginQueue.Count == 0) { ; } KNet_User user = loginQueue.Dequeue(); //Check database for user, send user data; if user not found, refuse them. //If login success: KNet_Packet packet = new KNet_Packet { rpcName = "RPC_LoginSuccess", rpcArgs = new object[0], //This will be Account Data in the future rpcTarget = user.id, packetID = DateTime.UtcNow.Ticks, isReliable = true }; SendPacket(packet); activeUsers.Add(user); } }