Exemplo n.º 1
0
        static private void OnSendAckComplete(IAsyncResult result)
        {
            CukunityClient cukieClient = result.AsyncState as CukunityClient;

            cukieClient.stream.EndWrite(result);
            cukieClient.BeginRead();
        }
Exemplo n.º 2
0
        static private void OnReadComplete(IAsyncResult result)
        {
            CukunityClient cukieClient = result.AsyncState as CukunityClient;
            int            byteCount   = cukieClient.stream.EndRead(result);
            string         line;

            // nothing received?
            if (byteCount <= 0)
            {
                cukieClient.Close();
                return;
            }

            // append read data to internal buffer
            cukieClient.bufferedString  += enc.GetString(cukieClient.buffer, cukieClient.bufferUsedCount, byteCount);
            cukieClient.bufferUsedCount += byteCount;

            // can we extract a line from this buffer?
            while ((line = cukieClient.ExtractLine(cukieClient.bufferedString)) != null)
            {
                int lineByteCount = enc.GetByteCount(line);
                cukieClient.bufferedString   = cukieClient.bufferedString.Remove(0, lineByteCount);
                cukieClient.bufferUsedCount -= lineByteCount;

                // process received line
                string response = cukieClient.ProcessLineRequest(line);
                if (response.Length > 0)
                {
                    // respond to client
                    byte[] responseBytes = enc.GetBytes(response + "\n");
                    cukieClient.stream.BeginWrite(responseBytes, 0, responseBytes.Length, null, null);
                }
                else
                {
                    cukieClient.Close();
                    return;
                }
            }

            // next read
            cukieClient.BeginRead();
        }