public void SetupConnection() { _NetStream = _TcpClient.GetStream(); _Ssl = new SslStream(_NetStream, false); try { _Ssl.AuthenticateAsServer(_Program.cert); _br = new BinaryReader(_Ssl, Encoding.UTF8); _bw = new BinaryWriter(_Ssl, Encoding.UTF8); _bw.Write((int)TcpCommands.Hello); _bw.Flush(); int hello = _br.ReadInt32(); if (!_Program.ShuttingDown && hello == (int)TcpCommands.Hello) { if ((TcpCommands)_br.ReadInt32() == TcpCommands.Login) { LoginArgs args = _br.ReadObject <LoginArgs>(); lock ( _lock ) { if (args.IsBot) { // If this username is already logged in, report error if (_Program.Users.Keys.Where(u => u.Username == args.Username).Count() > 0) { _bw.Write((int)TcpCommands.Error); _bw.Write("A bot with this username is already logged in"); _bw.Flush(); return; } // Verify minimum version if (args.Version < Program.MinVersion) { _bw.Write((int)TcpCommands.Error); _bw.Write(string.Format("This HylandMedConfig.Bot version is not supported. Please upgrade to version {0}", Program.MinVersion)); _bw.Flush(); return; } _User = new ChatUser(args.Username, args.DisplayName, args.Nickname, args.ImageUrl, true, args.BotCreatorUserName); if (!string.IsNullOrEmpty(args.PreviousMood)) { _User.Mood = args.PreviousMood; } _User.SetClientVersion(args.Version); _Connection = new UserConnection(); _Program.Users.Add(_User, _Connection); _Program.Stats.GetStats(args.Username).LastLogin = DateTime.Now; _Program.Stats.Save(); } // Normal user else { if (args.Version < Program.MinVersion) { _bw.Write((int)TcpCommands.Error); _bw.Write(string.Format("This version is not supported. Please upgrade")); _bw.Flush(); ConsoleMessage("{0} tried to connect with version {1}", args.Username, args.Version); return; } foreach (var user in _Program.Users.Where(u => u.Key.Username == args.Username)) { if (user.Value.Client != null) { _bw.Write((int)TcpCommands.Error); _bw.Write("A user with this username is already logged in"); _bw.Flush(); return; } _User = user.Key; if (!string.IsNullOrEmpty(args.PreviousMood)) { _User.Mood = args.PreviousMood; } _User.SetClientVersion(args.Version); _Connection = user.Value; _Program.Stats.GetStats(args.Username).LastLogin = DateTime.Now; _Program.Stats.Save(); } if (_User == null) { _bw.Write((int)TcpCommands.Error); _bw.Write("This user does not exist"); _bw.Flush(); return; } } } } else { _bw.Write((int)TcpCommands.Error); _bw.Write("Invalid handshake"); _bw.Flush(); return; } // Login is a success, send down the user list and the logged on commend to all users lock (_lock) { _Connection.SetClient(this); _Connection.LastActivity = DateTime.Now; _User.Status = UserStatus.Active; _bw.Write((int)TcpCommands.OK); _bw.WriteObject(_Program.Users.Keys.ToArray()); _bw.Flush(); SendArgsToOtherUsers((int)TcpCommands.UserLoggedOn, _User); } _inactivityTask = Task.Factory.StartNew(() => { while (!_inactivityTaskCancelationSource.Token.IsCancellationRequested) { CheckLastActivity(); Thread.Sleep(_inactivityDelayMS); } }, _inactivityTaskCancelationSource.Token, TaskCreationOptions.AttachedToParent, TaskScheduler.Current); Receiver(); } } catch (IOException) { // Thrown when client shuts down right when logging in if (_User != null) { _User.IsFoozReady = false; _User.Status = UserStatus.Offline; _User.IsTyping = false; _Connection.SetClient(null); lock ( _lock ) { if (_User.IsBot) { _Program.Users.Remove(_User); } SendArgsToAllUsers((int)TcpCommands.UserLoggedOut, _User.Username); } ConsoleMessage("{0} Logged Out Crazily", _User.Username); } CloseConn(); } catch (Exception ex) { ConsoleMessage("Exception has occured: {0}", ex.Message); } }