/// <summary> /// 设置tcp服务端通信 /// </summary> public bool Setup(IServerConfig config) { try { if (_sersocket != null) { _sersocket.Close(); } _config = config; //初始化socket _sersocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //绑定端口 IPEndPoint iep = new IPEndPoint(_config.IP, _config.Port); try { _sersocket.Bind(iep); } catch (Exception ex) { State = ServerStateEnum.NotInitialized; return(false); } State = ServerStateEnum.NotStarted; return(true); } catch (Exception ex) { State = ServerStateEnum.NotInitialized; return(false); } }
private string HandlePlayerPieces(string[] messageParts) { string result = ""; string playerNum = ""; if (messageParts[0] == "Pieces") { if (messageParts.GetPiecesOrNull(1) == null) { result = "No player number provided"; } else if (messageParts.GetPiecesOrNull(2) == null) { result = "No player pieces sent"; } else { string num = messageParts[1]; if (num != "1" && num != "2") { result = "Player number not valid"; return(result); } string pieces = messageParts[2]; playerNum = num; // ToDo: Validate player has sent the correct amount of pieces if (_player1Board == null && playerNum == "1") { _player1Board = new PlayerBoard(pieces); result = "OK"; } else if (_player1Board != null && playerNum == "1") { result = "Pieces already recieved"; } else if (_player2Board == null && playerNum == "2") { _player2Board = new PlayerBoard(pieces); result = "OK"; } else if (_player2Board != null && playerNum == "2") { result = "Pieces already recieved"; } if (_player1Board != null && _player2Board != null) { GameState = ServerStateEnum.Player1Turn; } } } return(result); }
public void Stop() { if (State != ServerStateEnum.Running) { return; } try { //先断开服务端本身的socket通信 //否则客户端会提示,服务器强制断开远程连接 if (_sersocket != null) { if (_sersocket.Connected) { _sersocket.Shutdown(SocketShutdown.Both); } } CloseAllClient(); _sessionTable = null; _threadlist = null; if (Config.IsRecHeartbeat) { this.daemonthread.Close(); } State = ServerStateEnum.NotStarted; } catch (Exception ex) { State = ServerStateEnum.NotStarted; } }
private void OnStart() { try { _sersocket.Listen(_config.ListenBacklog); State = ServerStateEnum.Running; while (true) { Socket client = _sersocket.Accept(); AppSession newSession = new AppSession(client); newSession._lastactivedatetime = DateTime.Now; string aSessionID = Guid.NewGuid().ToString(); newSession.ID = aSessionID; lock (LockObj) { _sessionTable.Add(aSessionID, newSession); } Thread newthead = new Thread(new ParameterizedThreadStart(ServiceClient)); Thread newThreadSend = new Thread(new ParameterizedThreadStart(ServiceClientForHeart)); newThreadSend.Name = newthead.Name = aSessionID; newthead.IsBackground = true; newThreadSend.IsBackground = true; newthead.Start(aSessionID); newThreadSend.Start(aSessionID); _threadlist.Add(newthead); _threadlist.Add(newThreadSend); } } catch (Exception ex) { State = ServerStateEnum.NotStarted; } }
/// <summary> /// 启动服务 /// </summary> public bool Start() { try { //服务器配置和服务器状态判断 if (_config == null || State == ServerStateEnum.NotInitialized) { State = ServerStateEnum.NotInitialized; } _sessionTable = new Dictionary <string, AppSession>(); //客户端会话连接Session _threadlist = new List <Thread>(); //线程池 processor = new Thread(new ThreadStart(OnStart)); //新建一个处理线程 processor.IsBackground = true; //线程启动 processor.Start(); //线程开始准备就绪 //客户端心跳处理判断 daemonthread = new DaemonThread(this); State = ServerStateEnum.Running; return(true); } catch (Exception ex) { State = ServerStateEnum.NotStarted; return(false); } }
public bool ValidTransition(ServerStateEnum newState) { if (ValidNewStates.Contains(newState)) { return(true); } else { return(false); } }
public ServerState(ServerStateEnum state, bool isDegradedState, bool ignoreMirrorStateCheck, List <ServerStateEnum> validNewStates) { _state = state; if (state.ToString().StartsWith("PRIMARY")) { _isPrimaryRole = true; } else { _isPrimaryRole = false; } _isDegradedState = isDegradedState; _ignoreMirrorStateCheck = ignoreMirrorStateCheck; _validNewStates = validNewStates; }
private string HandlePlayerConnects(string[] messageParts) { string result = ""; if (messageParts[0] == "Connect") { if (GameState == ServerStateEnum.Player1Connect) { // Signal that they have connected as player 1 result = "OK|1"; GameState = ServerStateEnum.Player2Connect; } else if (GameState == ServerStateEnum.Player2Connect) { // Signal that they have connected as player 2 result = "OK|2"; GameState = ServerStateEnum.PlayerPieces; } } return(result); }
public BattleshipsServerState() { GameState = ServerStateEnum.Player1Connect; }
private string HandlePlayerTurn(string[] messageParts) { string result = ""; // Two commands - Recieve and Turn if (messageParts[0] == "Turn" && _lastPlay == null) { if (_state == ServerStateEnum.Player1Turn && messageParts[1] == "1") { // Apply hit to board _lastPlay = messageParts[2]; bool hit = _player2Board.ApplyHit(messageParts[2]); _lastPlayHit = hit; if (hit) { return("HIT"); } else { return("MISS"); } } else if (_state == ServerStateEnum.Player2Turn && messageParts[1] == "2") { // Apply hit to board _lastPlay = messageParts[2]; bool hit = _player1Board.ApplyHit(messageParts[2]); _lastPlayHit = hit; if (hit) { return("HIT"); } else { return("MISS"); } } else { result = "Not Your Turn"; } } else if (messageParts[0] == "Turn" && _lastPlay != null) { result = "Turn Already Taken"; } else if (messageParts[0] == "Recieve" && _lastPlay != null) { if ((messageParts[1] == "1" && GameState == ServerStateEnum.Player2Turn) || messageParts[1] == "2" && GameState == ServerStateEnum.Player1Turn) { result = "OK|" + _lastPlay; _lastPlay = null; // This triggers the move to the next player // ToDO: Check if the game is over maybe? if (GameState == ServerStateEnum.Player1Turn && _lastPlayHit == false) { GameState = ServerStateEnum.Player2Turn; } else if (GameState == ServerStateEnum.Player2Turn && _lastPlayHit == false) { GameState = ServerStateEnum.Player1Turn; } } else { result = "Not your turn to recieve"; } } else if (messageParts[0] == "Recieve" && _lastPlay == null) { if ((messageParts[1] == "1" && GameState == ServerStateEnum.Player2Turn) || messageParts[1] == "2" && GameState == ServerStateEnum.Player1Turn) { result = "Pending"; } else { // Not ready result = "NR"; } } return(result); }