Some question for headless server!!

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

wakawa2003
Posts: 14
Joined: 03 May 2019, 15:04

Some question for headless server!!

Postby wakawa2003 » 26 Jun 2019, 03:36

Hi!
i'm reading this blog: https://smartfoxserver.com/blog/best-of-both-worlds-sfs2x-server-side-unity-for-realtime-games-p2/
And i have some question:
1. In this blog have a sentence:

Code: Select all

In order to start a new game we create a Room and join all the players inside. Then  when clients are ready to start, we’ll spawn a new headless Unity server and tell each player to connect to it. This means that clients will use a maximum of 2 simultaneous connections, one toward SFS2X and the other toward the Unity server. With this approach everyone will be able to receive events from SFS2X (lobby updates, chat/buddy messages etc.) while the game is running.

->that mean we need to creat double SmartFox sfs=new SmartFox(); for sfs2x server and unity headless server in client?

2. How can i synchronized some thing like Object's position or rotation in client and unity headless server? :roll: :roll: :roll: :roll:
Thank you!
User avatar
Lapo
Site Admin
Posts: 23007
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Some question for headless server!!

Postby Lapo » 26 Jun 2019, 07:14

Hi,
->that mean we need to creat double SmartFox sfs=new SmartFox(); for sfs2x server and unity headless server in client?

No,
the SmartFoxServer instance is always one in this system. What we need to start is a new Unity engine from the server side.
This instance contains the game logic and data and it will control the game associated to the Room that was launched.

Makes sense?

If you check the code under the "Launching server side processes" section, you can see how new Unity process is launched and you can read more about the technical details.

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
wakawa2003
Posts: 14
Joined: 03 May 2019, 15:04

Re: Some question for headless server!!

Postby wakawa2003 » 26 Jun 2019, 07:35

Yes. i know we need start new Unity in server side.
Blog says how can i creat new Unity server and pass port into that.
But i don't know how to send or receive some data between Unity server and client? :?: :?:
User avatar
Lapo
Site Admin
Posts: 23007
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Some question for headless server!!

Postby Lapo » 28 Jun 2019, 17:45

In this article
https://smartfoxserver.com/blog/best-of ... -games-p2/
we explain how to pass init parameters to the Unity headless server. This is useful for setting up the game etc.

Typically you shouldn't need to continuosly send data between SFS2X and the headless Unity while the game is running. The Unity engine is supposed to handle the game logic in its entirety.
Of course you can still send events to SFS2X if something "interesting happens", such as the game is finished and you want to notify SFS2X for some reason. You can use HTTP and invoke a custom servlet, for example.

Otherwise if you need continuous communication you will need to establish a local connection via socket to SFS2X, though I would probably try to design the game without it, if possible.

To give you an example, imagine a real-time racing game for 8 players per "Room".
You want all the User and Room Management, Buddy lists and matchmaking handled by SFS2X. When 8 players are matched together you can create a new Room and assign a new Unity headless server to it.

The Unity headless is sent a bunch of parameters to setup the game and from there each player will connect to the Unity server for the racing game. At this point you've two connections from each player, one for the SFS2X services and one for the Unity server, to handle the game.

The race beings and all of the game related messaging is happening between the client and the Unity server. If anyone disconnects from Unity you will handle the logic in there. When the game ends you will probably want to save the scores, the placement of each player etc. Maybe in a database. Here you can call a custom SFS2X servlet from Unity and perform some database work from there.

Or you may just decide that you prefer to talk to the database directly from Unity and skip the middle-man, which is also not a bad idea.

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
yuneeb90
Posts: 3
Joined: 18 Jul 2022, 07:27

Re: Some question for headless server!!

Postby yuneeb90 » 21 Jul 2022, 08:11

I am having a problem starting the unity headless server, whenever we try to load the headless server build from smart fox extension (we have followed exactly how it was mentioned in the article) but we are getting the permission denied error 13. Here is the log generated from the server, along with the image of the directory structure.

Code: Select all

