codingOL

GFXRestServerCore.cs

Oct 7th, 2013
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.96 KB | None | 0 0
  1. using GameForestCore.Database;
  2. using GameForestCore.Services;
  3. using GameForestCore.Util;
  4.  
  5. using System;
  6. using System.ServiceModel;
  7. using System.ServiceModel.Description;
  8. using System.ServiceModel.Web;
  9. using System.Threading;
  10.  
  11. namespace GameForest
  12. {
  13.     public class GFXRestServerCore
  14.     {
  15.         private bool                isStarted               = false;
  16.  
  17.         private WebServiceHost      serviceHostUser;
  18.         private WebServiceHost      serviceHostGame;
  19.         private WebServiceHost      serviceHostLobi;
  20.         private WebServiceHost      serviceHostStat;
  21.  
  22.         public event EventHandler   OnServerRestStop;
  23.         public event EventHandler   OnServerRestStart;
  24.  
  25.         public bool                 IsStarted
  26.         {
  27.             get { return isStarted; }
  28.         }
  29.  
  30.         public                      GFXRestServerCore       ()
  31.         {
  32.             ServiceMetadataBehavior behavior = new ServiceMetadataBehavior
  33.             {
  34.                 HttpGetEnabled  = true,
  35.                 HttpsGetEnabled = true
  36.             };
  37.  
  38.             serviceHostUser = new WebServiceHost(new GFXUserService(), new Uri("http://localhost:1193/service/user"));
  39.             serviceHostGame = new WebServiceHost(new GFXGameService(), new Uri("http://localhost:1193/service/game"));
  40.             serviceHostLobi = new WebServiceHost(new GFXLobbyService(), new Uri("http://localhost:1193/service/lobby"));
  41.             serviceHostStat = new WebServiceHost(new GFXStatsService(), new Uri("http://localhost:1193/service/stats"));
  42.  
  43.             var httpBinding = new WebHttpBinding
  44.             {
  45.                 CrossDomainScriptAccessEnabled = true
  46.             };
  47.  
  48.             var uEndpoint = serviceHostUser.AddServiceEndpoint(typeof(GameForestCore.Services.IGFXUserService), httpBinding, "");
  49.             var gEndpoint = serviceHostGame.AddServiceEndpoint(typeof(GameForestCore.Services.IGFXGameService), httpBinding, "");
  50.             var lEndpoint = serviceHostLobi.AddServiceEndpoint(typeof(GameForestCore.Services.IGFXLobbyService), httpBinding, "");
  51.             var sEndpoint = serviceHostStat.AddServiceEndpoint(typeof(GameForestCore.Services.IGFXStatsService), httpBinding, "");
  52.  
  53.             uEndpoint.Behaviors.Add(new WebHttpBehavior());
  54.             gEndpoint.Behaviors.Add(new WebHttpBehavior());
  55.             lEndpoint.Behaviors.Add(new WebHttpBehavior());
  56.             sEndpoint.Behaviors.Add(new WebHttpBehavior());
  57.  
  58.             uEndpoint.Behaviors.Add(new BehaviorAttribute());
  59.             gEndpoint.Behaviors.Add(new BehaviorAttribute());
  60.             lEndpoint.Behaviors.Add(new BehaviorAttribute());
  61.             sEndpoint.Behaviors.Add(new BehaviorAttribute());
  62.  
  63.             serviceHostUser.Description.Behaviors.Add(behavior);
  64.             serviceHostGame.Description.Behaviors.Add(behavior);
  65.             serviceHostLobi.Description.Behaviors.Add(behavior);
  66.             serviceHostStat.Description.Behaviors.Add(behavior);
  67.         }
  68.  
  69.         public void                 Start                   ()
  70.         {
  71.             isStarted = true;
  72.  
  73.             var thread = new Thread(new ThreadStart(() =>
  74.             {
  75.                 serviceHostUser.Open();
  76.                 serviceHostGame.Open();
  77.                 serviceHostLobi.Open();
  78.                 serviceHostStat.Open();
  79.  
  80.                 if (OnServerRestStart != null)
  81.                     OnServerRestStart(this, EventArgs.Empty);
  82.  
  83.                 while (isStarted)
  84.                 {
  85.                     Thread.Sleep(100);
  86.                 }
  87.  
  88.                 serviceHostUser.Close();
  89.                 serviceHostGame.Close();
  90.                 serviceHostLobi.Close();
  91.                 serviceHostStat.Close();
  92.  
  93.                 if (OnServerRestStop != null)
  94.                     OnServerRestStop(this, EventArgs.Empty);
  95.             }));
  96.  
  97.             thread.Start();
  98.         }
  99.  
  100.         public void                 Stop                    ()
  101.         {
  102.             isStarted = false;
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment