public bool AddPlayer(ServerPlayer player) { if (this.HasPlayer(player)) { return true; } if (this.IsFull()) { return false; } for (int i = 0; i < Settings.MaxPartySize; i++) if (_members[i] == null) { _members[i] = player; ServerMessage msg = new ServerMessage(Protocol.netSquadJoin); byte n = (byte)i; msg.WriteByte(n); this.Broadcast(msg, player.accountID); player.SendMessage(msg, player.accountID); msg = null; for (int j = 0; j < Settings.MaxPartySize; j++) if (_members[j]!= null && j!=i) { msg = new ServerMessage(Protocol.netSquadJoin); msg.WriteByte(n); player.SendMessage(msg, _members[i].accountID); } break; } return true; }
public void AddPlayer(ServerPlayer player) { if (player == null) return; foreach (ServerPlayer other in _players) if (other.accountID == player.accountID) { return; } foreach (ServerPlayer other in _players) { ServerMessage msg = new ServerMessage(Protocol.netPlayerAdd); msg.WriteInteger(this._ID); msg.WriteString(this._region.Name); msg.WriteInteger(other.TX); msg.WriteInteger(other.TY); msg.WriteByte(other.level); player.SendMessage(msg, other.accountID); msg = null; msg = other.GetKeysMessage(); if (msg!=null) { player.SendMessage(msg, other.accountID); msg = null; } } _players.Add(player); }
public virtual void SendTo(ServerPlayer player) { ServerMessage msg; if (player == null) return; msg = new ServerMessage(Protocol.netBattleCommand); this.Write(msg); player.SendMessage(msg, owner.accountID); }
private void HandleClientComm(object obj) { TcpClient tcpClient = (TcpClient)obj; NetworkStream clientStream = tcpClient.GetStream(); ServerPlayer client = this.AllocateClient(tcpClient); if (client == null) { client = new ServerPlayer(tcpClient); ServerMessage dest = new ServerMessage(Protocol.netServerError); dest.WriteWord(Protocol.errServerFull); dest.WriteString(null); client.SendMessage(dest, 0); return; } playersOnline++; int bytesRead; byte[] buffer = new byte[1024]; while (!client.isRemoved() && !this.shutdown) { ServerQueuedMessage temp = new ServerQueuedMessage(); temp.client = client; bytesRead = 0; try { if (client.outdatedVersion) { ServerMessage dest = new ServerMessage(Protocol.netServerError); dest.WriteWord(Protocol.errInvalidVersion); dest.WriteString(null); client.SendMessage(dest, 0); } if (client.accountID>0) Utils.Log("Waiting for client " + client.accountID + " packets..", 2); ServerMessage msg = new ServerMessage(clientStream, buffer, ref bytesRead); } catch (Exception e) { //a socket error has occured Utils.Log("Socket error in client with id " + client.accountID, -1); break; } if (bytesRead == 0) { Utils.Log("Client with id " + client.accountID + " disconnected", 0); //the client has disconnected from the server break; } //protocol.HandleMessage(msg.header, msg.client, msg.data); messagequeue.Enqueue(temp); } OnClientRemove(client); client.removed = true; this.playersOnline--; tcpClient.Close(); tcpClient = null; System.GC.Collect(); }