public ServerClient(NetworkStream Stream, TcpClient Client) { stream = Stream; client = Client; logger = new Logger.Filelogger(); UserId = stream.GetHashCode(); }
public Client(NetworkStream Stream, TcpClient Client) { stream = Stream; client = Client; UserId = stream.GetHashCode(); this.displayName = UserId.ToString(); }
/// <summary> /// Handle newly connected client and spawn a thread. /// </summary> /// <param name="client">Connected TcpClient</param> private void HandleClient(object client) { TcpClient c = (TcpClient)client; clientList_.Add(c.GetHashCode(), c); NetworkStream clientStream = c.GetStream(); // Initialize message buffer for new client. currentClientMsg_.Add(clientStream.GetHashCode(), String.Empty); byte[] message = new byte[BLOCK_SIZE]; int bytesRead; while (true) { bytesRead = 0; try { bytesRead = clientStream.Read(message, 0, BLOCK_SIZE); } catch (System.Exception e) // Socket error { Debug.WriteLine("SocketAcceptor: client read exception: " + e.ToString()); break; } if (ClientHasDisconnected(bytesRead)) { break; } ///HandleDataReceived(Encoding.UTF8.GetString(message, 0, bytesRead), clientStream.GetHashCode()); } }
/** * \brief Обмен данными с клиентом, обработка входящих команд (регистрация, отправка сообщений и т.д.), обращение к базе данных, вывод информации о посещении чата * \param[in] o Клиентский сокет, с которым было установлено соединение */ static void Connection(object o) { TcpClient client = (TcpClient)o; string cmd = " ", nickname = "", ip = client.Client.RemoteEndPoint.ToString(), pchash = "", state = "/"; bool auth = false, first = true; NetworkStream stream = client.GetStream(); Stopwatch time1 = new Stopwatch(), time2 = new Stopwatch(); DateTime last = DateTime.MaxValue; int hash = stream.GetHashCode(); try { time2.Start(); int n; IO.SetKey(stream, true); while (cmd != "out") { if (client.Available > 0) { if (time1.IsRunning) { time1.Reset(); } cmd = IO.Read(stream, 3); switch (cmd) { case "reg": case "ath": if (Sign(stream, out nickname, out pchash, cmd) && !online.Contains(nickname)) { online.Add(nickname); IO.Write(stream, "Y", 1); auth = true; Console.WriteLine("{0} ({1}) has joined to the chat", nickname, ip); state = (string)SQLCommand("ban", pchash); if (state == "N") { IO.Write(stream, "N", 1); } else { IO.Write(stream, "Y", 1); goto case "ban"; } } else { IO.Write(stream, "N", 1); } break; case "msg": if (auth && time2.ElapsedMilliseconds > 1000) { state = (string)SQLCommand("ban", pchash); if (state == "N") { IO.Write(stream, "YN", 2); string text = IO.Read(stream, 256); for (int i = 0; i < 15; i++) { post[i] = post[i + 1]; } post[15] = new Post(DateTime.Now, nickname, text); SQLCommand("msg", post[15].date.ToString(), nickname, text); time2.Restart(); } else { IO.Write(stream, "YY", 2); goto case "ban"; } } else { IO.Write(stream, "NN", 2); } break; case "all": if (auth && (first || post[15].date != last)) { n = 16; IO.Write(stream, "Y", 1); if (!first) { for (int i = 15; i >= 0; i--) { if (post[i].date == last) { n = 15 - i; break; } } } else { first = false; } IO.Write(stream, ((char)n).ToString(), 1); for (int i = 16 - n; i < 16; i++) { IO.Write(stream, post[i].nickname + (post[i].nickname != " " ? ':' : ' ') + post[i].msg, 272); } last = post[15].date; } else { IO.Write(stream, "N", 1); } break; case "ban": if (auth) { IO.Write(stream, state.Split('/')[0], 19); IO.Write(stream, state.Split('/')[1], 256); cmd = "out"; } break; } } else { if (!time1.IsRunning) { time1.Start(); } else if (time1.Elapsed.Minutes == 1) { break; } Thread.Sleep(0); } } } catch (Exception error) { IO.error.Add(error); } finally { if (IO.list.ContainsKey(hash)) { IO.list.Remove(hash); } client.Close(); if (auth) { online.Remove(nickname); Console.WriteLine("{0} ({1}) has left the chat", nickname, ip); } } }
/** * Temporarily, debug purposes */ public void Log() { var client = this.client; Debug.Log(name + " C: " + client.GetHashCode() + "(" + client.Connected + ")" + " S: " + stream.GetHashCode() + "(" + stream.CanRead + "/" + stream.CanWrite + ")" + " So: " + client.Client.GetHashCode() + "(" + client.Client.Connected + ")"); }