Example #1
0
        /// <summary>
        /// Send
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void ButtonF5_Click(object sender, EventArgs e)
        {
            base.ButtonF5_Click(sender, e);
            try
            {
                // ▼▼▼ 業務処理 ▼▼▼

                string sendMsg  = tboxMessage.Text;
                string fromName = tboxName.Text;
                string toName   = cboxTarget.Text;

                TcpMessageUtility sendMsgMgr;
                if (toName.Equals("全員"))
                {
                    sendMsgMgr = new TcpMessageUtility(TcpMessageUtility.HeaderAllMsg, fromName, TcpMessageUtility.TargetAll, sendMsg);
                }
                else
                {
                    sendMsgMgr = new TcpMessageUtility(TcpMessageUtility.HeaderTargetMsg, fromName, toName, sendMsg);
                }

                TcpServerUtil.SendAll(sendMsgMgr.GetSendMessage());

                // 画面上にログを追加
                listViewLog.Items.Add(sendMsgMgr.GetRecvTargetMessage());
                // ▲▲▲ 業務処理 ▲▲▲
            }
            catch (Exception ex)
            {
                Logger.WriteException(MethodBase.GetCurrentMethod().Name, ex);
            }
        }
Example #2
0
        /// <summary>
        /// Send
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void ButtonF5_Click(object sender, EventArgs e)
        {
            base.ButtonF5_Click(sender, e);
            // ▼▼▼ 業務処理 ▼▼▼

            string sendMsg  = tboxMessage.Text;
            string fromName = tboxName.Text;
            string toName   = cboxTarget.Text;

            TcpMessageUtility sendMsgMgr;

            if (toName.Equals("全員"))
            {
                sendMsgMgr = new TcpMessageUtility(TcpMessageUtility.HeaderAllMsg, fromName, TcpMessageUtility.TargetAll, sendMsg);
            }
            else
            {
                sendMsgMgr = new TcpMessageUtility(TcpMessageUtility.HeaderTargetMsg, fromName, toName, sendMsg);
            }

            TcpServerUtil.SendAll(sendMsgMgr.GetSendMessage());
            // ▲▲▲ 業務処理 ▲▲▲
        }
Example #3
0
        private async Task ReadLoop(TcpServerManager.ClientInfo tcpClientMgr, CancellationToken cToken)
        {
            Logger.StartMethod(nameof(ReadLoop));

            try
            {
                TcpClient client = tcpClientMgr.Client;
                while (!cToken.IsCancellationRequested)
                {
                    // メッセージ受信
                    string texts = await TcpServerUtil.ReadAsync(client, cToken).ConfigureAwait(false);

                    if (texts != null)
                    {
                        // \nで分割
                        foreach (string text in texts.Split('\n'))
                        {
                            Logger.WriteLine(text);

                            Invoke((Action)(() =>
                            {
                                listViewLog.Items.Add($"[Read]{text}");
                            }));

                            TcpMessageUtility recvMsgMgr = new TcpMessageUtility(text);
                            TcpMessageUtility sendMsgMgr;
                            switch (recvMsgMgr.Header)
                            {
                            case TcpMessageUtility.HeaderConnect:
                                // 名前設定
                                // 辞書のキーの変更
                                dicTcpClient.Remove(tcpClientMgr.GetClientIpAndPort());

                                if (!dicTcpClient.ContainsKey(recvMsgMgr.Value))
                                {
                                    // 受信した名前が未登録の場合
                                    tcpClientMgr.Name = recvMsgMgr.Value;
                                    dicTcpClient.Add(tcpClientMgr.Name, tcpClientMgr);
                                }
                                else
                                {
                                    // 受信した名前が既に登録されている場合
                                    int    count = 0;
                                    string newName;
                                    do
                                    {
                                        // 番号を付加する
                                        count++;
                                        newName = recvMsgMgr.Value + count.ToString();
                                    } while (dicTcpClient.ContainsKey(newName));
                                    tcpClientMgr.Name = newName;
                                    dicTcpClient.Add(newName, tcpClientMgr);
                                }

                                // List&Comboに追加
                                Invoke((Action)(() =>
                                {
                                    listBoxUser.Items.Add(tcpClientMgr.Name);
                                    cboxTarget.Items.Add(tcpClientMgr.Name);
                                }));

                                // 全体に接続情報を送信
                                // Connect,IP:Port,受信した名前,登録した名前
                                sendMsgMgr = new TcpMessageUtility(TcpMessageUtility.HeaderConnect, tcpClientMgr.GetClientIpAndPort(), recvMsgMgr.Value, tcpClientMgr.Name);
                                TcpServerUtil.SendAll(sendMsgMgr.GetSendMessage());
                                break;

                            case TcpMessageUtility.HeaderTargetMsg:
                                // 送信元を設定
                                sendMsgMgr = new TcpMessageUtility(TcpMessageUtility.HeaderTargetMsg, tcpClientMgr.Name, recvMsgMgr.SendToTarget, recvMsgMgr.Value);

                                Invoke((Action)(() =>
                                {
                                    listViewLog.Items.Add(sendMsgMgr.GetRecvTargetMessage());
                                }));

                                // 受信したメッセージを対象に送信する
                                // 名前→IP:Portへの変換
                                string toTarget = dicTcpClient[recvMsgMgr.SendToTarget].GetClientIpAndPort();
                                TcpServerUtil.SendTarget(toTarget, sendMsgMgr.GetSendMessage());

                                // 受信したメッセージを送信元に送信する
                                // 名前→IP:Portへの変換
                                string fromTarget = tcpClientMgr.GetClientIpAndPort();
                                TcpServerUtil.SendTarget(fromTarget, sendMsgMgr.GetSendMessage());
                                break;

                            case TcpMessageUtility.HeaderAllMsg:
                            default:
                                // 送信元を設定
                                sendMsgMgr = new TcpMessageUtility(TcpMessageUtility.HeaderAllMsg, tcpClientMgr.Name, recvMsgMgr.SendToTarget, recvMsgMgr.Value);

                                Invoke((Action)(() =>
                                {
                                    listViewLog.Items.Add(sendMsgMgr.GetRecvTargetMessage());
                                }));

                                // 受信したメッセージを全体に送信する
                                TcpServerUtil.SendAll(sendMsgMgr.GetSendMessage());
                                break;
                            }
                        }
                    }
                    else
                    {
                        // 切断
                        Logger.WriteLine(nameof(ReadLoop), "切断されました。");
                        string deleteTargetIpAndPort = tcpClientMgr.GetClientIpAndPort();
                        _ = dicTcpClient.Remove(deleteTargetIpAndPort);
                        TcpServerUtil.Delete(deleteTargetIpAndPort);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                Logger.WriteException(nameof(ReadLoop), ex);
            }

            Logger.EndMethod(nameof(ReadLoop));
        }