示例#1
0
        public void ThreadRun()
        {
            try
            {
                //소켓등록 후 서버에서 Hello Sign 전송.
                Hello();

                while (RunFlag)
                {
                    byte[] buffer   = new byte[Config.Instance().ReadBufferSize];
                    int    readSize = Stream.Read(buffer, 0, buffer.Length);

                    if (readSize < 0)
                    {
                        break;
                    }

                    if (RunFlag == false)
                    {
                        break;
                    }

                    string   strMsg = Config.Instance().encod.GetString(buffer, 0, readSize);
                    MsgModel msg    = new MsgModel(strMsg.Trim());

                    OnReceived(msg);
                }
            }catch (Exception ex)
            {
                Console.WriteLine("TCP READ SOCKET ERR : " + ex.Message);
            }

            Dispose();
        }
示例#2
0
        public int WriteMsg(MsgModel msg)
        {
            int result = -9999;

            if (Sock == null)
            {
                result = -504;
                return(result);
            }

            if (Sock.Connected == false)
            {
                result = -503;
                return(result);
            }

            try
            {
                string strMsg = msg.SendFormMsg();
                Console.WriteLine($"-- WRITE_MSG[{strMsg}]");
                byte[] buffer = Config.Instance().encod.GetBytes(strMsg);

                Stream.Write(buffer, 0, buffer.Length);
                Stream.Flush();
                result = buffer.Length;
            }
            catch (Exception ex)
            {
                Console.WriteLine("TCP WRITE SOCKET ERR : " + ex.Message);
            }

            return(result);
        }
示例#3
0
        public void Hello()
        {
            MsgModel msg = new MsgModel();

            msg.SetCmd(CmdType.HELLO_MSG);
            msg.Guid     = this.Guid;
            msg.UserName = "******";
            msg.Msg      = this.Guid;

            WriteMsg(msg);
        }
示例#4
0
        public void OnRecviedToClient(MsgModel msg)
        {
            Console.WriteLine($"MSG PUSH Q  [{msg.SendFormMsg()}]");

            GetMsgQueue().Enqueue(msg);
        }
示例#5
0
        /// <summary>
        /// 통신 서비스 함수.
        /// </summary>
        private void ServiceOperation()
        {
            Console.WriteLine("******** Service Start ******** ");

            while (ServiceFlag)
            {
                Thread.Sleep(300);

                if (GetMsgQueue().Count > 0)
                {
                    var msgModel = GetMsgQueue().Dequeue();

                    switch (msgModel.GetCmd())
                    {
                    case CmdType.HELLO_ACK:
                        Console.WriteLine($"## ONLINE CLIENT - #{msgModel.Guid}");
                        break;

                    case CmdType.CLIENT_MSG:    //클라이언트로 메시지 수신
                    {
                        Console.WriteLine($"# SENDING MSG : [{msgModel.SendFormMsg()}]");
                        for (int i = 0; i < GetClientList().Count; i++)
                        {
                            Client cl = GetClientList().ElementAt(i).Value;
                            if (cl.Guid == msgModel.Guid)
                            {
                                MsgModel res = msgModel;
                                res.SetCmd(CmdType.CLIENT_ACK);

                                cl.WriteMsg(res);
                            }
                            else
                            {
                                MsgModel res = msgModel;
                                res.SetCmd(CmdType.BROAD_MSG);

                                cl.WriteMsg(res);
                            }
                        }
                        break;
                    }

                    case CmdType.BROAD_ACK:
                        break;

                    case CmdType.FILE_INFO_REQ:
                    {
                        if (GetClientList().TryGetValue(msgModel.Guid, out Client cl))
                        {
                            if (GetFileList().Count < 1)
                            {
                                MsgModel res = msgModel;
                                res.UserName = "******";
                                res.SetCmd(CmdType.FILE_INFO_REQ);
                                res.Msg = "[WARN] There are NO Registered Files!";
                                cl.WriteMsg(res);
                            }
                            else
                            {
                                foreach (var file in GetFileList().Values)
                                {
                                    MsgModel res = msgModel;
                                    res.UserName = "******";
                                    res.SetCmd(CmdType.FILE_INFO_REQ);
                                    res.Msg = $"[{file.Guid}] {file.Name} / {file.Size} Byte";
                                    cl.WriteMsg(res);
                                }
                            }
                        }

                        break;
                    }

                    case CmdType.CLOSE_REQ:
                    {
                        if (GetClientList().TryGetValue(msgModel.Guid, out Client cl))
                        {
                            MsgModel res = msgModel;
                            res.SetCmd(CmdType.CLOSE_ACK);

                            cl.WriteMsg(res);

                            cl.Dispose();
                        }

                        break;
                    }

                    case CmdType.ALIVE:
                    {
                        if (GetClientList().TryGetValue(msgModel.Guid, out Client cl))
                        {
                            MsgModel res = msgModel;
                            res.SetCmd(CmdType.ALIVE_ACK);

                            cl.WriteMsg(res);
                        }

                        break;
                    }

                    case CmdType.ERROR_ACK:
                    case CmdType.UNKNOWN:
                    default:
                        break;
                    }
                }
            }

            Console.WriteLine("******** Service Finished ******** ");
        }