//[RPC] void RPC_S2C_CreateNewServer(uLink.BitStream stream, uLink.NetworkP2PMessageInfo info) { try { string srvName = stream.Read <string>(); int srvMode = stream.Read <int>(); int srvPort = stream.Read <int>(); ConnectedServer cServer = connectedServers.Find(it => it.peer == info.sender); if (cServer == null) { ConnectedServer server = new ConnectedServer(srvName, srvMode, info.sender); connectedServers.Add(server); } LoadServer.AddServer(srvName, srvMode); if (null != OnServerRegisteredEvent) { OnServerRegisteredEvent(srvName, srvMode, srvPort); } } catch (Exception e) { if (LogFilter.logFatal) { Debug.LogErrorFormat("RPC_S2C_CreateNewServer error\r\n{0}\r\n{1}", e.Message, e.StackTrace); } } }
public void UnHideServerIP() { if (ConnectedServer.GetServerByUIN(NecessaryBaseChannelObject.UIN) != null) { ConnectedServer.GetServerByUIN(NecessaryBaseChannelObject.UIN).HideIP = false; } }
public void CloseConnection() { lock (clientLocker) { if (ClientConnected) { try { ClientWriter.WriteLine("-- " + Properties.strings.cya); ClientWriter.Flush(); } catch (IOException) { } ConnectedServer.WriteToAllClients("-- " + string.Format(Properties.strings.disconnectedClient, Name + "@" + ClientIep.Address, ClientIep.Port)); ClientWriter.Close(); ClientReader.Close(); ClientStream.Close(); ClientSocket.Close(); ClientConnected = false; ConnectedServer.connectedClients.Remove(this); } } ConnectedServer.RefreshInfo(); }
private void ReceiveMessageCallback(IAsyncResult Result) { ConnectedSocket.EndReceive(Result); WebSocketFrame CurrentFrame = new WebSocketFrame(ReceiveBuffer, ConnectedSocket); CurrentFrame.ReceivePayload(ConnectedSocket); if (CurrentFrame.PayloadOpCode == WebSocketOpCode.ConnectionCloseFrame) { ConnectedServer.DisconnectUser(this); return; } if (CurrentPacket == null) { CurrentPacket = new WebSocketPacket(CurrentFrame); } CurrentPacket.AddFrame(CurrentFrame); if (CurrentFrame.HeaderFrame.FinalPacket) { ConnectedServer.Receive(this, CurrentPacket.Payload, CurrentPacket.PayloadOpCode); CurrentPacket = null; GC.Collect(); } Array.Clear(ReceiveBuffer, 0, ReceiveBuffer.Length); ConnectedSocket.BeginReceive(ReceiveBuffer, 0, 2, SocketFlags.None, ReceiveMessageCallback, null); }
public void AddBot(float difficulty, string botName) { if (IsSinglePlayerMode) { if (string.IsNullOrWhiteSpace(botName)) { var rand = new Random(); var randomLineNumber = rand.Next(0, BotNames.Length - 1); botName = BotNames[randomLineNumber].Trim(); botName = char.ToUpper(botName[0]) + botName.Substring(1); if (botName.Length > 7) { botName = botName.Remove(7); } } if (ConnectedServer.PlayersCount <= ConnectedServer.SupportedPlayerCount) { var newPlayer = new Player(botName, playersID.Dequeue(), false, true, true); newPlayer.AddEventCollectionChanged(); newPlayer.OnCardAddedToHand += PlayerAddedCard; newPlayer.OnCardRemovedFromHand += PlayerRemovedCard; newPlayer.SetBotPlayer(this, newPlayer, difficulty); ConnectedServer.AddPlayer(newPlayer); OnBotAdded.Invoke(newPlayer); } else { MessageBox.Show("Failed to add bot, lobby full"); } } }
/// <summary> /// Used by clients to send data over an established connection in a connection-oriented protocol /// </summary> /// <param name="message">Message data</param> /// <param name="receiver">Connected receiver of message</param> /// <returns>A status indicating result of the operation</returns> public IStatus <string> Stream(byte[] message, INetworkNode receiver) { Socket recipient = null; if (ConnectedServer != null && ConnectedServer.GetHashCode() == receiver.GetHashCode())//check if its server { recipient = ConnectedServer.Socket; } else { foreach (var client in ConnectedClients.Keys) { if (client.GetHashCode() == receiver.GetHashCode()) { recipient = client.Socket; } } } IStatus <string> status = Util.Container.CreateInstance <IStatus <string> >(); if (recipient == null) { status.IsSuccess = false; status.StatusMessage = "No matching server or client node found in the list of connected nodes"; } else { recipient.Send(message); status.IsSuccess = true; status.StatusMessage = "Message Sent"; } return(status); }
static void CloseServer(string serverName, int sceneMode) { ConnectedServer server = connectedServers.Find(it => it.serverName.Equals(serverName) && it.sceneMode == (Pathea.PeGameMgr.ESceneMode)sceneMode); if (server != null) { P2PRPC("RPC_C2S_CloseServer", server.peer); } }
public void RemoveBot(Player player) { if (IsConnected && IsSinglePlayerMode) { ConnectedServer.RemovePlayer(player.ID); OnBotRemoved.Invoke(player.ID); playersID.Enqueue(player.ID); } }
private bool MessageReceived(byte[] data) { string receiveData = string.Empty; if (_key == null) { receiveData = Encoding.UTF8.GetString(data); } else { receiveData = Encoding.UTF8.GetString(ExtensionMethods.Decrypt(Convert.ToBase64String(data), _key)); } var message = JsonConvert.DeserializeObject <MessageModel>(receiveData); if (message == null) { return(true); } LocalInfo info; info.ipAddress = _client.LocalAddress; info.localEndPoint = Convert.ToInt32(_client.LocalPort); info.pcName = Environment.MachineName; if (message.Type == Type.AcceptLogon) { _isServerConnected = true; ConnectedServer?.Invoke(info); } if (message.Type == Type.Disconnect) { _client.Dispose(); _client = null; _flagOfThreadsBreak = true; _isServerConnected = false; DisconnectedServer?.Invoke("Connection disconnected by Server"); } if (message.Type == Type.Blocked) { _client.Dispose(); _client = null; _isServerConnected = false; DisconnectedServer?.Invoke("It does not connect to the server because it is a blocked IP."); } if (message.Type == Type.Message) { ReceivedMessageFromServer?.Invoke(message.Message); } return(true); }
private void HandleRemoveAllBots() { var countPlayers = lobbyReader.ReadInt32(); for (var i = 0; i < countPlayers; i++) { var playerID = lobbyReader.ReadInt32(); ConnectedServer.RemovePlayer(playerID); OnPlayerDisconnectedLobby.Invoke(playerID); } }
private void HandlePlayerConnected() { var player = ConnectedServer.ReadDataPlayer(lobbyReader); if (!Chat.MutedPlayers.ContainsKey(player.IPAddress)) { Chat.MutedPlayers.Add(player.IPAddress, false); } OnPlayerConnected.Invoke(player); }
public void SendServerDisconnectionEvent(ConnectedServer disconnectedServer_obj) { MemoryStream memoryStream_DataToSend = new MemoryStream(); CommonMethods.WriteIntToStream(memoryStream_DataToSend, 10); CommonMethods.WriteUInt64ToStream(memoryStream_DataToSend, disconnectedServer_obj.UIN); NecessaryBaseChannelObject.SendData(memoryStream_DataToSend.ToArray(), SentDataType.ApplicationData); memoryStream_DataToSend.Close(); }
void StartUIServerCtrl() { mServerCtrl.mList.mItems.Clear(); foreach (MyServer ms in LoadServer.ServerList) { ConnectedServer cServer = P2PManager.GetServer(ms.gameName, ms.gameMode); if (cServer != null) { mServerCtrl.mList.AddItem(ms.ToServerDataItem(), Color.green); } else { mServerCtrl.mList.AddItem(ms.ToServerDataItem()); } } mServerCtrl.mList.UpdateList(); }
public void CloseServerConnection() { try { MemoryStream memoryStream_ReceivedData = new MemoryStream(byteArray_ReceivedData, 4, byteArray_ReceivedData.Length - 4); ulong ulong_ServerUIN = CommonMethods.ReadUInt64FromStream(memoryStream_ReceivedData); if (ConnectedServer.IsNecesseryUINConnected((ulong)ulong_ServerUIN) == true) { ConnectedServer.GetServerByUIN(ulong_ServerUIN).Disconnect(7); } } catch { } }
public void uLink_OnPeerDisconnected(uLink.NetworkPeer peer) { ConnectedServer server = connectedServers.Find(it => it.peer == peer); if (server != null) { connectedServers.Remove(server); if (null != OnServerDisconnectedEvent) { OnServerDisconnectedEvent(server.serverName, (int)server.sceneMode); } if (LogFilter.logDebug) { Debug.LogFormat("{0} closed", server.serverName); } } }
void StartServer() { if (null == mServerCtrl || null == mServerCtrl.mList) { return; } if (mServerCtrl.mList.mSelectedIndex == -1) { return; } int index = mServerCtrl.mList.mSelectedIndex; string serverName = mServerCtrl.mList.mItems[index].mData[0]; int sceneMode = (int)MyServer.AdventureOrBuild(mServerCtrl.mList.mItems[index].mData[2]); ConnectedServer cServer = P2PManager.GetServer(serverName, sceneMode); if (cServer != null) { Debug.Log("Server is running!"); MessageBox_N.ShowOkBox(PELocalization.GetString(8000497)); return; } MyServer ms = LoadServer.GetServer(serverName, sceneMode); if (null == ms) { return; } mServerCtrl.GetMyServerInfo(ms); if (ms.gameMode == (int)Pathea.PeGameMgr.ESceneMode.Custom) { Pathea.PeGameMgr.mapUID = ms.uid; string dataPath = Path.Combine(GameConfig.CustomDataDir, ms.mapName); Pathea.CustomGameData.Mgr.Instance.GetCustomData(Pathea.PeGameMgr.mapUID, dataPath); } MyServerManager.StartMyServer(ms); }
private void HandlePlayerDisconnected() { var playerID = lobbyReader.ReadInt32(); var isLeave = lobbyReader.ReadBoolean(); var isBot = lobbyReader.ReadBoolean(); Chat.MutedPlayers.Remove(ConnectedServer.Players[playerID].IPAddress); if (ConnectedServer.State == ServerState.InGame) { ConnectedServer.Players[playerID].IsBot = isBot; OnPlayerDisconnectedGame.Invoke(playerID, isLeave, isBot); OnPlayerKicked.Invoke(); } else { ConnectedServer.RemovePlayer(playerID); OnPlayerDisconnectedLobby.Invoke(playerID); } }
public void InitInterconnectionProcess() //called by server { try { MemoryStream memoryStream_ReceivedData = new MemoryStream(byteArray_ReceivedData, 4, byteArray_ReceivedData.Length - 4); ulong ulong_ClientUIN = CommonMethods.ReadUInt64FromStream(memoryStream_ReceivedData); if (ConnectedClient.IsNecesseryUINConnected((ulong)ulong_ClientUIN) == true) { NetworkAction.InterConnectedObjects interConnectedObject_obj = new NetworkAction.InterConnectedObjects(); interConnectedObject_obj.AppliedServerChannel = ConnectedServer.GetServerByUIN(this.NecessaryBaseChannelObject.UIN).GetNecesseryAppliedServerChannelByClientUIN(ulong_ClientUIN); interConnectedObject_obj.AppliedClientChannel = ConnectedClient.GetClientByUIN(ulong_ClientUIN).GetNecesseryAppliedClientChannelByServerUIN(this.NecessaryBaseChannelObject.UIN); interConnectedObject_obj.AppliedClientChannel.InterconnectedObject = interConnectedObject_obj.AppliedServerChannel.ChannelOwner; // если клиент ждал сервера то этот объект InterconnectedObject назначается только здесь! new Thread(new ParameterizedThreadStart(new NetworkAction().ClientInterConnectionProcessReceiveCycle)).Start(interConnectedObject_obj); new Thread(new ParameterizedThreadStart(new NetworkAction().ServerInterConnectionProcessReceiveCycle)).Start(interConnectedObject_obj); CommonNetworkEvents.NewInterConnectionEvent(interConnectedObject_obj.AppliedServerChannel, interConnectedObject_obj.AppliedClientChannel); SendNewInterConnectionEventNotify(interConnectedObject_obj.AppliedServerChannel.ChannelOwner.SystemChannel.BaseChannel, interConnectedObject_obj.AppliedServerChannel.ChannelOwner.UIN); SendNewInterConnectionEventNotify(interConnectedObject_obj.AppliedClientChannel.ChannelOwner.SystemChannel.BaseChannel, interConnectedObject_obj.AppliedClientChannel.ChannelOwner.UIN); } memoryStream_ReceivedData.Close(); } catch { } finally { } }
public void ConnectHost() { lobbyWriter.Write((byte)NetMessageType.ConnectionApproval); ConnectedServer.WriteDataPlayer(lobbyWriter, PlayerUntill); }
private void ClientListening() { string message; lock (clientLocker) { if (ClientConnected) { try { ClientWriter.WriteLine("-- " + Properties.strings.motd + ": " + ConnectedServer.WelcomeMessage); ClientWriter.Flush(); ClientWriter.Write("-- " + Properties.strings.enterNickname + ": "); ClientWriter.Flush(); } catch (IOException) { } } } try { do { string name = ClientReader.ReadLine(); if (string.IsNullOrWhiteSpace(name)) { lock (clientLocker) { if (ClientConnected) { ClientWriter.Write("-- " + Properties.strings.enterNickname + ": "); ClientWriter.Flush(); } } } else { Name = name; } } while (string.IsNullOrWhiteSpace(name)); } catch (IOException) { } ConnectedServer.RefreshInfo(); ConnectedServer.WriteToAllClients("-- " + string.Format(Properties.strings.connectedWithClient, Name + "@" + ClientIep.Address, ClientIep.Port)); lock (clientLocker) { if (ClientConnected) { try { ClientWriter.WriteLine("-- " + Properties.strings.commands); ClientWriter.Flush(); } catch (IOException) { } } } while (ClientConnected) { try { message = ClientReader.ReadLine(); if (message != null) { //Handles available commands if (!string.IsNullOrWhiteSpace(message)) { if (message[0] == '#') { if (message == Properties.strings.exit) { lock (clientLocker) if (ClientConnected) { CloseConnection(); } } else if (message == Properties.strings.list) { lock (clientLocker) if (ClientConnected) { ConnectedServer.List(this); } } else if (message == Properties.strings.cmd) { lock (clientLocker) { if (ClientConnected) { ClientWriter.WriteLine("-- " + Properties.strings.availableCommands + ": "); ClientWriter.Flush(); ClientWriter.WriteLine("\t-- " + Properties.strings.cmd + ": " + Properties.strings.cmdInfo); ClientWriter.Flush(); ClientWriter.WriteLine("\t-- " + Properties.strings.exit + ": " + Properties.strings.exitInfo); ClientWriter.Flush(); ClientWriter.WriteLine("\t-- " + Properties.strings.list + ": " + Properties.strings.listInfo); ClientWriter.Flush(); } } } else { lock (clientLocker) { if (ClientConnected) { ClientWriter.WriteLine("-- " + Properties.strings.unknownCmd); ClientWriter.Flush(); } } } } else { ConnectedServer.WriteToAllClients("|| " + Name + "@" + ClientIep.Address + " >> " + message); } } else { lock (clientLocker) { if (ClientConnected) { ClientWriter.WriteLine("-- " + Properties.strings.enterMessage); ClientWriter.Flush(); } } } } else { CloseConnection(); } } catch (IOException) { break; } } CloseConnection(); }