21 Jul 2022 | 08:01:39,650 | INFO  | unity-worker:7000 | Extensions |     | {__lib__}: Error launching Unity executable: java.io.IOException: Cannot run program "/home/sfsuser/SmartFoxServer_2X/SFS2X/extensions/Unity/Unity Headless Server.exe" (in directory "/home/sfsuser/SmartFoxServer_2X/SFS2X/extensions/Unity"): error=13, Permission denied


And the code used in the extension is as follows,

Code: Select all

public class OverlordsRoomExtension extends SFSExtension
{
   String UnityExe = "Unity Headless Server.exe";
   String WorkingDir = new File("extensions/Unity/").getAbsolutePath();
   Thread LauncherThread;
   
   private class UnityRunner implements Runnable
   {
      private int TCPPort;
      
      public UnityRunner(int port)
      {
         this.TCPPort = port;
         try
         {
            getParentRoom().setVariable(new SFSRoomVariable("port", port));
         }
         catch(Exception ex)
         {
            trace(ex);
         }
      }
      
      @Override
      public void run()
      {
         try
         {
            String cmd = WorkingDir + "/" + UnityExe;
                        
            ProcessBuilder processBuilder = new ProcessBuilder(cmd, "-batchmode", "-logFile", WorkingDir + "/unity.log", "--serverPort", String.valueOf(TCPPort));
            processBuilder.directory(new File(WorkingDir));
            
            Process proc = processBuilder.start();
            int exit = proc.waitFor();
            trace("Unity process terminated with code: " + exit);
         }
         catch(IOException ex)
            {
            String cmd = WorkingDir + "/" + UnityExe;
            trace("Check CMD: " + cmd);
                trace("Error launching Unity executable: " + ex);
                ex.printStackTrace();
            }
            catch (Exception ex)
            {
                trace("Unexpected exceptions: " + ex);
            }
         finally
         {
            ((OverlordsZoneExtension) getParentZone().getExtension()).GetPortManager().ReleasePort(TCPPort);
         }
      }
   }
   
   @Override
   public void init()
   {
      trace("UnityLauncher Started-------------------------------------------");
      trace("Unity Working Dir: " + WorkingDir);
      OverlordsZoneExtension ext = (OverlordsZoneExtension)getParentZone().getExtension();
      int tcpPort = ext.GetPortManager().GetAvailablePort();
      
      this.LauncherThread = new Thread(new UnityRunner(tcpPort), "unity-worker:" + tcpPort);
      this.LauncherThread.start();      
   }
   
