コード例 #1
0
ファイル: GClient.cs プロジェクト: SteamHelpers/SGMulti
        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();
        }
コード例 #2
0
ファイル: GServer.cs プロジェクト: SteamHelpers/SGMulti
        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();
                }
            }
        }