public void AddClientConnection(ClientConnection cc) { _context.Send(new SendOrPostCallback(delegate(object state) { _clientConnections.Add(cc); cc.ConnectionClosed += new EventHandler<EventArgs>(cc_ConnectionClosed); cc.DataReceived += new EventHandler<EventArgs>(cc_DataReceived); cc.AuthentificationFinished += new EventHandler<EventArgs>(cc_AuthentificationFinished); cc.Start(); }), null); }
private void TcpListenerMethod() { TcpListener _TcpListener = new TcpListener(IPAddress.Any, 8542); _TcpListener.Start(); while (_doaccept) { TcpClient tcpClient = _TcpListener.AcceptTcpClient(); bool allowed = false; lock (this) { if (_doaccept) { string ip = tcpClient.Client.RemoteEndPoint.ToString(); ip = ip.Substring(0, ip.IndexOf(':')); //todo: check connection requests per IP allowed = (_clientHandler.GetIPConnectionsCount(ip)<10); if (allowed) { ClientConnection cc = new ClientConnection(tcpClient); cc.IP = ip; _clientHandler.AddClientConnection(cc); } } } if (!allowed) { try { tcpClient.Close(); } catch { } } } }
private void sendMessageToClients(ClientConnection[] clients, ChatMessage msg) { byte[] data = msg.ChatMessageData; foreach (var a in clients) { byte[] b = new byte[data.Length]; data.CopyTo(b, 0); a.SendData(b); } }
private void updateAvailableRooms(ClientConnection cc) { ClientConnection[] clients; if (cc == null) { clients = (from a in _clientConnections where a.AuthentificationChecked && !a.IsClosed && !string.IsNullOrEmpty(a.Username) select a).ToArray(); } else { clients = new ClientConnection[] { cc }; } ChatMessage msg = new ChatMessage(); msg.ID = ""; msg.Room = ""; msg.Name = "rooms"; msg.Parameters.Add(string.Format("name{0}", 0), "Lobby"); for (int i = 0; i < _availableRooms.Count; i++) { msg.Parameters.Add(string.Format("name{0}", i+1), _availableRooms[i]); } sendMessageToClients(clients, msg); }
void cc_DataReceived(object sender, EventArgs e) { _context.Post(new SendOrPostCallback(delegate(object state) { ClientConnection cc = sender as ClientConnection; if (_clientConnections.Contains(cc)) { try { byte[] data = cc.ReadData(); while (data != null && !cc.IsClosed) { //parse data ChatMessage msg = ChatMessage.Parse(data); if (msg != null) { //handle data if (msg.Name == "signon" && !cc.AuthentificationChecked) { //if there is a client with the same ID, then it is a reconnect var ccid = (from a in _clientConnections where a.ID == msg.ID select a).FirstOrDefault(); if (ccid != null) { ccid.Close(); _clientConnections.Remove(ccid); } cc.Room = msg.Room; cc.ID = msg.ID; cc.Token = msg.Parameters["token"]; cc.Authenticate(); } else if (cc.AuthentificationChecked && !string.IsNullOrEmpty(cc.Username)) { //general chat message, broadcast it if (msg.Name == "txt") { ClientConnection[] clients = GetClientsInRoom(cc.Room); ChatMessage bmsg = new ChatMessage(); bmsg.ID = cc.ID; bmsg.Room = cc.Room; bmsg.Name = "txt"; bmsg.Parameters.Add("msg", msg.Parameters["msg"]); bmsg.Parameters.Add("color", msg.Parameters["color"]); sendMessageToClients(clients, bmsg); } else if (msg.Name == "follow") { cc.ActiveGeocache = msg.Parameters["cache"]; cc.CanBeFollowed = bool.Parse(msg.Parameters["canfollow"]); ClientConnection[] clients = GetClientsInRoom(cc.Room); ChatMessage bmsg = new ChatMessage(); bmsg.ID = cc.ID; bmsg.Room = cc.Room; bmsg.Name = "follow"; bmsg.Parameters.Add("canfollow", msg.Parameters["canfollow"]); bmsg.Parameters.Add("cache", msg.Parameters["cache"]); if (msg.Parameters.ContainsKey("selected")) { string s = msg.Parameters["selected"]; if (string.IsNullOrEmpty(s)) { s = ""; cc.SelectionCount = -1; } else { int cnt; if (int.TryParse(s, out cnt)) { cc.SelectionCount = cnt; } else { s = ""; cc.SelectionCount = -1; } } bmsg.Parameters.Add("selected", s); } else { cc.SelectionCount = -1; bmsg.Parameters.Add("selected", ""); } sendMessageToClients(clients, bmsg); } else if (msg.Name == "room") { if (cc.Room != msg.Room) { //leave old string prevRoom = cc.Room; cc.Room = msg.Room; updateActiveClientsForRoom(prevRoom); if (availableRoomsChanged()) { updateAvailableRooms(null); } //enter new updateActiveClientsForRoom(cc.Room); } } else if (msg.Name == "byebye") { cc.Close(); break; } else if (msg.Name == "reqsel") { ClientConnection[] clients = GetClientsInRoom(cc.Room); ClientConnection destClient = (from c in clients where c.ID == msg.Parameters["clientid"] select c).FirstOrDefault(); if (destClient != null) { ChatMessage bmsg = new ChatMessage(); bmsg.ID = cc.ID; bmsg.Room = cc.Room; bmsg.Name = "reqsel"; bmsg.Parameters.Add("reqid", msg.Parameters["reqid"]); bmsg.Parameters.Add("clientid", msg.Parameters["clientid"]); sendMessageToClients(new ClientConnection[] { destClient }, bmsg); } else { ChatMessage bmsg = new ChatMessage(); bmsg.ID = cc.ID; bmsg.Room = cc.Room; bmsg.Name = "reqselresp"; bmsg.Parameters.Add("reqid", msg.Parameters["reqid"]); bmsg.Parameters.Add("clientid", msg.Parameters["clientid"]); bmsg.Parameters.Add("selection", ""); sendMessageToClients(new ClientConnection[] { cc }, bmsg); } } else if (msg.Name == "reqselresp") { ClientConnection[] clients = GetClientsInRoom(cc.Room); ClientConnection destClient = (from c in clients where c.ID == msg.Parameters["clientid"] select c).FirstOrDefault(); if (destClient != null) { ChatMessage bmsg = new ChatMessage(); bmsg.ID = cc.ID; bmsg.Room = cc.Room; bmsg.Name = "reqselresp"; bmsg.Parameters.Add("reqid", msg.Parameters["reqid"]); bmsg.Parameters.Add("clientid", msg.Parameters["clientid"]); bmsg.Parameters.Add("selection", msg.Parameters["selection"]); sendMessageToClients(new ClientConnection[] { destClient }, bmsg); } } } } data = cc.ReadData(); } } catch { cc.Close(); } } }), null); }