   @Override
   public void destroy()
   {
      super.destroy();
      
      if(this.LauncherThread.isAlive())
         this.LauncherThread.interrupt();
      
      trace("UnityLauncher Stopped");
   }
   


Please let me know if you need anything else to identify the problem. Thanks.
Attachments
image.png
(54.08 KiB) Not downloaded yet
User avatar
Lapo
Site Admin
Posts: 23007
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Some question for headless server!!

Postby Lapo » 21 Jul 2022, 09:52

Hi,
the error says that there's a permission issue, so I would go check that first. The file you want to launch must have the executable permissions set.

If not you can change it via terminal, like this:

Code: Select all

chmod +x somefile.exe

If the flag is already set check that the user running SmartFox can also run the .Net executable (i.e. it belongs to the same user or group at least)

Also, it should go without saying, the .Net runtime should be installed on your server.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
yuneeb90
Posts: 3
Joined: 18 Jul 2022, 07:27

Re: Some question for headless server!!

Postby yuneeb90 » 21 Jul 2022, 12:01

Hi,

Thank you for your reply, I just verified regarding permissions we are using unity on windows so while generating the build from editor we have selected x86_64 and check marked server build option. And since chmod +x is a linux command, the windows equivalent of it is to set the permission and we have given max permissions by going to the folder of the build, properties->Security and from there we have given full control to all the options there by editing the permissions. It is still giving the same error. We are using overcast for smart fox as the host using A.TB.micro machine.

Another thing I have tried is putting the unity server into __lib__ folder under extensions and it still gives the same permission error 13 inside the folder as well as outside of it.

Now since you have mentioned the linux command do you mean that we should deploy the unity server build as a linux build under extensions folder or do you have a windows version of chmod +x command to check the permissions of our build folder?

And last question how can we install .Net runtime on our server, is there a way to install .Net runtime on smart fox admin panel or overcast or what do you mean here exactly?

I am in the middle of a project and the resources regarding running unity as a headless server on smart fox is very limited on the internet. So I would really appreciate if you can ellaborate a bit more on how to do these steps correctly as it is needed in our project, we are using all windows machines for development. Thanks.
User avatar
Lapo
Site Admin
Posts: 23007
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Some question for headless server!!

Postby Lapo » 21 Jul 2022, 16:30

Oh so this is running on Overcast?
In that case you won't be able to run .Net executables at all, I am sorry. The Overcast servers simply do not support it and there's no way to add it. You need a custom server that you can manage yourself and add all the necessary software.

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
yuneeb90
Posts: 3
Joined: 18 Jul 2022, 07:27

Re: Some question for headless server!!

Postby yuneeb90 » 21 Jul 2022, 17:43

Lapo wrote:Oh so this is running on Overcast?
In that case you won't be able to run .Net executables at all, I am sorry. The Overcast servers simply do not support it and there's no way to add it. You need a custom server that you can manage yourself and add all the necessary software.

Thanks


I got it, by custom server you mean something like AWS ec2 instance? if so do we have to deploy smart fox server along with unity headless server on it or we can keep smart fox server on overcast and just deploy unity headless server on ec2 instance separately and connect them both? What would you suggest in this case? or is there any other service (like jelastic or other cloud solutions) or method through which we can retain the functionality and admin panel of smart fox server but connect unity headless server along with it on a per dynamic room basis like once a room is created dynamically we connect it to unity headless server that manages the game basically just like how it describes in the article.

I am sorry if I am asking too many questions but I am way too deep in the development of the project and its not possible to ditch smart fox for any other solution at this moment so what ever solution we have to figure out in order to run unity headless server it has to be along side smart fox server. So I would really appreciate your suggestions / solution so that we can move forward with our project. Our requirement is just like how its mentioned in the article.

Though just to clear up our requirements a bit more, we have a room with 2 players max and we have to run our game simulation on unity server due to its physics and path finding etc. So whenever a room is created and 2 players join the room we need to connect the game room to unity server to keep things synced up and make it secure / uncheatable etc.

Thanks.
User avatar
Lapo
Site Admin
Posts: 23007
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Some question for headless server!!

Postby Lapo » 22 Jul 2022, 09:45

yuneeb90 wrote:I got it, by custom server you mean something like AWS ec2 instance?

Yes any server that you manage directly so you can install any additional library/software you need.

if so do we have to deploy smart fox server along with unity headless server on it or we can keep smart fox server on overcast and just deploy unity headless server on ec2 instance separately and connect them both?

Yes, separating the SFS2X and Unity servers is actually a good idea, since the Unity server itself can be quite resource consuming. And you'll be running many headless instances, so the bare minimum would be a dedicated server for SFS2X and another one for running the headless Unity servers.

Using AWS would actually be good because if you run the Unity machine(s) in the same region used by Overcast, you would get low latency between the servers.

What would you suggest in this case? or is there any other service (like jelastic or other cloud solutions) or method through which we can retain the functionality and admin panel of smart fox server but connect unity headless server along with it on a per dynamic room basis like once a room is created dynamically we connect it to unity headless server that manages the game basically just like how it describes in the article.


As mentioned above keeping all servers in the same infrastructure is important to reduce the latency between them. I would recommend using the same cloud infrastructure for the whole project. So if you decide to keep the Overcast server use AWS in the same region you've chosen for your SFS2X instance.

If you choose another provider, then I'd say drop the Overcast server and install your own SFS2X on a machine from that provider (Jelastic, Azure, Linode, and a million more...)

We're pretty familiar with AWS because we're developing the Overcast Cloud with their tools and we like it. It's extremely flexible, sometimes overwhelmingly so. But we don't have specific recommendations on this.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 81 guests