/// <summary> /// Called when a packet is wanting to be sent, or was recieved. /// </summary> /// <param name="packet">The packet that wants to be sent.</param> /// <param name="isSelf">Is the object owned by us?</param> protected virtual void OnSynchronize(Packet packet, bool isSelf) { if (packet.IsWriteable) { // Write the position. Vector3 position = transform.position; packet.Write(position); // Write the rotation. Quaternion rotation = transform.rotation; packet.Write(rotation); } else if(!isSelf) { Vector3 position = packet.ReadVector3(); Quaternion rotation = packet.ReadQuaternion(); transform.position = position; transform.rotation = rotation; } }
public void Update() { byte[] recvBuffer; uint msgSize; CSteamID steamIDRemote; while(SteamNetworking.IsP2PPacketAvailable(out msgSize)){ recvBuffer = new byte[msgSize]; if(!SteamNetworking.ReadP2PPacket( recvBuffer, msgSize, out msgSize, out steamIDRemote )) break; if (steamIDRemote != ConnectedTo) { Packet packet = new Packet(this, false); packet.Bytes = recvBuffer; Debug.LogFormat("Recieved packet 0x{0} from Server.", packet.PacketID.ToString("X4")); try { if (packet.PacketID == Packet.PACKET_AUTHFAILED) { ServerExiting(); } else if(packet.PacketID == Packet.PACKET_USERKICKED) { string reason = ""; try { reason = packet.ReadString(); } catch { } ServerExiting(EGSConnectionError.Unknown, reason); } else if(packet.PacketID == Packet.PACKET_INTRODUCTION) { if (packet.ReadString() == _GenerateString) { uint pcbTicket; HAuthTicket ticketSess; byte[] ticket = RequestUserToken(out pcbTicket, out ticketSess); // Successfully introduced from server. Packet msg = CreatePacket(Packet.PACKET_DOAUTH); msg.Write((int)pcbTicket); msg.Write(ticket); msg.Send(EP2PSend.k_EP2PSendReliable, 0); } else { // Failed to introduce. ServerExiting(); } } } finally { packet.Seek(); } if (DataRecieved != null) DataRecieved(this, packet, 0); packet.Dispose(); } } foreach (int channel in _ExtraChannels) { if (channel == 0) // We Handle it up above. continue; while (SteamNetworking.IsP2PPacketAvailable(out msgSize, channel)) { recvBuffer = new byte[msgSize]; if (!SteamNetworking.ReadP2PPacket(recvBuffer, msgSize, out msgSize, out steamIDRemote)) break; Packet packet = new Packet(this, false); packet.Bytes = recvBuffer; if (DataRecieved != null) DataRecieved(this, packet, channel); packet.Dispose(); } } SteamAPI.RunCallbacks(); }
void SendFailedConnection(CSteamID recip, EGSConnectionError connectionError, string reason = "") { Packet packet = new Packet(this, true, Packet.PACKET_AUTHFAILED); packet.Write((int)connectionError); if(reason != string.Empty) { packet.Write(reason); } packet.Send(EP2PSend.k_EP2PSendReliable, 0, recip); }
void OnClientWantsAuth(CSteamID steamIDRemote, byte[] authPub, int authCub) { if(_Players.ContainsKey(steamIDRemote)){ // This got called again, why did it? return; } if(_Players.Count >= MaxPlayers){ SendFailedConnection(steamIDRemote, EGSConnectionError.ServerFull); } if (SteamGameServer.BeginAuthSession(authPub, authCub, steamIDRemote) != EBeginAuthSessionResult.k_EBeginAuthSessionResultOK){ SendFailedConnection(steamIDRemote, EGSConnectionError.AuthenticationError); } _Players.Add(steamIDRemote, EPlayerStatus.Pending); if(PlayerAuthenticated != null) PlayerAuthenticated(steamIDRemote); Packet msg = new Packet(this, true, Packet.PACKET_AUTHSUCCESS); msg.Send(EP2PSend.k_EP2PSendReliable, 0, steamIDRemote); }
public void Update() { if (!Initalized) return; GameServer.RunCallbacks(); if (SteamConnected) { if (UpdateServerDetails != null) UpdateServerDetails(); } byte[] recvBuffer; uint msgSize; CSteamID steamIDRemote; while(SteamGameServerNetworking.IsP2PPacketAvailable(out msgSize)){ recvBuffer = new byte[msgSize]; if(!SteamGameServerNetworking.ReadP2PPacket( recvBuffer, msgSize, out msgSize, out steamIDRemote )) break; Packet packet = new Packet(this, false); packet.Bytes = recvBuffer; try{ if (packet.PacketID == Packet.PACKET_DOAUTH) { // TODO: Cause server to handle Steam Authentication. int byteCount = packet.ReadInteger(); OnClientWantsAuth(steamIDRemote, packet.ReadByteArray(byteCount), byteCount); } else if (packet.PacketID == Packet.PACKET_SESSIONINFO) { Packet msg = CreatePacket(Packet.PACKET_SESSIONINFO); msg.Write(SteamGameServer.GetSteamID().m_SteamID); msg.Write(SteamGameServer.BSecure()); msg.Write(Name); msg.Send(EP2PSend.k_EP2PSendReliable, 0, steamIDRemote); } else if (packet.PacketID == Packet.PACKET_USERDISCONNECTED) { if (_Players.ContainsKey(steamIDRemote)) { if(_Players[steamIDRemote] == EPlayerStatus.Pending) { SteamGameServer.SendUserDisconnect(steamIDRemote); _Players.Remove(steamIDRemote); } else { RemovePlayerFromServer(steamIDRemote); } } else { Debug.LogWarning("How the absoulte loving TomSka joke in a game did this happen? No matching client???"); } } else if(packet.PacketID == Packet.PACKET_INTRODUCTION) { string connectionString = packet.ReadString(); _UserNames.Add(steamIDRemote, packet.ReadString()); Packet msg = CreatePacket(Packet.PACKET_INTRODUCTION); msg.Write(connectionString); msg.Send(EP2PSend.k_EP2PSendReliable, 0, steamIDRemote); Debug.LogFormat("Sent PACKET_INTRODUCTION with connection string of \"{0}\" to {1}.", connectionString, steamIDRemote); } } finally{ packet.Seek(); } if (DataRecieved != null) DataRecieved(steamIDRemote, packet, 0); packet.Dispose(); } foreach(int channel in _ExtraChannels) { if (channel == 0) // We Handle it up above. continue; while (SteamGameServerNetworking.IsP2PPacketAvailable(out msgSize, channel)) { recvBuffer = new byte[msgSize]; if (!SteamGameServerNetworking.ReadP2PPacket(recvBuffer, msgSize, out msgSize, out steamIDRemote)) break; Packet packet = new Packet(this, false); packet.Bytes = recvBuffer; if (DataRecieved != null) DataRecieved(steamIDRemote, packet, channel); packet.Dispose(); } } }