Ejemplo n.º 1
0
        protected virtual void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                StateObj clientObj = ar.AsyncState as StateObj;
                _receiveDone.Set();
                //System.IO.MemoryStream ms = new System.IO.MemoryStream(); ms.Seek(0, System.IO.SeekOrigin.Begin);
                int receiveLength = clientObj.ClientSocket.EndReceive(ar);

                if (receiveLength < clientObj.Receive_buffer.Length)
                {
                    Array.Resize(ref clientObj.Receive_buffer, receiveLength);
                }
                lock (lockObj)
                {
                    //Think:如果Client送過來的資料太大會被分割成數塊buffer大小的數據,測試時看起來依然會依切割的順序進來,不會亂入
                    //不過這樣判斷就需要有個判斷, 可以知道資料的結尾, 才能清除存資料的buffer
                    //ref:http://stackoverflow.com/questions/18368130/how-to-parse-and-validate-a-websocket-frame-in-java
                    if ((clientObj.Receive_buffer[0] & 0x80) != 0x80)
                    {
                        this.buffer.Clear();//要清除buffer
                    }
                    foreach (byte b in clientObj.Receive_buffer)
                    {
                        this.buffer.Enqueue(b);
                    }
                    dataCollection.Add(clientObj.SequnceNO, clientObj.Receive_buffer);
                    //Available 無法當依據,兩個非同步同時進來都是0
                    if (clientObj.ClientSocket.Available > 0)
                    {
                        Logger.WriteLog("還有多少數據可讀取:" + clientObj.ClientSocket.Available);
                        return;
                    }
                    else
                    {
                        Logger.WriteLog("Buffer資料量:" + this.buffer.Count);
                        Logger.WriteLog("Data:" + BitConverter.ToString(this.buffer.ToArray()));
                        Console.WriteLine("按一下吧 Available:{0}", clientObj.ClientSocket.Available);
                        //Console.ReadKey();
                        Console.WriteLine("dataCollection:{0}", dataCollection.Count);
                        //Console.ReadKey();
                    }
                }
                //lock (receiveData)
                //{
                //receiveData = receiveData.Concat(clientObj.ReceiveData);
                //}
                string echo  = "hello i am server!國字";
                byte[] send2 = Package(Encoding.UTF8.GetBytes(echo));
                clientObj.ClientSocket.Send(send2, 0, send2.Length, SocketFlags.None);
                //判斷websocket frame格式
                bool over126 = false;
                long payloadSize_over126;
                //下一次來的mask-key要重取
                if (/*null == _maskingKey && */ !FilterPayloadData(this.buffer.ToArray(), out over126, out payloadSize_over126))
                {
                    _clientHandler.CancelAsync();
                }
                byte[] data    = ParseRealData(_maskingKey, this.buffer.ToArray(), this._kindOfLength + this._maskingKey.Length);
                string receive = Encoding.UTF8.GetString(data);        //超過ASCII的長度就是3 bytes為一個單位
                Logger.WriteLog("Client " + clientObj.SequnceNO + " ReceiveData:" + receive);
                if (receive.Contains("Exit"))
                {
                    _keepAlive = false;
                    _receiveDone.Set();
                }
                byte[] sendData = Encoding.UTF8.GetBytes("Server Say:" + receive);
                sendData = Package(sendData);
                clientObj.ClientSocket.BeginSend(sendData, 0, sendData.Length, SocketFlags.None, SendCallback, clientObj.ClientSocket);
                Logger.WriteLog("SendLength:" + sendData.Length + " Server Say:" + receive);
            }
            catch (SocketException sckEx)
            {
                Logger.WriteLog(sckEx.Message);
            }
        }
Ejemplo n.º 2
0
 public void Handle(AbsRequestHandler handler)
 {
     Logger.WriteLog("Client " + handler.ClientNo + " Exit ...");
     //exit state
     handler.CancelAsync();
 }
Ejemplo n.º 3
0
 public void Handle(AbsRequestHandler handler)
 {
     Logger.WriteLog("Client " + handler.ClientNo + " Exit ...");
     //exit state
     handler.CancelAsync();
 }