public void TestCase()
 {
     var client = new ServiceNetClient(IPAddress.Parse("127.0.0.1"), 8668);
     //client.Subscribe();
     client.SendMessage(new Message());
     for(int i = 0; i < 6 && msg == null; i++)
         Thread.Sleep(100);
     client.Disconnect();
     Assert.That(msg, Is.Not.Null);
 }
Beispiel #2
0
        void listener_PlayerConnected(object sender, ServiceNetClient client, PlayerAccountData connectedPlayer)
        {
            if(++playerCounter < 2)
            return;

              if(InvokeRequired)
            Invoke(new Action<object, ServiceNetClient, PlayerAccountData>(listener_PlayerConnected), sender, client, connectedPlayer);
              else
              {
            ServerListener listener = (ServerListener)sender;
            if(listener.PlayerConnections != null)
            {
              listener.Dispose();
              clientStarter1.Start(null);
              clientStarter2.Start(null);

              PlayerInfo[] players = new PlayerInfo[] { serverPlayer, clientPlayer2, clientPlayer1 };
              DeckItem[] decks = new DeckItem[] { serverDeck, clientDeck2, clientDeck1 };
              Thread.Sleep(1000);
              StartGame(listener.PlayerConnections, players.ToList(), decks.ToList(),
            new string[] { string.Empty, string.Empty, string.Empty }, GameType.Host, false);
            }
              }
        }
 public ServerConnectionMessagesHandler(ServiceNetClient client)
 {
     this.client = client;
       client.MessageReceived += new EventHandler(client_MessageReceived);
 }
 protected virtual void OnPlayerDisconnected(ServiceNetClient client, PlayerInfo player)
 {
     if(PlayerDisconnected != null)
     PlayerDisconnected(this, client, player);
 }
 void netMessageService_ClientConnected(object sender, ServiceNetClient client)
 {
     using(var connectionMessagesHandler = new ServerConnectionMessagesHandler(client))
       {
     if(!WaitHandshake(connectionMessagesHandler))
     {
       if(Console != null)
     Console.WriteLine(MessageCategory.Warning, "Opponent handshake not received");
     }
     else
     {
       ConnectionResult result;
       string refuseReason;
       ValidatePlayer(connectionMessagesHandler, out result, out refuseReason);
       HandshakeResponse handshakeResponse = new HandshakeResponse()
       {
     Deck = new DeckDataContract(deck),
     PlayerInfo = Player,
     ConnectionResult = result,
       };
       Thread.Sleep(100);
       client.SendMessage(handshakeResponse);
       if(result != ConnectionResult.Accepted)
       {
     if(Console != null)
       Console.WriteLine(MessageCategory.Warning, string.Concat("Player ", connectionMessagesHandler.Handshake.PlayerInfo.NickName, " refused: ", refuseReason));
       }
       else
       {
     if(!WaitPlayerConfirm(connectionMessagesHandler))
     {
       if(Console != null)
         Console.WriteLine(MessageCategory.Warning, string.Concat("Player ", connectionMessagesHandler.Handshake.PlayerInfo.NickName, " hasn't confirmed the connection."));
     }
     else
     {
       client.Disconnected += new EventHandler(client_Disconnected);
       lock(syncObject)
         connections.Add(client, connectionMessagesHandler);
       PlayerAccountData connectedPlayer = new PlayerAccountData()
       {
         Info = connectionMessagesHandler.Handshake.PlayerInfo,
         Deck = new DeckItem(connectionMessagesHandler.Handshake.Deck),
         Password = connectionMessagesHandler.Handshake.Password
       };
       OnPlayerConnected(client, connectedPlayer);
     }
       }
     }
       }
 }
 protected virtual void OnPlayerConnected(ServiceNetClient client, PlayerAccountData connectedPlayer)
 {
     if(PlayerConnected != null)
     PlayerConnected(this, client, connectedPlayer);
 }
 void INetMessageService.Subscribe()
 {
     INetMessageServiceCallback channel = OperationContext.Current.GetCallbackChannel<INetMessageServiceCallback>();
       ServiceNetClient client = new ServiceNetClient(this, channel);
       OnClientConnected(client);
 }
 protected void OnClientConnected(ServiceNetClient client)
 {
     if(this.ClientConnected != null)
     this.ClientConnected(this, client);
 }
 public bool Connect(IPAddress address, int port, string password, GameStartMode startMode, DeckItem deck, 
     out string errorMessage, out ServerData opponentData)
 {
     bool confirmed = false;
       errorMessage = string.Empty;
       opponentData = null;
       ServiceNetClient client = new ServiceNetClient(address, port);
       client.Disconnected += new EventHandler(client_Disconnected);
       connectionMessagesHandler = new ClientConnectionMessagesHandler(client);
       try
       {
     client.Subscribe();
     Version clientVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
     HandshakeRequest handshake = new HandshakeRequest()
     {
       PlayerInfo = playerInfo,
       Deck = new DeckDataContract(deck),
       StartMode = startMode,
       GameDatabaseCRC = servicesProvider.GameDatabaseCrc,
       CardDatabaseCRC = servicesProvider.CardDatabaseCrc,
       ClientVersion = clientVersion.ToString(),
       Password = password,
     };
     Thread.Sleep(100);
     client.SendMessage(handshake);
     if(!WaitForHandshakeResponse())
       throw new Exception("Opponent handshake not received!");
     if(connectionMessagesHandler.Handshake.ConnectionResult != ConnectionResult.Accepted)
     {
       switch(connectionMessagesHandler.Handshake.ConnectionResult)
       {
     case ConnectionResult.VersionMismatch:
       errorMessage = servicesProvider.SystemStringsService.GetString("NEW_SERVER_GAME_DIALOG", "MSG_CONNECTIONREFUSED_VERSIONMISMATCH");
       break;
     case ConnectionResult.GameIsRunning:
       errorMessage = servicesProvider.SystemStringsService.GetString("NEW_SERVER_GAME_DIALOG", "MSG_CONNECTIONREFUSED_GAMEISRUNNING");
       break;
     case ConnectionResult.InvalidStartMode:
       errorMessage = servicesProvider.SystemStringsService.GetString("NEW_SERVER_GAME_DIALOG", "MSG_CONNECTIONREFUSED_INVALIDSTARTMODE");
       break;
     case ConnectionResult.NicknameDuplicated:
       errorMessage = servicesProvider.SystemStringsService.GetString("NEW_SERVER_GAME_DIALOG", "MSG_CONNECTIONREFUSED_NICKNAMEDUPLICATED");
       break;
     case ConnectionResult.GameDatabaseMismatch:
       errorMessage = servicesProvider.SystemStringsService.GetString("NEW_SERVER_GAME_DIALOG", "MSG_CONNECTIONREFUSED_GAMEDATAMISMATCH");
       break;
     case ConnectionResult.CardDatabaseMismatch:
       errorMessage = servicesProvider.SystemStringsService.GetString("NEW_SERVER_GAME_DIALOG", "MSG_CONNECTIONREFUSED_CARDDATAMISMATCH");
       break;
     case ConnectionResult.PasswordRequired:
       errorMessage = servicesProvider.SystemStringsService.GetString("NEW_SERVER_GAME_DIALOG", "MSG_CONNECTIONREFUSED_PASSWORDREQUIRED");
       break;
       }
     }
     else
     {
       Thread.Sleep(100);
       client.SendMessage(new ConfirmMessage());
       opponentData = new ServerData()
       {
     Channel = client,
     Player = connectionMessagesHandler.Handshake.PlayerInfo,
     Deck = new DeckItem(connectionMessagesHandler.Handshake.Deck)
       };
       confirmed = true;
     }
       }
       catch(Exception ex)
       {
     errorMessage = ex.Message;
       }
       finally
       {
     connectionMessagesHandler.Dispose();
       }
       return confirmed;
 }
 protected virtual void OnDisconnected(ServiceNetClient client)
 {
     if(Disconnected != null)
     Disconnected(this, client);
 }