Ejemplo n.º 1
0
        public void Send(OpCode opCode, int sequence, string message)
        {
            var bytes = ProtocolMaker.GetBytes(opCode, sequence, message);

            this.sendMessageQueue.Enqueue(bytes);
        }
Ejemplo n.º 2
0
        private void ReceiveMessage(NetworkStream stream)
        {
            List <byte> receiveBytes = new List <byte>();
            OpCode      opCode       = OpCode.None;
            int         sequence     = -1;

            while (true)
            {
                var bytes = new byte[4096];

                int n = stream.Read(bytes, 0, bytes.Length);
                try
                {
                    if (this.client.Connected)
                    {
                        byte[] data     = bytes;
                        int    stxIndex = -1;
                        int    etxIndex = -1;
                        int    thisOp   = -0;
                        int    thisSeq  = -0;

                        for (int i = 0; i < n; i++)
                        {
                            var  dataByte = data[i];
                            bool isData   = true;

                            if (stxIndex != -1)
                            {
                                if (opCode == OpCode.None)
                                {
                                    opCode = (OpCode)dataByte;
                                    thisOp++;
                                    isData = false;
                                }
                                else if (sequence == -1)
                                {
                                    sequence = (int)dataByte;
                                    thisSeq++;
                                    isData = false;
                                }
                            }

                            if (dataByte == ProtocolMaker.STX)
                            {
                                stxIndex = i;
                                isData   = false;
                            }
                            else if (dataByte == ProtocolMaker.ETX)
                            {
                                etxIndex = i;
                                isData   = false;
                            }

                            if (isData)
                            {
                                receiveBytes.Add(dataByte);
                            }

                            if (etxIndex != -1)
                            {
                                //Send
                                var opCodeType = ProtocolMaker.OpCodeToType(opCode);

                                if (typeof(string).Equals(opCodeType))
                                {
                                    OnReceived(opCode, sequence, ProtocolMaker.GetString(receiveBytes.ToArray()));
                                }
                                else
                                {
                                    OnReceived(opCode, sequence, Common.Util.FromJson(receiveBytes.ToArray(), opCodeType));
                                }
                                //run((dataformat.TransFormat{ Op: op, Seq: seq, Data: readMessage}))
                                opCode   = OpCode.None;
                                sequence = -1;
                                stxIndex = -1;
                                etxIndex = -1;
                                thisOp   = 0;
                                thisSeq  = 0;
                                receiveBytes.Clear();
                            }
                            else if (i == n - 1)
                            {
                                stxIndex = -1;
                                etxIndex = -1;
                                thisOp   = 0;
                                thisSeq  = 0;
                            }
                        }
                    }

                    this.receiveEvent.Set();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }

                Console.WriteLine("받기 대기시작!");
                this.receiveEvent.WaitOne();
                Console.WriteLine("받기 대기끝!");
            }
        }