public void ReceivePackets() { while (host.Service(1) >= 0) { Event @event; while (host.CheckEvents(out @event) > 0) { Log("[NetLib] Client: revived " + @event.Type.ToString()); switch (@event.Type) { case ENet.EventType.Connect: Log("[NetLib] Connect"); isConnected = true; netEventQueue.Enqueue(new NetEvent(@event.Type)); break; case ENet.EventType.Disconnect: Log("[NetLib] Disconnect"); isConnected = false; netEventQueue.Enqueue(new NetEvent(@event.Type)); break; case ENet.EventType.Receive: var data = @event.Packet.GetBytes(); Packet packet = new Packet(data); netEventQueue.Enqueue(new NetEvent(@event.Type, packet)); @event.Packet.Dispose(); break; } } } }
public void SendPacket(NetCommands command) { Packet sending = new Packet(); sending.Write((uint)command); peer.Send(0, sending.Bytes); }
private void HandlePacket(NetCommands command, Packet packet) { switch (command) { case NetCommands.IdentifySuccessful: isIdentified = true; Log("[NetLib] Identify successful"); OnIdentificationSuccess(); OnIdentificationSuccessEvent?.Invoke(); break; case NetCommands.IdentifyFailure: isIdentified = false; Log("[NetLib] Identify failure: The server didn't accept your access token! It might be already in use or it has expired. Please retry logging in."); var failuerCode = (IdentifyResponse)packet.ReadUint(); OnIdentificationFailure(failuerCode); OnIdentificationFailureEvent?.Invoke(failuerCode); break; case NetCommands.HandshakeSuccess: Log("[NetLib] Handshake successful"); var identificationPacket = GetIdentity(); SendPacket(NetCommands.Identify, identificationPacket); Log("[NetLib] Identifying..."); break; default: Log("[NetLib] command was not implemented"); break; } }
public void HandleAnyPacket(Packet packet) { uint commandInt = packet.ReadUint(); var command = (NetCommands)commandInt; if (command == NetCommands.CustomCommand) { uint customCommand = packet.ReadUint(); Log("[NetLib] handling customCommand " + customCommand.ToString()); HandleCustomPacket(customCommand, packet); } else { Log("[NetLib] handling NetCommands " + ((NetCommands)command).ToString()); HandlePacket(command, packet); } }
public NetEvent(EventType type, Packet packet) { this.type = type; this.packet = packet; }
protected abstract void HandleCustomPacket(uint customCommand, Packet packet);