C#自定义事件

声明委托

public delegate void ServerHandler(ServerConfig e);

在需要被触发的事件类内绑定委托

public event ServerHandler ServerStop;
public event ServerHandler ServerStart;

触发事件

//防止没有订阅引发异常
if(ServerStart != null) ServerStart(new ServerConfig()); 
if(ServerStop != null)  ServerStop(new ServerConfig());

订阅事件

Server.ServerStart += new ServerHandler(this.Server_Start);
Server.ServerStop += new ServerHandler(this.Server_Stop);

private void Server_Start(ServerConfig e)
{
    MessageBox.Show("服务端进程启动~");
}

private void Server_Stop(ServerConfig e)
{
    MessageBox.Show("服务端进程退出~");
}

记得释放事件 以免发生内存泄漏!

CentOS7 安装 LNMP

CentOS7 configuration:

systemctl disable firewalld && systemctl stop firewalld

Install dependency package:

yum install wget unzip tar gcc gcc-c++ make \
bison ncurses perl pcre pcre-devel zlib-devel cmake \
gnutls openssl jemalloc libaio libaio-devel ncurses \
ncurses-devel libxml2 libxml2-devel \
openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel \
libpng libpng-devel libjpeg libjpeg-devel freetype freetype-devel libvpx libvpx-devel \
libmcrypt libmcrypt-devel -y

MySQL

Download and install MySQL:

cd /usr/src/ && wget https://files.x-speed.cc/link/8132351340035751 -O mysql.tar.gz && tar -xvf ./mysql.tar.gz && cd mysql-*

Complie MySQL:

cmake \
-DCMAKE_INSTALL_PREFIX=/opt/mysql \
-DMYSQL_DATADIR=/opt/mysql/data \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/dev/shm/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci

make -j 8 && make install

Configuration MySQL permission:

useradd -M -r --shell /sbin/nologin --home-dir /opt/mysql mysql && chown -R mysql:mysql /opt/mysql

rm -rf /etc/my.cnf && cd /opt/mysql && ./scripts/mysql_install_db --basedir=/opt/mysql --datadir=/opt/mysql/data --user=mysql

cp ./support-files/mysql.server /etc/init.d/mysqld && chmod +x /etc/init.d/mysqld && systemctl enable mysqld

echo -e '\n\nexport PATH=/opt/mysql/bin:$PATH\n' >> /etc/profile && source /etc/profile

vi /etc/my.cnf

Edit MySQL configuration file.

[client]
port = 3306
socket = /dev/shm/mysql.sock

[mysqld]
user = mysql
skip-innodb
default-storage-engine=MyISAM
skip-name-resolve
bind-address = 0.0.0.0
port = 3306
socket = /dev/shm/mysql.sock
skip-external-locking
key_buffer_size = 16K
max_allowed_packet = 1M
table_open_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 240K
server-id = 1

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M

[mysqlhotcopy]
interactive-timeout

Launch MySQL process:

systemctl start mysqld

Nginx

Download and install Nginx:

cd /usr/src/ && \
wget https://files.x-speed.cc/link/8132351340035753 -O nginx-ct.tar.gz && tar -xvf ./nginx-ct.tar.gz && \
wget https://files.x-speed.cc/link/8149821340045787 -O openssl.tar.gz && tar -xvf ./openssl.tar.gz && \
wget https://files.x-speed.cc/link/6132321318156618 -O nginx.tar.gz && tar -xvf ./nginx.tar.gz && cd nginx-*

Complie Nginx:

./configure --prefix=/opt/nginx --user=www --group=www --with-http_ssl_module --with-http_v2_module --with-stream --with-openssl=../openssl-1.0.2k --add-module=../nginx-ct-1.3.2

make -j 8 && make install

Edit Nginx service file:

vi /etc/init.d/nginx
#!/bin/sh
#
# nginx – this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /opt/nginx/conf/nginx.conf
# pidfile: /opt/nginx/logs/nginx.pid

# Source function library.
. /etc/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/opt/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    start
}

reload() {
    configtest || return $?
    echo -n $”Reloading $prog: ”
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
    $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
    esac

Configuration Nginx permission and register service:

useradd -M -r --shell /sbin/nologin --home-dir /opt/nginx www && chown -R www:www /opt/nginx && \
chmod +x /etc/init.d/nginx && \
systemctl enable nginx && \
systemctl start nginx

PHP

Download and install PHP:

cd /usr/src/ && wget https://files.x-speed.cc/link/6132321318156617 -O php.tar.gz && tar -xvf ./php.tar.gz && cd php-*

Complie PHP:

./configure --prefix=/opt/php \
--with-config-file-path=/opt/php/etc \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-opcache \
--enable-fpm \
--with-fpm-user=www --with-fpm-group=www \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-gettext \
--enable-mbstring \
--with-iconv \
--with-mcrypt \
--with-mhash \
--with-openssl \
--enable-bcmath \
--enable-soap \
--with-libxml-dir \
--enable-pcntl \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-sockets \
--with-curl \
--with-zlib \
--enable-zip \
--with-bz2 \
--with-gd

make -j 8 && make install

Configuration php:

cp ./php.ini-production /opt/php/etc/php.ini && \
cp /opt/php/etc/php-fpm.conf.default /opt/php/etc/php-fpm.conf && \
cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && \
chmod +x /etc/init.d/php-fpm

Register php-fpm service:

systemctl enable php-fpm && systemctl start php-fpm

Configuration php-fpm(/opt/php/etc/php-fpm.conf):

[global]
pid = run/php-fpm.pid
log_level = error

[www]
listen = 127.0.0.1:9000
user = www
group = www
pm = dynamic
pm.max_children = 5
pm.start_servers = 1
pm.min_spare_servers = 1
pm.max_spare_servers = 5
request_terminate_timeout = 30
rlimit_files = 51200

Bukkit动态引入Jar包

需要把Jar包丢进配置文件夹的lib目录下才行
onEnable()时调用addBukkitLibraries()即可!

public void addBukkitLibraries()
{
        try
            {
                Method method = URLClassLoader.class.getDeclaredMethod("addURL",new Class[] { URL.class });
                method.setAccessible(true);
                ClassLoader loader = this.getClassLoader();
                for (File file : new File(this.getDataFolder(), "lib").listFiles())
                {
                    if (file.getName().endsWith(".jar"))
                    {
                        URL url = file.toURI().toURL();
                        method.invoke(loader, new Object[] { url });
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

利用Process获取CPU占用率

using (var pro = Process)
{
    //间隔时间(毫秒)
    int interval = 1000;
    //上次记录的CPU时间
    var prevCpuTime = TimeSpan.Zero;
    while (true)
    {
        try
        {
            //当前时间
            var curTime = pro.TotalProcessorTime;
            //多核占用
            //var value = (curTime - prevCpuTime).TotalMilliseconds / interval / Environment.ProcessorCount * 100;
            //单核占用
            var value = (curTime - prevCpuTime).TotalMilliseconds / interval * 100;
            prevCpuTime = curTime;
            CPU = value;
        }catch{
            CPU = 0;
        }
        Thread.Sleep(interval);
    }
}