protected override bool OnRecv(BaseNetState state, byte msgType, object data) { switch (msgType) { case 0: byte sig = (byte)data; #if DEBUG Console.WriteLine("UdpClient {0}: Disconnected with Reason {1}", state.ToString(), sig); #endif if (state.Initialized) RaiseOnDisconnect(new ConnectionArgs(state.ID, state.Name)); return true; case 2: string name = (string)data; #if DEBUG Console.WriteLine("UdpClient {0}: Name changed to {1}", state.ToString(), name); #endif state.Name = name; return true; case 3: state.Initialized = true; state.ID = (byte)data; #if DEBUG Console.WriteLine("UdpClient {0}: Connected with ID {1}", state.ToString(), state.ID); #endif RaiseOnConnect(new ConnectionArgs(state.ID, state.Name)); return true; } return false; }
protected override bool OnRecv(BaseNetState netState, byte msgType, object data) { UdpState state = netState as UdpState; switch (msgType) { case 0: //Disconnect byte signal = (byte)data; if (state.Connected) { RemoveClient(state.EndPoint); if (state.Connected) RaiseOnDisconnect(new ConnectionArgs(state.ID, state.Name)); #if DEBUG Console.WriteLine("UdpServer: Client {0} Disconnected. Reason {1}", state.ToString(), signal); #endif } return true; case 2: //Name Change if (state != null) { string name = VerifyName((string)data); #if DEBUG Console.WriteLine("UdpServer: Client {0} changing name to {1}", state.ToString(), name); #endif state.Name = name; byte[] nameArr = msgTypes[2].ServerFormatData(name); client.Send(nameArr, nameArr.Length, state.EndPoint); } return true; case 3: //Connection Request byte sig = (byte)data; if (sig == 0)//Want to Connect { if (state.Connected) return true; //already made a request #if DEBUG Console.WriteLine("UdpServer: Remote {0} wants to connect", state.EndPoint); #endif byte id = FreeID(); if (id == 255) return true; state.Connected = true; state.ID = id; states[id] = state; endPoints.Add(state.EndPoint, id); } else if (sig == 1)//Finished Initalization { if (state.Connected && state.Name != "")//Initalize requirement { #if DEBUG Console.WriteLine("UdpServer: Client {0} Connected", state.ToString()); #endif state.Initialized = true; SendMessage(state.ID, msgTypes[3].ServerFormatData((object)state.ID)); RaiseOnConnect(new ConnectionArgs(state.ID, state.Name)); } } else if (sig == 2) //heartbeat response { } return true; case 4: //Auth Info Dictionary<string, string> authInfo = data as Dictionary<string, string>; ConnectionRequestArgs arg = new ConnectionRequestArgs(state.ID, state.Name, authInfo); if (ConnectionRequest != null) ConnectionRequest(this, arg); if (!arg.Approved) Kick(state.ID); return true; } return false; }
/// <summary> /// Handles internal messages, and returns whether the message got handled /// </summary> /// <param name="state">Remote state that sent it</param> /// <param name="msgType">Message Type ID</param> /// <param name="payload">Formatted data from payload</param> /// <returns>True if message was handled, false if not</returns> protected abstract bool OnRecv(BaseNetState state, byte msgType, object data);