Exemplo n.º 1
0
        /// <summary>
        /// エコーサーバーの実行
        /// </summary>
        public void Run()
        {
            try
            {
                mrs_initialize();
                mrs_set_ssl_certificate_data(s_SslCertificateData);
                mrs_set_ssl_private_key_data(s_SslPrivateKeyData);

                using (var sig = new ExitSignal())
                using (var tcp_server = new MrsServerFoundation(MrsConnectionType.TCP, m_ArgServerAddr, m_ServerPort, m_BackLog)) // TCP
                using (var udp_server = new MrsServerFoundation(MrsConnectionType.UDP, m_ArgServerAddr, m_ServerPort, m_BackLog)) // UDP
                {
                    sig.SetSignal((obj, e) => {
                        SignalEventArgs event_args = e as SignalEventArgs;
                        MRS_LOG_DEBUG("Event: {0}", event_args.SignalEnums);
                        m_bLoop = false;
                        Thread.Sleep(10);
                    });

                    mrs_server_set_new_connection_callback(tcp_server.Server, OnNewConnection);
                    mrs_server_set_new_connection_callback(udp_server.Server, OnNewConnection);

                    mrs_set_error_callback(tcp_server.Server, m_OnError);
                    mrs_set_error_callback(udp_server.Server, m_OnError);

                    if (!m_isValidRecord)
                    {
                        mrs_set_read_callback(tcp_server.Server, m_OnRead);
                        mrs_set_read_callback(udp_server.Server, m_OnRead);
                    }

                    MRS_LOG_DEBUG("{0} listening on {1} {2}", ConnectionTypeToString( tcp_server.Server ), m_ArgServerAddr, m_ServerPort);
                    MRS_LOG_DEBUG("{0} waiting on {1} {2}", ConnectionTypeToString( udp_server.Server ), m_ArgServerAddr, m_ServerPort);

                    while (m_bLoop)
                    {
                        mrs_update();
                        mrs_sleep(m_SleepMsec);
                    }
                }
            }
            catch (Exception e)
            {
                MRS_LOG_ERR("exception: {0}", e.Message);
            }
            finally
            {
                mrs_finalize();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 実行
        /// </summary>
        public void Run()
        {
            try
            {
                mrs_initialize();
                using (ExitSignal sig = new ExitSignal())
                {
                    sig.SetSignal((obj, e) =>
                    {
                        m_bLoop = false;
                        Thread.Sleep(10);
                    });

                    uint connections = 0;
                    for (uint i = 0; i < m_nConnections; ++i)
                    {
                        MrsConnection client = g_Connect.FallbackConnect();
                        if (client == IntPtr.Zero)
                        {
                            MRS_LOG_ERR("mrs_connect[{0}]: {1}", i, mrs_get_error_string(mrs_get_last_error()));
                            break;
                        }
                        ++connections;
                    }
                    if (connections != m_nConnections)
                    {
                        return;
                    }

                    while (m_bLoop)
                    {
                        mrs_update();
                        mrs_sleep(m_SleepMsec);
                    }
                }
            }
            catch (Exception e)
            {
                MRS_LOG_ERR(e.Message);
            }
            finally
            {
                mrs_finalize();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// メイン関数
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            DateTime dateTime = DateTime.Now;

            // ファイル名の作成
            string fileName = string.Format("{0}.log", dateTime.ToString("yyyyMMddHHmmss"));

            using (StreamWriter writer = new StreamWriter(fileName))
            {
                using (ExitSignal sig = new ExitSignal())
                {
                    // ログコールバックの作成
                    GCHandle hLogCallback = GCHandle.Alloc(new MrsLogOutputCallback((level, msg) => {
                        string time_stamp = mrs.DateTime.Now().ToString();
                        string log        = string.Format("[{0}] {1}", time_stamp, msg);
                        mrs_console_log(level, log);
                        writer.WriteLine(log);
                    }), GCHandleType.Normal);
                    if (!hLogCallback.IsAllocated)
                    {
                        Console.WriteLine("Fail: alloc to delegate.");
                        return;
                    }

                    // 強制終了対応
                    sig.SetSignal((obj, e) => {
                        SignalEventArgs event_args = e as SignalEventArgs;
                        MRS_LOG_DEBUG("Event: {0}", event_args.SignalEnums);
                        s_bIsRun = false;
                        Thread.Sleep(10);                         // ここで待ち
                    });

                    // ログコールバックの登録
                    mrs_set_log_callback(hLogCallback.Target as MrsLogOutputCallback);

                    // mrsの初期化
                    mrs_initialize();
                    MRS_LOG_DEBUG("サーバーの開始");

                    try
                    {
                        while (s_bIsRun)
                        {
                            // mrsの更新
                            mrs_update();

                            // sleep
                            mrs_sleep(m_nSleepMsec);
                        }
                    }
                    finally
                    {
                        MRS_LOG_DEBUG("サーバーの終了");
                        // mrsの終了処理
                        mrs_finalize();
                        // ログコールバックの解放
                        hLogCallback.Free();
                    }
                }
            }
        }